Skip to content

refactor: drive ZstdStreamBuffer offsets from a MemoryLayout#60

Merged
dfa1 merged 1 commit into
mainfrom
refactor/streambuffer-memorylayout
Jun 28, 2026
Merged

refactor: drive ZstdStreamBuffer offsets from a MemoryLayout#60
dfa1 merged 1 commit into
mainfrom
refactor/streambuffer-memorylayout

Conversation

@dfa1

@dfa1 dfa1 commented Jun 28, 2026

Copy link
Copy Markdown
Owner

What

ZstdStreamBuffer backs the native ZSTD_inBuffer / ZSTD_outBuffer structs ({ void* ptr; size_t size; size_t pos; }). It previously hardcoded the struct shape with magic numbers:

static final long BYTES = 24;
private static final long OFF_PTR = 0;
private static final long OFF_SIZE = 8;
private static final long OFF_POS = 16;

This replaces those with a proper named StructLayout and derives everything from it:

  • BYTES now comes from LAYOUT.byteSize() (same name/type/visibility, still package-accessible).
  • Fields are accessed through VarHandles obtained from the layout (ADDRESS for ptr, JAVA_LONG for size/pos), passing base offset 0L.
  • The allocation uses the layout directly (arena.allocate(LAYOUT)).

Why

Self-documenting field names instead of byte offsets, and the offsets/size can no longer drift out of sync with the declared layout.

Behavior

Behavior-preserving. The public/package surface is unchanged (ZstdStreamBuffer(Arena), segment(), set(MemorySegment, long), size(), pos()), and set still zeroes pos.

Verification

  • ./mvnw -pl zstd test — 261 tests, all green (streaming paths ZstdSegmentStreamTest / ZstdStreamTest exercise this struct).
  • ./mvnw javadoc:javadoc -pl zstd — zero output.
  • ./mvnw -pl zstd validate (checkstyle) — clean.

🤖 Generated with Claude Code

Replace the hardcoded ZSTD_inBuffer/ZSTD_outBuffer struct layout (magic
offsets OFF_PTR/OFF_SIZE/OFF_POS and BYTES = 24) with a named StructLayout
and derive the total size, field handles, and offsets from it. Field access
now goes through VarHandles obtained from the layout. Behavior-preserving.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@dfa1 dfa1 merged commit 29a8e8a into main Jun 28, 2026
1 check passed
@dfa1 dfa1 deleted the refactor/streambuffer-memorylayout branch June 28, 2026 16: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