Skip to content

[perf] Optimize enclave with async I/O, parallel decryption, and HashMap#259

Merged
jplock merged 4 commits intoaws-samples:mainfrom
jplock:claude/optimize-enclave-performance-uxuDb
Dec 24, 2025
Merged

[perf] Optimize enclave with async I/O, parallel decryption, and HashMap#259
jplock merged 4 commits intoaws-samples:mainfrom
jplock:claude/optimize-enclave-performance-uxuDb

Conversation

@jplock
Copy link
Copy Markdown
Contributor

@jplock jplock commented Dec 23, 2025

  • Switch from BTreeMap to HashMap for O(1) field lookups
  • Add rayon for parallel HPKE field decryption across multiple cores
  • Convert to async I/O using tokio and tokio-vsock for concurrent client handling (spawn task per connection)
  • Add async protocol functions (send_message_async, recv_message_async)
  • Pre-allocate HashMap with capacity hints to reduce allocations
  • Use Mutex for thread-safe error collection during parallel decryption

Performance impact:

  • Field decryption scales with CPU cores for multi-field requests
  • Concurrent request handling eliminates head-of-line blocking
  • HashMap provides constant-time field access vs O(log n) for BTreeMap

The code maintains no-panic guarantees with proper error handling throughout all new code paths.

Issue #, if available:

Description of changes:

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

- Switch from BTreeMap to HashMap for O(1) field lookups
- Add rayon for parallel HPKE field decryption across multiple cores
- Convert to async I/O using tokio and tokio-vsock for concurrent
  client handling (spawn task per connection)
- Add async protocol functions (send_message_async, recv_message_async)
- Pre-allocate HashMap with capacity hints to reduce allocations
- Use Mutex for thread-safe error collection during parallel decryption

Performance impact:
- Field decryption scales with CPU cores for multi-field requests
- Concurrent request handling eliminates head-of-line blocking
- HashMap provides constant-time field access vs O(log n) for BTreeMap

The code maintains no-panic guarantees with proper error handling
throughout all new code paths.
Minimize tokio dependency surface area by removing features not used:
- net: tokio-vsock provides its own vsock networking
- sync: No tokio sync primitives used in the codebase

Retained features:
- rt-multi-thread: Required for async runtime and tokio::spawn
- io-util: Required for AsyncReadExt/AsyncWriteExt traits
- macros: Required for #[tokio::main]
@jplock jplock self-assigned this Dec 23, 2025
Reverts the async/tokio approach in favor of simpler synchronous code:
- Removes tokio and tokio-vsock dependencies
- Uses std::thread for concurrent client handling
- Keeps rayon for parallel field decryption
- Keeps HashMap for O(1) lookups
- Reduces external dependency surface area

The enclave typically handles a single parent connection at a time,
making async overhead unnecessary. Rayon still provides parallelization
for CPU-bound field decryption operations.
@jplock jplock marked this pull request as ready for review December 24, 2025 00:05
@jplock jplock requested a review from a team as a code owner December 24, 2025 00:05
@jplock jplock added enhancement New feature or request rust Pull requests that update Rust code labels Dec 24, 2025
@jplock jplock marked this pull request as draft December 24, 2025 00:14
Critical fixes:
- Add connection limiting (MAX_CONCURRENT_CONNECTIONS=32) to prevent DoS
- Create SecureHpkePrivateKey wrapper with ZeroizeOnDrop for key material
- Remove expect() calls in functions.rs, use proper error handling

High severity fixes:
- Fix mutex poisoning: log critical error instead of silently ignoring
- Sanitize error messages in logs to prevent sensitive data leakage
- Gate detailed error logging behind debug builds only

The enclave now:
- Limits concurrent connections to prevent resource exhaustion
- Properly zeroizes HPKE private key material on drop
- Never panics in production code paths
- Logs critical conditions (mutex poisoning) for debugging
@jplock jplock marked this pull request as ready for review December 24, 2025 00:23
@jplock jplock merged commit 70fb3e6 into aws-samples:main Dec 24, 2025
2 checks passed
@jplock jplock deleted the claude/optimize-enclave-performance-uxuDb branch December 24, 2025 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants