Skip to content

Rust perf: cache LazyLock pricing table; hoist regex statics #344

@willwashburn

Description

@willwashburn

Context

Several allocate-per-call hotspots that turn into LazyLock<T> one-liners.

1. crates/relayburn-sdk/src/analyze/pricing.rs:94-96 load_builtin_pricing() re-parses the embedded models.dev JSON snapshot every call. It's invoked from at least 5 public verbs in query_verbs.rs. Wrap in static BUILTIN: LazyLock<PricingTable> and .clone() once (or return Arc<PricingTable>).

2. Per-call regex compilation:

  • crates/relayburn-sdk/src/analyze/ghost_surface.rs:298-302 claude_command_name_re()
  • crates/relayburn-sdk/src/analyze/claude_md.rs:249-250 open_re, heading_re inside find_headings (called per overhead file)
  • crates/relayburn-sdk/src/reader/classifier.rs:680-682 and :725-726 declare the same static RE: LazyLock<Regex> for ^[A-Za-z_][A-Za-z0-9_]*= — two cells for one pattern; lift to module scope.

provider_reattribution.rs:DEFAULT_RULES_INNER already uses LazyLock; this is the same trick applied consistently.

3. crates/relayburn-sdk/src/analyze/cost.rs:126-149 sum_costs allocates model: "aggregate".to_string() for every call regardless of input length. Make model: &'static str (or Cow<'static, str> if a callsite needs ownership).

Proposed fix

Mechanical: wrap each in LazyLock::new(...) (already an MSRV-stable feature with the toolchain pinned at 1.94). Verify the conformance gate stays green.

References

  • crates/relayburn-sdk/src/analyze/pricing.rs:94-96
  • crates/relayburn-sdk/src/analyze/ghost_surface.rs:298-302
  • crates/relayburn-sdk/src/analyze/claude_md.rs:249-250
  • crates/relayburn-sdk/src/reader/classifier.rs:680-682, 725-726
  • crates/relayburn-sdk/src/analyze/cost.rs:126-149
  • Analyze + reader review notes from the May 2026 Rust review.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions