Skip to content

Avoid repeated decoding and byte boxing in ResultChunkAssembler #54

@nficano

Description

@nficano

ResultChunkAssembler.accept decodes every buffered chunk when checking the assembled size and then decodes every chunk again when building the final byte array. The size pass at lib/src/main/kotlin/dev/arcp/client/ResultChunkAssembler.kt:20 calls decodedBytes for each chunk, and the final assembly at lib/src/main/kotlin/dev/arcp/client/ResultChunkAssembler.kt:26 calls decodedBytes again, converts each ByteArray to an Iterable<Byte>, flattens into boxed Byte values, and finally converts back to a primitive ByteArray. This is avoidable allocation and CPU work on the hot path for large streamed results, especially near the 64 MiB default limit.

Fix prompt: Store decoded bytes or per-chunk decoded sizes as chunks arrive so each chunk is decoded once. When the terminal chunk arrives, allocate a single output ByteArray of the known total size and copy each decoded chunk into it, or use ByteArrayOutputStream with the expected size. Keep the existing max-size guard, preserve mixed-encoding and monotonic-sequence validation, and add a regression test that confirms large base64 chunks are assembled without changing semantics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or allocation improvementseverity:lowLow severity issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions