]> git.ipfire.org Git - thirdparty/rspamd.git/commit
[Feature] Improve memory pool destructors and allocation strategies
authorVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 20 Oct 2025 21:22:28 +0000 (22:22 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 20 Oct 2025 21:22:28 +0000 (22:22 +0100)
commita6b086302cc36adb33eb74c3d9bc7116695dc55a
tree13dbae5ad92e69d51db946bef4580e4f081b34d9
parent599bb105b2e153ca70834bf4b68aac94f3a24fcb
[Feature] Improve memory pool destructors and allocation strategies

This commit introduces several improvements to the memory pool subsystem:

1. Priority-based destructors using binary heap:
   - Replace linked list with min-heap for deterministic destructor ordering
   - Add rspamd_mempool_add_destructor_priority() for priority control
   - Maintain backward compatibility with existing rspamd_mempool_add_destructor()
   - Destructors now execute in priority order (lowest first)

2. Destructor statistics and preallocation:
   - Track destructor count per allocation point in entry statistics
   - Preallocate heap based on historical usage patterns
   - Adaptive sizing with configurable maximum (128 destructors)

3. Pool type differentiation:
   - Add RSPAMD_MEMPOOL_LONG_LIVED flag for configuration/global data
   - Add RSPAMD_MEMPOOL_SHORT_LIVED flag for task/temporary data
   - Optimize page sizes: 16KB minimum for long-lived, 4KB for short-lived
   - Provide convenience macros: rspamd_mempool_new_long_lived() and
     rspamd_mempool_new_short_lived()

4. Heap utility enhancements:
   - Add rspamd_min_heap_size() to query heap element count
   - Enable better integration with pool statistics

Benefits:
- Controlled resource cleanup order prevents use-after-free scenarios
- Reduced memory fragmentation for long-lived pools
- Better performance for frequently created/destroyed short-lived pools
- Automatic adaptation to actual usage patterns
src/libutil/heap.c
src/libutil/heap.h
src/libutil/mem_pool.c
src/libutil/mem_pool.h
src/libutil/mem_pool_internal.h