Skip to content

Conversation

@anthony-swirldslabs
Copy link
Contributor

Description:
This is a part of an original proof of concept PR at #612 .

Introducing an ability to write protobuf data to pre-existing byte arrays. This allows client code to reuse the same byte array for multiple write operations and avoid allocating new arrays or buffers for this purpose. This also enables certain optimizations, such as a more efficient writing of varint data types where the write operation can start directly from a byte at a larger index, and then descend gradually writing to lesser significant bytes.

In the new ProtoArrayWriterTools.java, the current PR already implements the optimized version of varint writing. There'll be a follow-up PR focusing on varints specifically that would add benchmarks, but they are outside of scope of this particular PR.

Related issue(s):

Fixes #613

Notes for reviewer:
All tests should pass. This is a new API, so no existing code is affected.

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)

Signed-off-by: Anthony Petrov <anthony@swirldslabs.com>
@github-actions
Copy link

github-actions bot commented Sep 16, 2025

JUnit Test Report

   77 files  ±0     77 suites  ±0   3m 30s ⏱️ -27s
1 328 tests ±0  1 324 ✅ ±0   4 💤 ±0  0 ❌ ±0 
7 185 runs  ±0  7 165 ✅ ±0  20 💤 ±0  0 ❌ ±0 

Results for commit 47c1b51. ± Comparison against base commit 28f0a82.

This pull request removes 8 and adds 7 tests. Note that renamed tests count towards both.
, 1
com.hedera.pbj.runtime.ProtoWriterToolsTest ‑ [1] FLOAT, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c04479f0@15c3585, [0.1, 0.5, 100.0], 12, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c0447c00@5b86f4cb
com.hedera.pbj.runtime.ProtoWriterToolsTest ‑ [1] STRING, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c0454a60@912a644, [string 1, testing here, testing there], com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c0454c70@478cc5f1
com.hedera.pbj.runtime.ProtoWriterToolsTest ‑ [2] BYTES, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c0454e80@37d3e740, [010203, ff7f0f, 42da07370bff], com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c0455090@5afba80c
com.hedera.pbj.runtime.ProtoWriterToolsTest ‑ [2] DOUBLE, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c044c000@630e5010, [0.1, 0.5, 100.0, 1.7653472635472653E240], 32, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c044c210@10b5ff4d
com.hedera.pbj.runtime.ProtoWriterToolsTest ‑ [3] BOOL, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c044c420@222eda8a, [true, false, false, true, true, true], 6, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c044c630@3331800f
com.hedera.pbj.runtime.ProtoWriterToolsTest ‑ [4] ENUM, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c044c840@4edd8a0a, [Mock for EnumWithProtoMetadata, hashCode: 1678404939, Mock for EnumWithProtoMetadata, hashCode: 1161255903, Mock for EnumWithProtoMetadata, hashCode: 1049618395], 3, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c044ca50@1701beb3
com.hedera.pbj.runtime.Utf8ToolsTest ‑ [4] 
com.hedera.pbj.runtime.ProtoWriterToolsTest ‑ [1] FLOAT, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c044c638@48da5106, [0.1, 0.5, 100.0], 12, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c044c848@31cddb11
com.hedera.pbj.runtime.ProtoWriterToolsTest ‑ [1] STRING, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c04556c0@747f0f34, [string 1, testing here, testing there], com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c04558d0@556ae220
com.hedera.pbj.runtime.ProtoWriterToolsTest ‑ [2] BYTES, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c0455ae0@638341b0, [010203, ff7f0f, 42da07370bff], com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c0455cf0@7962c1d5
com.hedera.pbj.runtime.ProtoWriterToolsTest ‑ [2] DOUBLE, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c044ca58@166a5659, [0.1, 0.5, 100.0, 1.7653472635472653E240], 32, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c044cc68@1afabf06
com.hedera.pbj.runtime.ProtoWriterToolsTest ‑ [3] BOOL, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c044ce78@443b6765, [true, false, false, true, true, true], 6, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c044d088@a72925
com.hedera.pbj.runtime.ProtoWriterToolsTest ‑ [4] ENUM, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c044d298@4c6eaa65, [Mock for EnumWithProtoMetadata, hashCode: 731942694, Mock for EnumWithProtoMetadata, hashCode: 263466012, Mock for EnumWithProtoMetadata, hashCode: 1323163740], 3, com.hedera.pbj.runtime.ProtoWriterToolsTest$$Lambda/0x00000007c044d4a8@6b170692
com.hedera.pbj.runtime.Utf8ToolsTest ‑ [4] 
, 1

♻️ This comment has been updated with latest results.

@github-actions
Copy link

github-actions bot commented Sep 16, 2025

Integration Test Report

    405 files  +1      405 suites  +1   18m 2s ⏱️ +51s
114 836 tests +1  114 836 ✅ +1  0 💤 ±0  0 ❌ ±0 
115 077 runs  +1  115 077 ✅ +1  0 💤 ±0  0 ❌ ±0 

Results for commit 47c1b51. ± Comparison against base commit 28f0a82.

This pull request removes 3 and adds 4 tests. Note that renamed tests count towards both.
com.hedera.pbj.integration.test.ParserNeverWrapsTest ‑ [1] com.hedera.pbj.integration.test.ParserNeverWrapsTest$$Lambda/0x00007fef50b4c218@3a917c9e
com.hedera.pbj.integration.test.ParserNeverWrapsTest ‑ [2] com.hedera.pbj.integration.test.ParserNeverWrapsTest$$Lambda/0x00007fef50b4c448@48ec0070
com.hedera.pbj.integration.test.ParserNeverWrapsTest ‑ [3] com.hedera.pbj.integration.test.ParserNeverWrapsTest$$Lambda/0x00007fef50b4c678@579c0f39
com.hedera.pbj.integration.test.CustomSourceSetTest ‑ testPbjCodeFromCustomSourceSet()
com.hedera.pbj.integration.test.ParserNeverWrapsTest ‑ [1] com.hedera.pbj.integration.test.ParserNeverWrapsTest$$Lambda/0x00007f1c68b0bad0@7ce1ba3d
com.hedera.pbj.integration.test.ParserNeverWrapsTest ‑ [2] com.hedera.pbj.integration.test.ParserNeverWrapsTest$$Lambda/0x00007f1c68b0bd00@209f3f4f
com.hedera.pbj.integration.test.ParserNeverWrapsTest ‑ [3] com.hedera.pbj.integration.test.ParserNeverWrapsTest$$Lambda/0x00007f1c68b0a800@5648111c

♻️ This comment has been updated with latest results.

Signed-off-by: Anthony Petrov <anthony@swirldslabs.com>
Signed-off-by: Anthony Petrov <anthony@swirldslabs.com>
imalygin
imalygin previously approved these changes Sep 17, 2025
Signed-off-by: Anthony Petrov <anthony@swirldslabs.com>
@anthony-swirldslabs anthony-swirldslabs merged commit fd93a3b into main Sep 17, 2025
12 checks passed
@anthony-swirldslabs anthony-swirldslabs deleted the 613-writeToByteArray branch September 17, 2025 22:48
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.

Introduce writeTo(byte[])

4 participants