Skip to content

perf: optimize shared-kernel-rs utilities#12

Open
Magizhan wants to merge 1 commit into
mainfrom
feat/reliability-performance-audit-fixes
Open

perf: optimize shared-kernel-rs utilities#12
Magizhan wants to merge 1 commit into
mainfrom
feat/reliability-performance-audit-fixes

Conversation

@Magizhan
Copy link
Copy Markdown

@Magizhan Magizhan commented Mar 16, 2026

Problem

The shared-kernel-rs library had several performance inefficiencies and code quality issues: unnecessary string allocations in error handling, uncompiled regex patterns evaluated on every call, redundant format!() wrappers, deprecated AWS SDK usage, and missing inline hints on hot-path utility functions. Additionally, there were no performance benchmarks to catch regressions.

Solution

Performance Optimizations

  • Lazy-compiled UUID regex via once_cell::sync::Lazy in middleware utils (eliminates re-compilation per request)
  • &'static str for error codescode() returns static strings instead of allocating String on every error
  • #[inline] hints on hot-path functions: get_method(), get_headers(), Deref impl, error helpers
  • Reduced allocations.first() instead of .get(0), to_owned() over to_string() for &str, direct string clones instead of format wrappers
  • #[serde(skip_serializing_if)] to avoid serializing empty optional fields

Code Quality

  • Updated deprecated AWS SDKaws_config::from_env()aws_config::defaults(BehaviorVersion::latest())
  • Removed unused imports (log::info, tracing::*, UnboundedReceiver, UnboundedSender)
  • Proc macro improvementmeasure_duration now preserves function attributes (#[allow(...)], #[cfg(...)])
  • Extracted ErrorHandler<E> type alias for readability in callapi module
  • Middleware refactor — extracts headers/method/path into locals before passing by reference

Benchmarks

  • Added criterion benchmarks for Redis serialization patterns, serde serialization/deserialization, and middleware path normalization/URL decoding/error formatting

Testing

  • cargo build passes
  • cargo clippy clean
  • cargo test passes
  • Run benchmarks: cargo bench
  • Verify downstream services compile against updated shared-kernel

Impact

  • 17 files changed (4 new benchmark files, 13 modified source files)
  • Risk: LOW-MEDIUM — this is a shared library; changes affect all downstream Rust services
  • Key hot-path improvements: regex compilation (once vs per-request), error code allocation elimination, inline hints
  • New benchmark suite enables future performance regression detection

- Optimize Redis command implementations
- Improve middleware request handling
- Add performance benchmarks
- Fix clippy warnings
- cargo fmt clean

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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