Add benchmark tests and optimize encryption performance

- Introduced comprehensive benchmark tests for NIP-44 and NIP-4 encryption/decryption, including various message sizes and round-trip operations.
- Implemented optimizations to reduce memory allocations and CPU processing time in encryption functions, focusing on pre-allocating buffers and minimizing reallocations.
- Enhanced error handling in encryption and decryption processes to ensure robustness.
- Documented performance improvements in the new PERFORMANCE_REPORT.md file, highlighting significant reductions in execution time and memory usage.
This commit is contained in:
2025-11-02 18:08:11 +00:00
parent b47a40bc59
commit 53fb12443e
9 changed files with 1237 additions and 20 deletions

View File

@@ -77,9 +77,18 @@ func AppendList(
dst []byte, src [][]byte, separator byte,
ac AppendBytesClosure,
) []byte {
// Pre-allocate buffer if nil to reduce reallocations
// Estimate: sum of all source sizes + separators
if dst == nil && len(src) > 0 {
estimatedSize := len(src) - 1 // separators
for i := range src {
estimatedSize += len(src[i]) * 2 // worst case with escaping
}
dst = make([]byte, 0, estimatedSize)
}
last := len(src) - 1
for i := range src {
dst = append(dst, ac(dst, src[i])...)
dst = ac(dst, src[i])
if i < last {
dst = append(dst, separator)
}