-
Notifications
You must be signed in to change notification settings - Fork 63
Use PathElementSet and similar to index PathElement #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package fieldpath | ||
|
||
import ( | ||
"sort" | ||
|
||
"sigs.k8s.io/structured-merge-diff/value" | ||
) | ||
|
||
// PathElementValueMap is a map from PathElement to value.Value. | ||
// | ||
// TODO(apelisse): We have multiple very similar implementation of this | ||
// for PathElementSet and SetNodeMap, so we could probably share the | ||
// code. | ||
type PathElementValueMap struct { | ||
members sortedPathElementValues | ||
} | ||
|
||
func MakePathElementValueMap(size int) PathElementValueMap { | ||
return PathElementValueMap{ | ||
members: make(sortedPathElementValues, 0, size), | ||
} | ||
} | ||
|
||
type pathElementValue struct { | ||
PathElement PathElement | ||
Value value.Value | ||
} | ||
|
||
type sortedPathElementValues []pathElementValue | ||
|
||
// Implement the sort interface; this would permit bulk creation, which would | ||
// be faster than doing it one at a time via Insert. | ||
func (spev sortedPathElementValues) Len() int { return len(spev) } | ||
func (spev sortedPathElementValues) Less(i, j int) bool { | ||
return spev[i].PathElement.Less(spev[j].PathElement) | ||
} | ||
func (spev sortedPathElementValues) Swap(i, j int) { spev[i], spev[j] = spev[j], spev[i] } | ||
|
||
// Insert adds the pathelement and associated value in the map. | ||
func (s *PathElementValueMap) Insert(pe PathElement, v value.Value) { | ||
loc := sort.Search(len(s.members), func(i int) bool { | ||
return !s.members[i].PathElement.Less(pe) | ||
}) | ||
if loc == len(s.members) { | ||
s.members = append(s.members, pathElementValue{pe, v}) | ||
return | ||
} | ||
if s.members[loc].PathElement.Equals(pe) { | ||
return | ||
} | ||
s.members = append(s.members, pathElementValue{}) | ||
copy(s.members[loc+1:], s.members[loc:]) | ||
s.members[loc] = pathElementValue{pe, v} | ||
} | ||
|
||
// Get retrieves the value associated with the given PathElement from the map. | ||
// (nil, false) is returned if there is no such PathElement. | ||
func (s *PathElementValueMap) Get(pe PathElement) (value.Value, bool) { | ||
loc := sort.Search(len(s.members), func(i int) bool { | ||
return !s.members[i].PathElement.Less(pe) | ||
}) | ||
if loc == len(s.members) { | ||
return value.Value{}, false | ||
} | ||
if s.members[loc].PathElement.Equals(pe) { | ||
return s.members[loc].Value, true | ||
} | ||
return value.Value{}, false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package fieldpath | ||
|
||
import ( | ||
"testing" | ||
|
||
"sigs.k8s.io/structured-merge-diff/value" | ||
) | ||
|
||
func TestPathElementValueMap(t *testing.T) { | ||
m := PathElementValueMap{} | ||
|
||
if _, ok := m.Get(PathElement{FieldName: strptr("onion")}); ok { | ||
t.Fatal("Unexpected path-element found in empty map") | ||
} | ||
|
||
m.Insert(PathElement{FieldName: strptr("carrot")}, value.StringValue("knife")) | ||
m.Insert(PathElement{FieldName: strptr("chive")}, value.IntValue(2)) | ||
|
||
if _, ok := m.Get(PathElement{FieldName: strptr("onion")}); ok { | ||
t.Fatal("Unexpected path-element in map") | ||
} | ||
|
||
if val, ok := m.Get(PathElement{FieldName: strptr("carrot")}); !ok { | ||
t.Fatal("Missing path-element in map") | ||
} else if !val.Equals(value.StringValue("knife")) { | ||
t.Fatalf("Unexpected value found: %#v", val) | ||
} | ||
|
||
if val, ok := m.Get(PathElement{FieldName: strptr("chive")}); !ok { | ||
t.Fatal("Missing path-element in map") | ||
} else if !val.Equals(value.IntValue(2)) { | ||
t.Fatalf("Unexpected value found: %#v", val) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,7 +142,7 @@ func (v *validatingObjectWalker) doScalar(t *schema.Scalar) ValidationErrors { | |
} | ||
|
||
func (v *validatingObjectWalker) visitListItems(t *schema.List, list *value.List) (errs ValidationErrors) { | ||
observedKeys := map[string]struct{}{} | ||
observedKeys := fieldpath.MakePathElementSet(len(list.Items)) | ||
for i, child := range list.Items { | ||
pe, err := listItemToPathElement(t, i, child) | ||
if err != nil { | ||
|
@@ -152,11 +152,10 @@ func (v *validatingObjectWalker) visitListItems(t *schema.List, list *value.List | |
// this element. | ||
continue | ||
} | ||
keyStr := pe.String() | ||
if _, found := observedKeys[keyStr]; found { | ||
errs = append(errs, v.errorf("duplicate entries for key %v", keyStr)...) | ||
if observedKeys.Has(pe) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really faster? i know the benchmarks are better but it makes the constant time operation with something that has to sort a slice and do a binary search There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @lavalamp It is slightly faster but most importantly it's more correct. Using the string as the index is a little wrong since it's not 100% canonicalized and is not meant to be used for "production use". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Allocating a buffer for the string, converting the pathelement to a string, hashing the string, comparing the strings, is quite expensive too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. String() was actually written to make this work, so this should be equally correct. The set impl is O(log n) but with a low enough constant factor that it was faster than the O(1) thing it replaced. Now we don't have a O(1) thing to compare it with, I assume at some size it would stop being faster. |
||
errs = append(errs, v.errorf("duplicate entries for key %v", pe.String())...) | ||
} | ||
observedKeys[keyStr] = struct{}{} | ||
observedKeys.Insert(pe) | ||
v2 := v.prepareDescent(pe, t.ElementType) | ||
v2.value = child | ||
errs = append(errs, v2.validate()...) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm surprised I missed these uses of strings when I was working on this last quarter...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyways, /shrug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is a fairly small improvement compared to some of the things that are coming.