Skip to content

perf(codec): fast-path versioned decode by peeking at the first byte#149

Open
Kewe63 wants to merge 1 commit into
circlefin:mainfrom
Kewe63:fix/131-versioned-codec-fast-path
Open

perf(codec): fast-path versioned decode by peeking at the first byte#149
Kewe63 wants to merge 1 commit into
circlefin:mainfrom
Kewe63:fix/131-versioned-codec-fast-path

Conversation

@Kewe63

@Kewe63 Kewe63 commented Jun 14, 2026

Copy link
Copy Markdown

Summary

NetCodec and WalCodec are used on every incoming message in the consensus node. Every message is now prefixed with the V1 version byte (0x01), yet the decoder still attempted a full protobuf parse of the un-versioned payload first — before reading the version byte — on every single message. This work is guaranteed to fail for a V1 message because 0x01 cannot start any real Arc protobuf payload (the smallest protobuf field tag with a non-zero field number is 0x08).


Fix

Added a pre-check on the first byte inside impl_versioned_codec!: if it matches the configured version byte, strip it and decode the remainder as protobuf in a single pass. The legacy "try-raw-protobuf-first" branch is preserved for backward compatibility with messages from pre-versioning nodes.

This is complementary to the Phase 3 cleanup in #132, which removes the legacy branch entirely once all nodes are upgraded. This PR only adds the fast path on top of the existing code — safe to merge before Phase 3, and #132 can rebase cleanly on top.


Changes

File: crates/types/src/codec/ (impl_versioned_codec! macro)

  • Added first-byte pre-check: if byte matches version, strip and decode in a single pass.
  • Legacy branch preserved for pre-versioning node compatibility.

File: crates/types/src/codec/wal.rs

  • Added fast_path_decodes_v1_prefixed_message_in_one_pass test locking in two invariants:
    1. The encoded form of a versioned message starts with the V1 version byte.
    2. Decoding that byte sequence returns the original message.

How to Test

# Codec tests
cargo test -p arc-consensus-types --lib codec::
# ✅ 61 passed, 0 failed

# Format & lint
cargo fmt --check -p arc-consensus-types
cargo clippy -p arc-consensus-types --all-targets -- -D warnings
# ✅ Both clean

The legacy branch is exercised by the existing test_previous_codec_compatibility tests in wal.rs and network.rs — any regression there surfaces in those tests, not in the new one.


Risk & Impact

Low. The fast path is additive — the legacy branch is fully preserved and exercised by existing tests. No behavioral change for pre-versioning messages. V1 messages now skip a guaranteed-to-fail protobuf parse on every decode.

Type: ⚡ Performance improvement
Closes: #131

NetCodec and WalCodec are used by the consensus node on every
incoming message, and every message they ship is now prefixed with
the V1 version byte (0x01). The decoder still attempted a full
protobuf parse of the un-versioned payload first before reading
the version byte, on every single message — work that is
guaranteed to fail for a V1 message because 0x01 is an invalid
leading byte for any real Arc protobuf payload (the smallest
field tag with a non-zero field number is 0x08).

Add a pre-check on the first byte: if it matches the configured
version, strip the byte and decode the rest as protobuf in a
single pass. The legacy 'try-raw-protobuf-first' branch is
preserved for backward compatibility with messages from
pre-versioning nodes — that branch is the only one exercised by
the existing test_previous_codec_compatibility tests in wal.rs
and network.rs.

Note this is complementary to the Phase 3 cleanup in circlefin#132: that
PR removes the legacy branch entirely once all nodes are
upgraded; this one only adds the fast path on top of the
existing code, so it is safe to merge before Phase 3.

Closes circlefin#131
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