Skip to content

feat: Adding PbjWriter - #909

Open
ldintr wants to merge 1 commit into
ldintr-addReaderfrom
ldintr-addWriter
Open

feat: Adding PbjWriter#909
ldintr wants to merge 1 commit into
ldintr-addReaderfrom
ldintr-addWriter

Conversation

@ldintr

@ldintr ldintr commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Write-side counterpart to PbjReader: fixed-width/varint(+zigzag) writes, a placeholder/patch mechanism (placehold/writeAtUnsafe/reinsertVarInt) for filling in length prefixes after writing content (avoiding a two-pass encode for short strings), constructible from OutputStream/ByteBuffer/growable buffers/WritableSequentialData, with toByteArray()/toPbjReader() round-tripping. Makes sizeOfStringNoTag public for the large-string fallback.

Overview: PbjReader/PbjWriter are a leaner, buffer-reusing alternative to the existing ReadableSequentialData/WritableSequentialData read/write path used by Codec, ProtoParserTools/ProtoWriterTools, and the pbj-compiler generators. They reuse an internal ~16KB (L1-cache-sized) buffer across many parse/write calls instead of allocating per call, avoid checked-exception-based control flow by recording a sticky internal error code instead, and split hot-path operations (e.g. zigzag vs. non-zigzag varints, direct UTF-8 decode into a reusable char[]) into dedicated fast methods.

@ldintr ldintr self-assigned this Aug 31, 2026
@ldintr
ldintr requested review from a team as code owners August 31, 2026 16:39
@github-actions

Copy link
Copy Markdown

JUnit Test Report

   521 files  ±0     521 suites  ±0   28s ⏱️ ±0s
 1 537 tests ±0   1 533 ✅ ±0   4 💤 ±0  0 ❌ ±0 
10 755 runs  ±0  10 727 ✅ ±0  28 💤 ±0  0 ❌ ±0 

Results for commit 219ade1. ± Comparison against base commit d2ae85b.

@github-actions

Copy link
Copy Markdown

Integration Test Report

    428 files  ±0      428 suites  ±0   21m 8s ⏱️ +47s
115 048 tests ±0  115 048 ✅ ±0  0 💤 ±0  0 ❌ ±0 
115 292 runs  ±0  115 292 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit 219ade1. ± Comparison against base commit d2ae85b.

@ldintr ldintr changed the title Adding PbjWriter feat: Adding PbjWriter Aug 31, 2026
Comment on lines +511 to +530
public void writeIntLE(int value) {
if (pos + 4 <= cap) {
buf[pos] = (byte) value;
buf[pos + 1] = (byte) (value >>> 8);
buf[pos + 2] = (byte) (value >>> 16);
buf[pos + 3] = (byte) (value >>> 24);
pos += 4;
return;
}
writeIntLEInternal(value);
}

private void writeIntLEInternal(int value) {
flushOrGrow(4);
buf[pos] = (byte) value;
buf[pos + 1] = (byte) (value >>> 8);
buf[pos + 2] = (byte) (value >>> 16);
buf[pos + 3] = (byte) (value >>> 24);
pos += 4;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't see where writeIntLEInternal() is used other than in writeIntLE() so why not just have:

    public void writeIntLE(int value) {
        if (pos + 4 > cap) flushOrGrow(4);
        buf[pos] = (byte) value;
        buf[pos + 1] = (byte) (value >>> 8);
        buf[pos + 2] = (byte) (value >>> 16);
        buf[pos + 3] = (byte) (value >>> 24);
        pos += 4;
    }

?

Signed-off-by: ldintr <levo.d@swirldslabs.com>
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.

2 participants