Skip to content
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

[IMPROVED] NumPending calculations and subject index memory in filestore #4960

Merged
merged 12 commits into from Jan 20, 2024

Conversation

derekcollison
Copy link
Member

Swapped out psim as a go hashmap for our stree implementation.

Stree is an adaptive radix tree implementation used for storing and retrieving literal subjects. It also allows quick matching to wildcard subjects, which is it's major design goal along with using less memory in high subject cardinality situations.

This will be used in the filestore implementation to replace the PSIM hash map which was fast at insert and lookup but suffered when trying to filter based on wildcard subjects.

This is used specifically in calculations on NumPending with a wildcard, and given we push folks to use larger muxed streams with down filtered consumers and/or mirrors this was becoming a performance issue.

Signed-off-by: Derek Collison derek@nats.io

Stree is an adaptive radix tree implementation used for storing and retrieving literal subjects. It also allows quick matching to wildcard subjects, which is it's major design goal along with using less memory in high subject cardinality situations.
This will be used in the filestore implementation to replace the PSIM hash map which was fast at insert and lookup but suffered when trying to filter based on wildcard subjects.

This is used specifically in calculations on NumPending with a wildcard, and given we push folks to use larger muxed streams with down filtered consumers and/or mirrors this was becoming a performance issue.

Signed-off-by: Derek Collison <derek@nats.io>
Signed-off-by: Derek Collison <derek@nats.io>
@derekcollison derekcollison requested a review from a team as a code owner January 16, 2024 14:11
Copy link
Member

@neilalexander neilalexander left a comment

Choose a reason for hiding this comment

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

Overall looks good! Few minor things I noticed.

server/stree/node16.go Outdated Show resolved Hide resolved
server/stree/node16.go Outdated Show resolved Hide resolved
server/stree/node16.go Outdated Show resolved Hide resolved
server/stree/node4.go Outdated Show resolved Hide resolved
server/stree/node4.go Outdated Show resolved Hide resolved
Signed-off-by: Derek Collison <derek@nats.io>
Copy link
Member

@neilalexander neilalexander left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@Jarema Jarema left a comment

Choose a reason for hiding this comment

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

Still doing deeper dive into some parts of the codebase and benching it, but code itself looks good!

LGTM!

server/stree/dump.go Outdated Show resolved Hide resolved
derekcollison and others added 8 commits January 18, 2024 02:59
Signed-off-by: Derek Collison <derek@nats.io>
Signed-off-by: Neil Twigg <neil@nats.io>
The `_pre` and `cparts` copies are more often than not unnecessary and
result in potentially gigabytes of allocations which can slow down
functions like `NumPending`.

Passing `pre` and `nparts` down recursively without copies is usually
safe, even where there are appends in those, because there is no
concurrency and those appends will not modify the slice length back at
the callsite. Instead we'll only reallocate either when `append` does so
naturally (we've ran out of capacity) or when we know specifically that
we're modifying something in-place (like in `matchParts`).

This improves performance of things like `NumPending` with high subject
cardinality.

Signed-off-by: Neil Twigg <neil@nats.io>
When using `make(x, y, z)`, there is a heap escape due to the non-constant
size/capacity. Try to stay on the stack instead, reducing GC pressure.

Signed-off-by: Neil Twigg <neil@nats.io>
When using `make(x, y, z)`, there is a heap escape due to the
non-constant size/capacity. Try to stay on the stack instead, reducing
GC pressure.

Signed-off-by: Neil Twigg <neil@nats.io>
In #4974 I removed preallocated buffers for `pre` as the copies were
unnecessary on each single recursion, however it turns out that having
a preallocation up front removes quite a few unnecessary allocations
from subject construction, relieving further pressure on the GC.

Signed-off-by: Neil Twigg <neil@nats.io>
In #4974 I removed preallocated buffers for `pre` as the copies were
unnecessary on each single recursion, however it turns out that having a
preallocation up front removes quite a few unnecessary allocations from
subject construction as the underlying memory gets reused throughout the
iteration or match process, relieving further pressure on the GC.

Signed-off-by: Neil Twigg <neil@nats.io>
Signed-off-by: Neil Twigg <neil@nats.io>
Copy link
Member

@neilalexander neilalexander left a comment

Choose a reason for hiding this comment

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

Otherwise LGTM!

server/stree/bench_test.go Outdated Show resolved Hide resolved
…ing memory for the GC.

The solution was to have a node return its children as a []node. Since node256 is sparse the upper layers need to check for nil, but this improved the performance.

Signed-off-by: Derek Collison <derek@nats.io>
@derekcollison derekcollison merged commit d9235ab into main Jan 20, 2024
4 checks passed
@derekcollison derekcollison deleted the stree branch January 20, 2024 18:59
Copy link
Member

@wallyqs wallyqs left a comment

Choose a reason for hiding this comment

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

LGTM

derekcollison added a commit that referenced this pull request Jan 22, 2024
This updates the memory store to use the new subject tree from #4960 for
per-subject tracking.

Signed-off-by: Neil Twigg <neil@nats.io>
neilalexander added a commit that referenced this pull request Jan 22, 2024
…ore (#4960)

Swapped out psim as a go hashmap for our stree implementation.

Stree is an adaptive radix tree implementation used for storing and
retrieving literal subjects. It also allows quick matching to wildcard
subjects, which is it's major design goal along with using less memory
in high subject cardinality situations.

This will be used in the filestore implementation to replace the PSIM
hash map which was fast at insert and lookup but suffered when trying to
filter based on wildcard subjects.

This is used specifically in calculations on NumPending with a wildcard,
and given we push folks to use larger muxed streams with down filtered
consumers and/or mirrors this was becoming a performance issue.

Signed-off-by: Derek Collison <derek@nats.io>

---------

Signed-off-by: Derek Collison <derek@nats.io>
Signed-off-by: Neil Twigg <neil@nats.io>
Co-authored-by: Neil Twigg <neil@nats.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants