Skip to content

fix(serializing): reuse larger pooled writers#1054

Open
sqdbruh wants to merge 1 commit into
FirstGearGames:mainfrom
sqdbruh:fix/writerpool-length-bucket-reuse
Open

fix(serializing): reuse larger pooled writers#1054
sqdbruh wants to merge 1 commit into
FirstGearGames:mainfrom
sqdbruh:fix/writerpool-length-bucket-reuse

Conversation

@sqdbruh

@sqdbruh sqdbruh commented Jun 25, 2026

Copy link
Copy Markdown

Summary

This changes WriterPool.Retrieve(length) so it can reuse a writer from the closest larger length bucket when the exact bucket is empty.

The exact-bucket path stays the same. The fallback only runs when there is no writer available for the requested bucket, and empty buckets are removed after popping.

Background

I ran into this on a long-running Minecraft-like server with chunk streaming and large, variable payload sizes. Each player reconnect caused FishNet to retain some memory even after all player-owned resources were cleaned up. After enough reconnects, memory usage kept climbing until the server eventually ran out of RAM.

The issue is that a writer can be returned to the length pool under a bucket based on its actual capacity, which may be larger than the bucket requested later. Since Retrieve(length) only checked the exact bucket, larger cached writers could sit in _lengthPool and remain strongly referenced instead of being reused.

Reusing the nearest larger bucket fixed the retained allocation growth in that workload and kept memory usage stable across repeated reconnects.

Length-pooled writers can be stored under a larger capacity bucket than the requested length bucket. Reusing the nearest larger bucket prevents pooled writers from staying rooted in the static length pool under variable payload sizes.
@sqdbruh sqdbruh marked this pull request as ready for review June 25, 2026 12:29
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.

1 participant