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

Commits on Jan 16, 2024

  1. Stree initial 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>
    derekcollison committed Jan 16, 2024
    Configuration menu
    Copy the full SHA
    172fb73 View commit details
    Browse the repository at this point in the history
  2. Swapped out psim as a hashmap for our stree impl.

    Signed-off-by: Derek Collison <derek@nats.io>
    derekcollison committed Jan 16, 2024
    Configuration menu
    Copy the full SHA
    a5290b3 View commit details
    Browse the repository at this point in the history

Commits on Jan 17, 2024

  1. Updates based on PR feedback

    Signed-off-by: Derek Collison <derek@nats.io>
    derekcollison committed Jan 17, 2024
    Configuration menu
    Copy the full SHA
    334206f View commit details
    Browse the repository at this point in the history

Commits on Jan 18, 2024

  1. Remove comment that is no longer applicable

    Signed-off-by: Derek Collison <derek@nats.io>
    derekcollison committed Jan 18, 2024
    Configuration menu
    Copy the full SHA
    c0c41be View commit details
    Browse the repository at this point in the history
  2. stree: Reduce allocations in iter and match

    Signed-off-by: Neil Twigg <neil@nats.io>
    neilalexander committed Jan 18, 2024
    Configuration menu
    Copy the full SHA
    8a0cbc6 View commit details
    Browse the repository at this point in the history
  3. stree: Reduce allocations in iter and match (#4974)

    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>
    derekcollison committed Jan 18, 2024
    Configuration menu
    Copy the full SHA
    cecd5eb View commit details
    Browse the repository at this point in the history

Commits on Jan 19, 2024

  1. stree: Reduce heap escapes in iter

    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>
    neilalexander committed Jan 19, 2024
    Configuration menu
    Copy the full SHA
    f40e99b View commit details
    Browse the repository at this point in the history
  2. stree: Reduce heap escapes in iter (#4977)

    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>
    derekcollison committed Jan 19, 2024
    Configuration menu
    Copy the full SHA
    6de76a4 View commit details
    Browse the repository at this point in the history
  3. Preallocate pre at top level Match/Iter calls

    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>
    neilalexander committed Jan 19, 2024
    Configuration menu
    Copy the full SHA
    5919557 View commit details
    Browse the repository at this point in the history
  4. stree: Preallocate pre at top level Match/Iter calls (#4978)

    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>
    neilalexander committed Jan 19, 2024
    Configuration menu
    Copy the full SHA
    e309831 View commit details
    Browse the repository at this point in the history
  5. Add BenchmarkSubjectTreeMatch

    Signed-off-by: Neil Twigg <neil@nats.io>
    neilalexander committed Jan 19, 2024
    Configuration menu
    Copy the full SHA
    30baeba View commit details
    Browse the repository at this point in the history

Commits on Jan 20, 2024

  1. Iteration across a node's children was slower than expected and creat…

    …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 committed Jan 20, 2024
    Configuration menu
    Copy the full SHA
    90a8897 View commit details
    Browse the repository at this point in the history