Skip to content

Reuse Zstd decompression resources across frames - #2

Open
Lyptik wants to merge 5 commits into
mainfrom
perf/reuse-zstd-decompression-resources
Open

Lyptik wants to merge 5 commits into
mainfrom
perf/reuse-zstd-decompression-resources

Conversation

@Lyptik

@Lyptik Lyptik commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Why

Compressed tracking data currently calls new Decompressor().Unwrap(...) for every frame.

ZstdNet.Decompressor owns a native ZSTD_DCtx and implements IDisposable. Creating one per frame without disposing it immediately pushes native cleanup onto the finalizer thread. At 30–60 Hz, especially with point clouds, this can create significant native-memory pressure and GC/finalizer churn.

The current Unwrap overload also allocates a new decompressed byte[] for every compressed frame.

Changes

  • keep one reusable Zstd decompression context per Augmenta client
  • keep one reusable managed decompression buffer per client
  • grow the buffer only when a larger decompressed frame arrives
  • use the exact valid span of the reusable buffer for packet parsing
  • release the native decompression context on Shutdown()
  • make Client<TVector3> implement IDisposable for clients discarded without an explicit shutdown
  • document the resource lifetime in the README
  • add a lightweight PR build workflow so this and future SDK changes compile before merge

Memory behavior

Before:

compressed frame
→ new Decompressor / native ZSTD_DCtx
→ new decompressed byte[]
→ parse
→ native context waits for Dispose/finalizer

After:

Client
→ one reusable ZSTD_DCtx
→ one high-water reusable byte[]
→ parse each frame from the valid span

Memory is therefore bounded by the client's largest decompressed frame rather than by allocation/finalizer lag.

Compatibility

  • no protocol behavior change
  • compression default remains enabled
  • polling default remains disabled
  • public parsing/event semantics are unchanged
  • Shutdown() remains reusable: a later compressed session lazily creates a fresh decompression context

@Lyptik
Lyptik marked this pull request as ready for review October 6, 2026 07:28
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