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

Optimise filestore writeFullState for high subject cardinality #5004

Merged
merged 2 commits into from Jan 26, 2024

Conversation

neilalexander
Copy link
Member

This PR makes two changes to writeFullState:

  1. Use Match(">", ...) instead of Iter(...) as it is faster and results in less heap escapes
  2. Don't allocate a 4K buffer on the stack if it's obvious that we won't use it

Signed-off-by: Neil Twigg neil@nats.io

… on stack if we won't use it

Signed-off-by: Neil Twigg <neil@nats.io>
@neilalexander neilalexander requested a review from a team as a code owner January 26, 2024 10:10
Copy link
Member

@derekcollison derekcollison left a comment

Choose a reason for hiding this comment

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

LGTM - One nit

if sz <= cap(raw) {
buf, sz = raw[0:2:cap(raw)], cap(raw)
if sz <= ssz {
var _buf [ssz]byte
Copy link
Member

Choose a reason for hiding this comment

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

This seems like it would make runtime work since you are betting it does not grow stack unless on conditional but it would then need to escape the block properly. Being only 4k I think its fine on main stack, I like the make branch.

Copy link
Member Author

Choose a reason for hiding this comment

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

I can put it back to being unconditional, but I did check it with escape analysis and the escape paths are the same (either via .Seal() or a realloc with append), regardless of whether it's inside the conditional block or not. Just seems like a little way to save the allocation from happening at all if we know from the get-go that we're over 4K.

Copy link
Member

Choose a reason for hiding this comment

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

ok if you checked.. Hopefully makes sense though that the runtime seems like it would need to do more work to not decrease stack leaving that block since access to that escapes the block but not the func(). Stack growth is free unless it splits the stack being very big.

@derekcollison derekcollison merged commit b85068a into main Jan 26, 2024
4 checks passed
@derekcollison derekcollison deleted the neil/writefullstate branch January 26, 2024 15:11
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

2 participants