Skip to content

refactor(alloc): Track objects in small extent - #9327

Open
TreeHunter9 wants to merge 2 commits into
FirebirdSQL:masterfrom
TreeHunter9:master-alloc-small-objects-deallocation
Open

TreeHunter9 wants to merge 2 commits into
FirebirdSQL:masterfrom
TreeHunter9:master-alloc-small-objects-deallocation

Conversation

@TreeHunter9

@TreeHunter9 TreeHunter9 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Main problem

Some customers reported enormous memory usage, ~120GiB was mapped by single transaction, even though it was currently using only ~2GiB. The attachment that holds this transaction at the moment was doing basically nothing, but still, it holds 120GiB of memory.
I investigated this anomaly and discovered that all 120GiB had been taken from the parent pool (tra_pool -> att_pool). I have found that this amount of memory can be redirected only via small allocations, to be specific, when small extent need to be allocated, it can be redirected to parent pool without any limits. Looking at our allocator closely, I have found that small objects are not freed upon deallocation, these objects are only removed when the pool is destroyed. It means, until these objects is alive, we cannot give back the extents that we borrow from the parent pool.

As I understand, it was done intentionally, and I see why we might want to preserve these small objects, but still, I don't think this tradeoff works in our favor. If tra_pool -> att_pool memory usage can be fixed by commiting transaction, in att_pool -> dbb_pool chain memory can be freed only when attachment is dead, so in this non-reclamation of memory can make things even worse.

Reproducible example
SET TERM ^;

CREATE PROCEDURE ALLOC_BLOB_BURST (P_BLOB_COUNT INTEGER)
AS
    DECLARE B BLOB SUB_TYPE TEXT;
    DECLARE BLOBS_CREATED INT;
BEGIN
    BLOBS_CREATED = 0;
    WHILE (BLOBS_CREATED < P_BLOB_COUNT) DO
    BEGIN
        B = CAST('x' AS BLOB SUB_TYPE TEXT);
        BLOBS_CREATED = BLOBS_CREATED + 1;
    END
END^

SET TERM ;^

EXECUTE PROCEDURE ALLOC_BLOB_BURST(16000000); -- Be careful, several GiB will be consumed.

COMMIT RETAIN;

SELECT * FROM MON$MEMORY_USAGE ORDER BY MON$MEMORY_ALLOCATED DESC;

Solution

Before I begin my explanation, I'd like to clarify some terminology: small extent - 4-7 KiB extent, hunk or big extent - 64KiB extent that is allocated directly by OS. Hunk is cut into small extents, as well as extent is cut into slots. The hunk can be freed when its extents are freed, and extent can be freed when its slots are freed.

I propose track when a small extents become empty, like it's done for medium objects. This will allow us to detect when a small extent contains no active objects, so we can safely deallocate it. The baseline patch is straight forward, DoubleLinkedList will provide policy for this logic, but overall I propose other changes to improve performance of allocator:

  1. Add a local cache of small extents. The main change reduces the allocator's performance in reusability cases, so I introduce a small local cache to slightly improve performance in such case.
  2. Change redirection logic, so the child pool will not wait busy parent and do the allocation on its own. This change is not related to the main problem, but I found this behavior is strange, because the one of the main purpose of memory pools is to eliminate contention under parallel memory allocation/deallocation. The redirection mechanism is kinda going against this logic, but in tradeoff introduce memory reusability between child pools.

