Skip to content

memory pools for walkers #97

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

Merged
merged 1 commit into from
Aug 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions typed/typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package typed

import (
"fmt"
"sync"

"sigs.k8s.io/structured-merge-diff/fieldpath"
"sigs.k8s.io/structured-merge-diff/schema"
Expand Down Expand Up @@ -66,7 +67,9 @@ func (tv TypedValue) AsValue() *value.Value {

// Validate returns an error with a list of every spec violation.
func (tv TypedValue) Validate() error {
if errs := tv.walker().validate(); len(errs) != 0 {
w := tv.walker()
defer w.finished()
if errs := w.validate(); len(errs) != 0 {
return errs
}
return nil
Expand All @@ -77,6 +80,7 @@ func (tv TypedValue) Validate() error {
func (tv TypedValue) ToFieldSet() (*fieldpath.Set, error) {
s := fieldpath.NewSet()
w := tv.walker()
defer w.finished()
w.leafFieldCallback = func(p fieldpath.Path) { s.Insert(p) }
w.nodeFieldCallback = func(p fieldpath.Path) { s.Insert(p) }
if errs := w.validate(); len(errs) != 0 {
Expand Down Expand Up @@ -203,6 +207,10 @@ func (tv TypedValue) Empty() *TypedValue {
return &tv
}

var mwPool = sync.Pool{
New: func() interface{} { return &mergingWalker{} },
}

func merge(lhs, rhs *TypedValue, rule, postRule mergeRule) (*TypedValue, error) {
if lhs.schema != rhs.schema {
return nil, errorFormatter{}.
Expand All @@ -213,14 +221,27 @@ func merge(lhs, rhs *TypedValue, rule, postRule mergeRule) (*TypedValue, error)
errorf("expected objects of the same type, but got %v and %v", lhs.typeRef, rhs.typeRef)
}

mw := mergingWalker{
lhs: &lhs.value,
rhs: &rhs.value,
schema: lhs.schema,
typeRef: lhs.typeRef,
rule: rule,
postItemHook: postRule,
}
mw := mwPool.Get().(*mergingWalker)
defer func() {
mw.lhs = nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work if you do *mw = mergingWalker{}?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I'm deliberately preserving two of the fields...

mw.rhs = nil
mw.schema = nil
mw.typeRef = schema.TypeRef{}
mw.rule = nil
mw.postItemHook = nil
mw.out = nil
mw.inLeaf = false

mwPool.Put(mw)
}()

mw.lhs = &lhs.value
mw.rhs = &rhs.value
mw.schema = lhs.schema
mw.typeRef = lhs.typeRef
mw.rule = rule
mw.postItemHook = postRule

errs := mw.merge()
if len(errs) > 0 {
return nil, errs
Expand Down
26 changes: 21 additions & 5 deletions typed/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,33 @@ limitations under the License.
package typed

import (
"sync"

"sigs.k8s.io/structured-merge-diff/fieldpath"
"sigs.k8s.io/structured-merge-diff/schema"
"sigs.k8s.io/structured-merge-diff/value"
)

var vPool = sync.Pool{
New: func() interface{} { return &validatingObjectWalker{} },
}

func (tv TypedValue) walker() *validatingObjectWalker {
return &validatingObjectWalker{
value: tv.value,
schema: tv.schema,
typeRef: tv.typeRef,
}
v := vPool.Get().(*validatingObjectWalker)
v.value = tv.value
v.schema = tv.schema
v.typeRef = tv.typeRef
return v
}

func (v *validatingObjectWalker) finished() {
v.value = value.Value{}
v.schema = nil
v.typeRef = schema.TypeRef{}
v.leafFieldCallback = nil
v.nodeFieldCallback = nil
v.inLeaf = false
vPool.Put(v)
}

type validatingObjectWalker struct {
Expand Down