Skip to content

Commit 09368ac

Browse files
authored
fix(selectorvalidator): memory optimization (#97)
Do not reconstruct a complicated selector on each call to selector validate
1 parent a0cc6f0 commit 09368ac

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

selectorvalidator/selectorvalidator.go

+19-18
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,15 @@ var (
1919
ErrInvalidLimit = errors.New("unsupported recursive selector limit")
2020
)
2121

22-
// SelectorValidator returns an OnRequestReceivedHook that only validates
23-
// requests if their selector only has no recursions that are greater than
24-
// maxAcceptedDepth
25-
func SelectorValidator(maxAcceptedDepth int) graphsync.OnIncomingRequestHook {
26-
return func(p peer.ID, request graphsync.RequestData, hookActions graphsync.IncomingRequestHookActions) {
27-
err := ValidateMaxRecursionDepth(request.Selector(), maxAcceptedDepth)
28-
if err == nil {
29-
hookActions.ValidateRequest()
30-
}
31-
}
32-
}
22+
var maxDepthSelector selector.Selector
3323

34-
// ValidateMaxRecursionDepth examines the given selector node and verifies
35-
// recursive selectors are limited to the given fixed depth
36-
func ValidateMaxRecursionDepth(node ipld.Node, maxAcceptedDepth int) error {
24+
func init() {
3725
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Map)
3826

3927
// this selector is a selector for traversing selectors...
4028
// it traverses the various selector types looking for recursion limit fields
4129
// and matches them
42-
s, err := ssb.ExploreRecursive(selector.RecursionLimitNone(), ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) {
30+
maxDepthSelector, _ = ssb.ExploreRecursive(selector.RecursionLimitNone(), ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) {
4331
efsb.Insert(selector.SelectorKey_ExploreRecursive, ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) {
4432
efsb.Insert(selector.SelectorKey_Limit, ssb.Matcher())
4533
efsb.Insert(selector.SelectorKey_Sequence, ssb.ExploreRecursiveEdge())
@@ -61,12 +49,25 @@ func ValidateMaxRecursionDepth(node ipld.Node, maxAcceptedDepth int) error {
6149
efsb.Insert(selector.SelectorKey_Next, ssb.ExploreRecursiveEdge())
6250
}))
6351
})).Selector()
52+
}
6453

65-
if err != nil {
66-
return err
54+
// SelectorValidator returns an OnRequestReceivedHook that only validates
55+
// requests if their selector only has no recursions that are greater than
56+
// maxAcceptedDepth
57+
func SelectorValidator(maxAcceptedDepth int) graphsync.OnIncomingRequestHook {
58+
return func(p peer.ID, request graphsync.RequestData, hookActions graphsync.IncomingRequestHookActions) {
59+
err := ValidateMaxRecursionDepth(request.Selector(), maxAcceptedDepth)
60+
if err == nil {
61+
hookActions.ValidateRequest()
62+
}
6763
}
64+
}
65+
66+
// ValidateMaxRecursionDepth examines the given selector node and verifies
67+
// recursive selectors are limited to the given fixed depth
68+
func ValidateMaxRecursionDepth(node ipld.Node, maxAcceptedDepth int) error {
6869

69-
return traversal.WalkMatching(node, s, func(progress traversal.Progress, visited ipld.Node) error {
70+
return traversal.WalkMatching(node, maxDepthSelector, func(progress traversal.Progress, visited ipld.Node) error {
7071
if visited.ReprKind() != ipld.ReprKind_Map || visited.Length() != 1 {
7172
return ErrInvalidLimit
7273
}

0 commit comments

Comments
 (0)