Overview
While OpenTDFKit meets current functional requirements, several hot paths generate avoidable overhead. This issue captures the prioritized improvements and the acceptance criteria for completing the optimization work.
Goals
- Reduce unnecessary allocations and actor hops in NanoTDF creation/decryption flows.
- Accelerate key-generation code paths that are currently serialized despite asynchronous scaffolding.
- Minimize redundant HKDF and AES-GCM operations inside the KAS rewrap logic.
- Preserve functional parity and spec compliance while improving throughput.
Scope
1. CryptoHelper Lifetime Management
Current Pain Point
NanoTDF.getPayloadPlaintext and createNanoTDF instantiate CryptoHelper() per call, despite the helper being stateless. Actor creation and cross-actor await introduce latency.
Acceptance Criteria
2. KeyStore Batch Generation Concurrency
Current Pain Point
KeyStore.generateAndStoreKeyPairs uses withThrowingTaskGroup but each task immediately awaits self.generateKeyPair(), serializing the work.
Acceptance Criteria
3. BinaryParser Zero-Copy Reads
Current Pain Point
BinaryParser.read uses Data.subdata(in:), creating a copy on every header field.
Acceptance Criteria
4. KASService Rewrap Version Hinting
Current Pain Point
KASService.rewrapKeyInternal always derives both v12 and v13 HKDF outputs and retries AES-GCM decryption on failure.
Acceptance Criteria
Implementation Guidance
- Start by writing or updating benchmarks to capture baseline numbers before code changes.
swift test --filter "BenchmarkTests" should run cleanly.
- Optimize in small, reviewable increments; avoid combining multiple concerns in a single PR.
- Use Instruments (Time Profiler, Allocations) or Swift’s
signpost APIs to validate real-world improvements when available.
- Maintain comprehensive unit test coverage. Add regression tests whenever optimizing parsing or cryptographic flows.
- Document any public API adjustments in
Docs/TDF.md and update changelog entries if external behavior changes.
- Run
swiftformat --swiftversion 6.2 . and swift test prior to submission. Include performance numbers in the PR description.
Completion Definition
- All acceptance criteria above are satisfied and validated by tests/benchmarks.
- Documentation reflects the changes and their rationale.
- No observed regressions in existing benchmark suites or functional tests.
Overview
While OpenTDFKit meets current functional requirements, several hot paths generate avoidable overhead. This issue captures the prioritized improvements and the acceptance criteria for completing the optimization work.
Goals
Scope
1. CryptoHelper Lifetime Management
Current Pain Point
NanoTDF.getPayloadPlaintextandcreateNanoTDFinstantiateCryptoHelper()per call, despite the helper being stateless. Actor creation and cross-actorawaitintroduce latency.Acceptance Criteria
awaithops from CryptoHelper convenience methods.NanoTDFBenchmarkTestsshow measurable improvement (>10% reduction in per-op runtime on M1 Max reference numbers).2. KeyStore Batch Generation Concurrency
Current Pain Point
KeyStore.generateAndStoreKeyPairsuseswithThrowingTaskGroupbut each task immediately awaitsself.generateKeyPair(), serializing the work.Acceptance Criteria
keyPairs.KeyStoreBenchmarkTests) to confirm throughput regression does not occur; document improved ops/sec if observed.3. BinaryParser Zero-Copy Reads
Current Pain Point
BinaryParser.readusesData.subdata(in:), creating a copy on every header field.Acceptance Criteria
data[cursor..<cursor + length]or usewithUnsafeBytesto avoid allocations.mallocstats or Instruments snapshots).4. KASService Rewrap Version Hinting
Current Pain Point
KASService.rewrapKeyInternalalways derives both v12 and v13 HKDF outputs and retries AES-GCM decryption on failure.Acceptance Criteria
Implementation Guidance
swift test --filter "BenchmarkTests"should run cleanly.signpostAPIs to validate real-world improvements when available.Docs/TDF.mdand update changelog entries if external behavior changes.swiftformat --swiftversion 6.2 .andswift testprior to submission. Include performance numbers in the PR description.Completion Definition