memstore: speed up LoadNextMsg when using wildcard filter#7840
memstore: speed up LoadNextMsg when using wildcard filter#7840neilalexander merged 1 commit intomainfrom
Conversation
|
Results for the benchmarks in in this PR: This PR improves the case "wildcard_bounded_scan". This case populates a memstore with 10 million messages and the repeatedly calls LoadNextMsg until EOF. In this case, the filter given to LoadNextMsg will only match subset of 100 messages out of the 10 million. The other cases show no regresions, nor improvements. That's the expectation. |
neilalexander
left a comment
There was a problem hiding this comment.
Overall looks great, couple minor things.
| // Internal function which can be called recursively to match all leaf nodes to a given filter subject which | ||
| // once here has been decomposed to parts. These parts only care about wildcards, both pwc and fwc. | ||
| func (t *SubjectTree[T]) match(n node, parts [][]byte, pre []byte, cb func(subject []byte, val *T)) { | ||
| // Returns false if the callback requested to stop matching. |
There was a problem hiding this comment.
Does anything use this return value outside of tests? Trying to decide if it's a useful value.
There was a problem hiding this comment.
Currently no use outside of tests. I don't have a strong opinion on this. Let me know if I should remove it.
server/memstore.go
Outdated
| if start > ss.Last { | ||
| return 0, 0, false | ||
| } | ||
| if ss.First > start { |
There was a problem hiding this comment.
Might be able to simplify this into a max() case on the return line for brevity? e.g. return max(start, ss.First), ...
Detailed review of the MatchUntil + early-termination optimization for wildcard-filtered LoadNextMsg. Identifies a benchmark bug (start/count not reset between b.Loop() iterations) and suggests reconsidering the linearScanMaxFSS=256 threshold which limits the optimization to small subject trees. https://claude.ai/code/session_0198NdQiRC8VME4WzZyQYEt3
Ran benchmarks on both base and PR commits with the start/count reset fix applied for accurate measurements. Results confirm ~12,400x speedup for wildcard bounded scan (4s -> 323us) with no regressions in linear scan or literal scan paths. https://claude.ai/code/session_0198NdQiRC8VME4WzZyQYEt3
| name: "wildcard_linear_scan", | ||
| msgs: 10_000_000, | ||
| matchingMsgEvery: 10_000, | ||
| filter: "foo.baz.*", |
There was a problem hiding this comment.
this does not match the subject from the stream config but it is ok for the test? []string{"foo.*"}
There was a problem hiding this comment.
event though it made no difference
This commit speeds up wildcard based filtering: - Avoid expanding the bounds for matching fss entries that are past our search - Avoid unnecessary creation of a list with matching subjects - Introduce MatchUntil, this allows early stop if we find a match with first <= start Signed-off-by: Daniele Sciascia <daniele@nats.io>
50e15a5 to
0fd9865
Compare
|
There was a bug in the benchmark accounting. Variables What changed? The bug favored somewhat the optimization. Now we have |
This commit speeds up wildcard based filtering:
Signed-off-by: Daniele Sciascia daniele@nats.io