Skip to content

Conversation

JacobBarthelmeh
Copy link
Contributor

@JacobBarthelmeh JacobBarthelmeh commented Jul 29, 2025

bash-3.2$ ./memory_bucket_optimizer ../testwolfcrypt.log 
Found 24 unique allocation sizes
Peak heap usage: 60074 bytes (maximum concurrent memory usage)

Allocation Sizes, Frequencies, and Concurrent Usage:
Size    Count   Max Concurrent
----    -----   --------------
4208    1       1
3128    914     19
1600    13      1
1584    85      1
1120    13      1
1040    1       1
1024    4       2
800     37      1
257     65      2
256     9       1
235     7       1
227     5       1
223     5       1
207     5       1
191     5       1
136     5       1
128     8       1
104     5       1
72      5       1
64      6       1
48      5       1
32      2       1
28      1       1
0       0       0

Optimization Summary:
Padding size per bucket: 32 bytes
Maximum unique buckets allowed: 9
Total buckets created: 9
Note: Reached maximum bucket limit (9). Some allocations may use larger buckets.
Note: Allocations with waste < padding size use existing buckets to reduce overhead
Note: Bucket limit helps balance memory efficiency vs. management overhead

Optimized Bucket Sizes and Distribution:
Data Size + Padding = Bucket Size    Dist
----------------------------------------
272     + 32      = 304            2
800     + 32      = 832            1
1024    + 32      = 1056           2
1040    + 32      = 1072           1
1120    + 32      = 1152           1
1584    + 32      = 1616           1
1600    + 32      = 1632           1
3136    + 32      = 3168           19
4208    + 32      = 4240           1

WOLFMEM_BUCKETS and WOLFMEM_DIST Macros:
#define WOLFMEM_BUCKETS 304,832,1056,1072,1152,1616,1632,3168,4240
#define WOLFMEM_DIST 2,1,2,1,1,1,1,19,1
Memory Efficiency Analysis:
Note: Allocations with waste < 32 bytes (padding size) use existing buckets
Size    Count   Concurrent Bucket   Waste   Coverage
----    -----   ---------- ------   -----   --------
4208    1       1          4240    0       ✓
3128    914     19         3168    8       ✓
1600    13      1          1632    0       ✓
1584    85      1          1616    0       ✓
1120    13      1          1152    0       ✓
1040    1       1          1072    0       ✓
1024    4       2          1056    0       ✓
800     37      1          832     0       ✓
257     65      2          304     15      ✓
256     9       1          304     16      ✓
235     7       1          304     37      ✓
227     5       1          304     45      ✓
223     5       1          304     49      ✓
207     5       1          304     65      ✓
191     5       1          304     81      ✓
136     5       1          304     136     ✓
128     8       1          304     144     ✓
104     5       1          304     168     ✓
72      5       1          304     200     ✓
64      6       1          304     208     ✓
48      5       1          304     224     ✓
32      2       1          304     240     ✓
28      1       1          304     244     ✓
0       0       0          304     272     ✓

Efficiency Summary:
Total allocations: 1206
Allocations handled: 1206 (100.0%)
Total memory waste: 16654.00 bytes
Average waste per allocation: 13.81 bytes
Total bucket memory: 73456 bytes
Memory overhead: 1239 bytes
  - Padding per bucket: 32 bytes (included in bucket sizes)
  - Heap structures: 296 bytes
  - Alignment: 15 bytes
Total memory needed: 74695 bytes
Data memory: 3096130 bytes

Buffer Size Recommendations:
============================
Minimum buffer size needed: 74696 bytes

Usage in wolfSSL application:
============================
// Allocate buffer
byte staticBuffer[74696];

// Load static memory
WOLFSSL_HEAP_HINT* heapHint = NULL;
if (wc_LoadStaticMemory_ex(&heapHint, 9, bucket_sizes, bucket_dist,
    staticBuffer, 74696, 0, 0) != 0) {
    // Handle error
}

// Use in wolfSSL context
wolfSSL_CTX_load_static_memory(&method, NULL, staticBuffer,
    74696, 0, 1);
 

@JacobBarthelmeh JacobBarthelmeh self-assigned this Jul 29, 2025
@dgarske dgarske self-requested a review August 4, 2025 23:58
@dgarske dgarske self-assigned this Aug 5, 2025
Copy link
Contributor

@dgarske dgarske left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Works great

@dgarske dgarske merged commit 05b3b76 into wolfSSL:master Aug 5, 2025
1 check passed
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.

3 participants