Skip to content

Ascii85 Improvements - #86

Open
tehtelev wants to merge 1 commit into
anegostudios:masterfrom
tehtelev:patch-12
Open

Ascii85 Improvements#86
tehtelev wants to merge 1 commit into
anegostudios:masterfrom
tehtelev:patch-12

Conversation

@tehtelev

@tehtelev tehtelev commented Jul 9, 2026

Copy link
Copy Markdown

Summary

  • Replace StringBuilder with string.Create: Accurate length calculation and direct writing via Span completely eliminate heap allocations and unnecessary memory copies during encoding.
  • Replace MemoryStream with a byte[] array: Direct writing by index (byteIdx) eliminates stream overhead and dynamic expansion of internal buffers during decoding.
  • Switch from char[] arrays to stackalloc: Temporary buffers are allocated on the stack, which is instantaneous and does not burden the garbage collector (GC).
  • Move stackalloc outside the loop: Reusing a single stack buffer prevents unnecessary stack pointer shifts on each iteration.
  • Add static to lambda: The modifier disables closure capture of external variables, preventing hidden heap allocations of closure classes during method invocation.
  • Method inlining (EncodeValue, DecodeValue): Inlining logic directly into loops reduces method call overhead and improves the JIT compiler's optimization capabilities.
  • Tighter overflow checking (checked): Fixing implicit arithmetic and adding a try/catch to the padding block ensures that invalid data will cause a FormatException rather than silently corrupting the result due to uint overflow.

Also sent to Stratum

Performance numbers

Tested on the generation of 10200 chunk columns:

  • Total GC memory allocations: reduced from 733 MB to 157 MB.

Before
ascii before mem

After
ascii after mem

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