Side note on local cache: it can lead to memory fragmentation. By memory fragmentation I mean if pool is going allocate only 8 bytes - it will hold whole 64KiB hunk. It also means if local cache will hold all 24 small extents, it is possible that each of the small extent is came from different hunk, so by holding these 24 small extents, we will hold 1.5MiB. This is still better then holding every small object forever, but this is a tradeoff between memory footprint and performance. I have introduce FIFO eviction algorithm to minimize memory footprint and limit cache size to 24 extents (I think it's a sweet spot).

Benchmarks

I'd also like to propose addition of small benchmark to Firebird project. I've read about this practice in other large projects and I think it will be a good addition for us as well. If someone also would introduce changes to allocator module, they will be able to use this bench to test their patch. For my patch, it was very helpful to see how changes affects performance in various cases.
For example, my first implementation of local cache was targeting hunk cache. Right now we have only global hunk cache, and I thought that introduction of local hunks cache would increase performance, but it didn't, so I came up with current implementation of local cache.

Benchmark can be build via make alloc_bench.
To run predefined scenarios I wrote a python script, it can be run like this:

python3 run_alloc_bench.py --threads 1,8 --pool-per-thread --binary ~/firebird/gen/Release/firebird/tests/alloc_bench --output-folder ~/firebird/bench_results/ --output-prefix with-patch

My predefined scenarios are grouped by sequence (a lot of allocations, then all deallocation) and mixed (allocations/deallocation mixed):

  • sequence-long - large live set, full cleanup, and retained memory
  • sequence-reuse - repeated allocation and extent reuse;
  • mixed-frequent - frequent small frees, approximately balanced load;
  • mixed-rare - rare large bursts of deallocations, approximately balanced load;
  • mixed-drain - repeatedly empty the live-object list;
  • mixed-growing - growing live set despite intermittent frees

Unpatched allocator → full patch

Operation latencies here cover iteration 1. Later iterations give the unpatched pool a previously allocated working set to reuse. Whole-case time includes that reuse. In eight-thread sequence-long, mapping before the first cleanup was close (2,181 → 2,158 MiB). The deallocation time is increased because of cleanup overhead, but this overhead was basically moved from pool deletion, so case allocation -> deallocation -> pool deletion in terms of time is the same.

The large difference appeared after cleanup, this is the real problem this patch should fix. The biggest change can be seen in sequence-long case, where memory after cleanup reduced from 2GiB to 0.44MiB.

Table
Workload Threads Whole-case wall, s Iter 1 alloc, ns/op Iter 1 free, ns/op Mapped before first cleanup, MiB Retained after final cleanup, MiB
sequence-long 1 3.571 → 4.161 (+16.5%) 197.5 → 199.3 (+0.9%) 116.0 → 190.1 (+63.9%) 2,181 → 2,186 2,182 → 0.44
sequence-long 8 1.774 → 1.876 (+5.8%) 1,551.9 → 793.6 (−48.9%) 331.6 → 837.6 (+152.6%) 2,181 → 2,158 2,186 → 7.15
sequence-reuse 1 2.053 → 2.594 (+26.4%) 203.5 → 204.1 (+0.3%) 115.6 → 189.1 (+63.6%) 545.19 → 546.38 547.56 → 0.56
sequence-reuse 8 0.771 → 1.104 (+43.2%) 1,550.9 → 812.5 (−47.6%) 325.0 → 696.8 (+114.4%) 545.19 → 539.79 552.04 → 7.15
mixed-frequent 1 1.548 → 1.572 (+1.5%) 54.7 → 58.2 (+6.4%) 49.2 → 48.9 (−0.6%) 1.19 → 0.44 1.19 → 0.75
mixed-frequent 8 0.603 → 0.622 (+3.1%) 267.2 → 281.9 (+5.5%) 257.5 → 265.4 (+3.1%) 4.31 → 4.77 4.38 → 4.08
mixed-rare 1 1.374 → 1.471 (+7.1%) 53.7 → 62.8 (+17.0%) 48.8 → 52.3 (+7.2%) 9.00 → 2.75 15.38 → 0.69
mixed-rare 8 0.598 → 0.601 (+0.4%) 280.1 → 279.2 (−0.3%) 260.9 → 259.3 (−0.6%) 37.44 → 33.60 46.33 → 7.60
mixed-drain 1 1.367 → 1.453 (+6.3%) 51.8 → 58.7 (+13.2%) 46.9 → 49.1 (+4.7%) 0 → 0 0 → 0.06
mixed-drain 8 0.568 → 0.600 (+5.7%) 251.3 → 282.6 (+12.5%) 248.7 → 271.1 (+9.0%) 0.65 → 0.96 0.75 → 1.02
mixed-growing 1 1.098 → 1.297 (+18.1%) 160.1 → 161.4 (+0.8%) 47.5 → 48.7 (+2.7%) 382.56 → 383.44 383.50 → 0.44
mixed-growing 8 0.454 → 0.510 (+12.3%) 1,144.5 → 576.6 (−49.6%) 85.7 → 162.7 (+89.8%) 383.56 → 380.06 386.96 → 6.60

Single candidate (old behavior - 1 cache slot) → 24-slot FIFO, with parent try-lock enabled in both builds

The mixed scenario is getting improvements, as expected.

Table
Workload Threads Allocation, ns/op Free, ns/op Whole-case wall, s Retained after final cleanup, MiB
sequence-long 1 199.3 → 198.2 (−0.6%) 181.1 → 186.7 (+3.1%) 4.139 → 4.161 (+0.5%) 0.31 → 0.44
sequence-long 8 811.9 → 808.8 (−0.4%) 821.1 → 811.5 (−1.2%) 1.884 → 1.876 (−0.4%) 1.94 → 7.15
sequence-reuse 1 198.5 → 200.8 (+1.2%) 181.7 → 188.2 (+3.5%) 2.556 → 2.594 (+1.5%) 0.25 → 0.56
sequence-reuse 8 796.3 → 791.5 (−0.6%) 714.4 → 711.0 (−0.5%) 1.104 → 1.104 (0.0%) 1.94 → 7.15
mixed-frequent 1 56.8 → 57.9 (+1.9%) 49.4 → 48.8 (−1.3%) 1.585 → 1.572 (−0.8%) 0.19 → 0.75
mixed-frequent 8 277.4 → 282.1 (+1.7%) 266.4 → 267.4 (+0.4%) 0.620 → 0.622 (+0.4%) 1.25 → 4.08
mixed-rare 1 66.9 → 62.3 (−6.7%) 56.0 → 51.6 (−7.9%) 1.547 → 1.471 (−5.0%) 0.38 → 0.69
mixed-rare 8 288.9 → 280.2 (−3.0%) 268.2 → 263.5 (−1.7%) 0.615 → 0.601 (−2.3%) 1.67 → 7.60
mixed-drain 1 61.0 → 58.2 (−4.5%) 51.7 → 48.7 (−5.9%) 1.506 → 1.453 (−3.5%) 0 → 0.06
mixed-drain 8 277.6 → 282.0 (+1.6%) 267.8 → 271.4 (+1.3%) 0.595 → 0.600 (+0.8%) 0.83 → 1.02
mixed-growing 1 158.0 → 158.8 (+0.5%) 48.9 → 48.7 (−0.3%) 1.287 → 1.297 (+0.8%) 0.31 → 0.44
mixed-growing 8 556.3 → 557.5 (+0.2%) 162.8 → 164.5 (+1.0%) 0.509 → 0.510 (+0.3%) 1.90 → 6.60

Blocking parent allocation → parent try-lock, with the 24-slot FIFO in both builds

The sequence allocations are getting huge performance increase in multithreaded scenario, but memory footprint is slightly increase.
I think this is a good tradeoff.

Table
Workload Threads Allocation, ns/op Free, ns/op Whole-case wall, s Wall Δ Retained after cleanup, MiB
sequence-long 1 201.4 → 198.2 183.5 → 186.7 4.155 → 4.161 +0.1% 0.44 → 0.44
sequence-long 8 1,593.7 → 808.8 1,219.7 → 811.5 3.025 → 1.876 −38.0% 6.52 → 7.15
sequence-reuse 1 191.6 → 200.8 182.8 → 188.2 2.515 → 2.594 +3.1% 0.56 → 0.56
sequence-reuse 8 1,529.5 → 791.5 1,215.4 → 711.0 1.844 → 1.104 −40.1% 5.81 → 7.15
mixed-frequent 1 55.8 → 57.9 48.1 → 48.8 1.541 → 1.572 +2.0% 0.75 → 0.75
mixed-frequent 8 273.0 → 282.1 259.3 → 267.4 0.618 → 0.622 +0.6% 3.67 → 4.08
mixed-rare 1 60.7 → 62.3 50.9 → 51.6 1.442 → 1.471 +2.0% 0.69 → 0.69
mixed-rare 8 285.9 → 280.2 265.4 → 263.5 0.609 → 0.601 −1.3% 8.00 → 7.60
mixed-drain 1 56.2 → 58.2 47.5 → 48.7 1.415 → 1.453 +2.7% 0.06 → 0.06
mixed-drain 8 283.6 → 282.0 270.4 → 271.4 0.599 → 0.600 +0.1% 0.81 → 1.02
mixed-growing 1 151.5 → 158.8 47.5 → 48.7 1.252 → 1.297 +3.6% 0.44 → 0.44
mixed-growing 8 1,107.5 → 557.5 88.4 → 164.5 0.816 → 0.510 −37.5% 6.60 → 6.60

P.S.: If someone want to run these bench on it's own pc, use this suggestions:

using SmallFreeList = NonReclaimingFreeList; // To restore old behavior for small objects.
using SmallFreeList = ReclaimingFreeList<SingleExtentCache>; // Remove local extents cache.

constexpr Flags NO_WAIT_REDIRECTION = 0 << 1; // To disable no-wait behavior.

Artyom Ivanov added 2 commits November 20, 2026 16:50
- Implement small extents cache;
- Add no-wait behavior on parent allocation, so we don't have to wait parent and do allocation on its own;
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.

1 participant