Skip to content

perf(relay): avoid allocating String in MessageClass::from_frame - #1395

Open
tirthraj01 wants to merge 1 commit into
cameri:rewrite/mainfrom
tirthraj01:fix/1385-from-frame-no-alloc
Open

tirthraj01 wants to merge 1 commit into
cameri:rewrite/mainfrom
tirthraj01:fix/1385-from-frame-no-alloc

Conversation

@tirthraj01

@tirthraj01 tirthraj01 commented Dec 5, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1385

Summary

  • Replace the heap-allocated String in MessageClass::from_frame (crates/nostream-relay/src/limits.rs) with a stack [u8; 8] buffer and byte slice match (&word[..len]), keeping all existing parser behavior identical.
  • Add a_frame_whose_prefix_is_not_ascii_is_classified_without_panicking in crates/nostream-relay/tests/limits.rs.

Measured Effect

  • Before: 1 heap allocation (8 bytes) per classified inbound frame (7 allocations / 56 bytes across the 7 valid frame classes measured via GlobalAlloc).
  • After: 0 allocations (0 bytes) per frame (1 allocation per inbound frame removed).

Verification

  • cargo fmt --all --check
  • cargo clippy -p nostream-relay --all-targets --all-features -- -D warnings
  • cargo clippy -p nostream-relay --lib --bins --all-features -- -D warnings -D clippy::unwrap_used -D clippy::expect_used -D clippy::todo -D clippy::unimplemented -D clippy::panic
  • cargo test -p nostream-relay --lib --test limits --all-features (30 unit tests + 35 limits tests passed)
  • cargo test -p nostream-relay --test handlers an_event_burst_leaves_the_req_window_intact --all-features (1 passed)

Summary by CodeRabbit

  • Performance
    • Reduced temporary memory allocation when classifying incoming messages, improving efficiency during frame processing.
  • Bug Fixes
    • Improved handling of non-ASCII text in message prefixes so malformed input is classified safely without causing a panic.

@changeset-bot

changeset-bot Bot commented Dec 5, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e78db58

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

MessageClass::from_frame now classifies frame prefixes without creating a String. A regression test covers non-ASCII characters in and around the prefix.

Changes: Frame classification

Layer / File(s) Summary
Byte-based frame classification
crates/nostream-relay/src/limits.rs, crates/nostream-relay/tests/limits.rs, .changeset/from-frame-no-alloc.md
MessageClass::from_frame uses a fixed eight-byte buffer and byte-slice matching. Tests cover non-ASCII input within and after the prefix. The changeset records the allocation change.

Suggested reviewers: chappie-daemon, greptile-apps

Change: Refactor

Merge Risk: Minimal · up to e78db

This change classifies inbound frame prefixes using a fixed byte buffer instead of allocating a string per frame. Classification behavior is unchanged for the cases checked, so no user-visible effect is expected and the change looks safe to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: MessageClass::from_frame no longer allocates a String per frame. The change swaps the heap String for a fixed [u8; 8] buffer with byte-slice matching. The title uses t…
Linked Issues check Passed [#1385] The requirement is to derive the frame class without allocating, keep existing parser cases unchanged, and add one non-ASCII prefix case. At head, MessageClass::from_frame in `crates/nostrea…
Out of Scope Changes check Passed The whole-PR changes (merge base to head) consist of: a changeset entry for the performance change, the MessageClass::from_frame rewrite in limits.rs, and one added test in tests/limits.rs. Each…
Full details: Title check

Explanation

The title names the main change: MessageClass::from_frame no longer allocates a String per frame. The change swaps the heap String for a fixed [u8; 8] buffer with byte-slice matching. The title uses the conventional perf(relay) prefix and stays within one sentence of six words. The title matches the changeset and the linked issue's goal of removing one allocation per inbound frame.

Full details: Linked Issues check

Explanation

[#1385] The requirement is to derive the frame class without allocating, keep existing parser cases unchanged, and add one non-ASCII prefix case. At head, MessageClass::from_frame in crates/nostream-relay/src/limits.rs uses a stack let mut word = [0u8; 8] with a len counter and matches &word[..len] against the eight byte-string tokens. The len < word.len() guard keeps the 8-character bound. Non-ASCII characters fail is_ascii_alphabetic() and return Other, so no panic path exists in the word loop. The 32-byte prefix scan and char-boundary handling are unchanged. The PR description and the change summary report a new test, a_frame_whose_prefix_is_not_ascii_is_classified_without_panicking, in crates/nostream-relay/tests/limits.rs. The test file at head was truncated in my read, so I confirmed the test's presence only from the change summary and PR description, not from its source. The reported before/after measurement (1 allocation per frame removed, via a harness) satisfies the issue's measurement requirement. Parser behavior equivalence to the prior String version rests on the PR's claim and was not verified by a base-to-head diff, because the diff tool was unavailable.

Full details: Out of Scope Changes check

Explanation

The whole-PR changes (merge base to head) consist of: a changeset entry for the performance change, the MessageClass::from_frame rewrite in limits.rs, and one added test in tests/limits.rs. Each change serves #1385 directly. The changeset documents the same performance change. No unrelated functional, API, or configuration change is reported in the change summary. Diffs were unavailable for a line-by-line check, so this verdict relies on the change summary and the head file contents.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 4c4f6a6 and e78db58.

Files selected for processing: 3

  • .changeset/from-frame-no-alloc.md
  • crates/nostream-relay/src/limits.rs
  • crates/nostream-relay/tests/limits.rs

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@greptile-apps

greptile-apps Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5RetriggerView in Greptile

[Low impact] The PR appears safe to merge.

Summary

MessageClass::from_frame replaces its temporary String with an eight-byte stack buffer.

  • The relay reads inbound message classes without allocating a string.

Reviews (1) · Last reviewed commit: "perf(relay): avoid allocating String in ..." · Reviewed by Greptile

This branch has not been deployed

No deployments
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