Skip to content

Replace Vec<u8> with Bytes - #4684

Open
MartinquaXD wants to merge 3 commits into
mainfrom
use-bytes-crate
Open

Replace Vec<u8> with Bytes#4684
MartinquaXD wants to merge 3 commits into
mainfrom
use-bytes-crate

Conversation

@MartinquaXD

Copy link
Copy Markdown
Contributor

Description

A lot of data we have to pass around is currently stored in Vec<u8> (e.g. calldata, signatures). Cloning those is very expensive because we always need to do a full clone (fresh allocation + copying all the bytes). This can be avoided by using bytes::Bytes. That's effectively an immutable and reference counted Vec<u8> which makes clones just an atomic pointer increment.

Changes

replaced Vec<u8> with bytes::Bytes almost everywhere. Exceptions are:

  • auto generated code - obviously
  • database code (weirdly sqlx doesn't support that type)
  • where external crate API don't allow the usage of Bytes
  • solana

How to test

all tests should still pass
performance impact can be measured by temporarily deploying to prod

@MartinquaXD
MartinquaXD requested a review from a team as a code owner July 30, 2026 09:03
@MartinquaXD

Copy link
Copy Markdown
Contributor Author

@claude do your thing

@claude

claude Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Claude finished @MartinquaXD's task in 4m 20s —— View job


PR Review: Replace Vec<u8> with Bytes

  • Read the diff against origin/main
  • Analyze changes for correctness & performance
  • Post inline comments for issues found
  • Post review summary

This is a clean, mechanical migration and I don't see any correctness regressions. A few things I specifically checked that hold up well:

  • Wire format is preserved. Every migrated field keeps its #[serde(with = "bytes_hex")] / serde_ext::Hex adapter, so serialization stays hex-encoded and doesn't silently fall back to bytes' native byte-sequence serde impl.
  • Bytes::from_static(&[...]) uses in tests all point at literal arrays / byte-strings, so const promotion to 'static is valid — no runtime data passed to from_static.
  • .calldata().clone().0 pattern (alloy Bytes clone → inner bytes::Bytes) is genuinely zero-copy, nice.
  • DB / solana / external-API exceptions are consistent with what the description calls out (.to_vec() only at the sqlx boundary).

One optional perf nit posted inline on crates/simulator/src/encoding.rs:111BytesMut::from_iter(Vec<u8>) copies element-by-element instead of memcpy; suggested extend_from_slice.

Nothing blocking from my side.
• branch use-bytes-crate

Comment thread crates/simulator/src/encoding.rs Outdated
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