lib: test bitmap vs IDA vs Maple Tree performance for region allocations
Subsystems (7)
PATCH v2 1/1 lib: test bitmap vs IDA vs Maple Tree performance for region allocations
Compare the cost of allocating and freeing variable-sized regions using a bitmap, IDA and a Maple Tree. All implementations process the same randomly generated sequence of region sizes, ranging from 1 to 32 entries, until the configured capacity is exhausted.
Run the benchmark at several capacities to show how the approaches scale. Report allocation and free times separately because bitmap, IDA and Maple Tree removal have substantially different costs.
On x86/kvm, the output example is:
type alloc (ns) free (ns) capacity memory (B)
bitmap 179573071 342105 1000000 125000 IDA 46555636 33931498 1000000 134864 maple 18629665 19741396 1000000 1548304 bitmap 1630912 30933 100000 12504 IDA 6144785 3354590 100000 14288 maple 1745026 1825032 100000 155408 bitmap 28448 3374 10000 1256 IDA 418978 333641 10000 1872 maple 185398 211138 10000 15632 bitmap 2253 610 1000 128 IDA 42755 36432 1000 144 maple 19728 23474 1000 1552
Reported IDA and Maple Tree memory figures exclude slab overhead and transient allocations. The Maple Tree figure is additionally a lower-bound estimate that assumes fully occupied leaf nodes and excludes internal nodes.
IDA has no region-allocation API, so each region is implemented as a sequence of single-ID allocations. The IDs remain contiguous because this benchmark fills an initially empty IDA monotonically.
The benchmark is motivated by the discussion at the link below about choosing the best data structure for the channel ID pool with the capacity of 2048 IDs for the nova GPU driver.
Specifically for 2048 IDs the result is:
bitmap 3907 831 2048 256 IDA 83545 71007 2048 848 maple 34632 39710 2048 3600
---
v2: add IDA benchmark (Matthew)
v1: https://lore.kernel.org/all/[email protected]/
MAINTAINERS | 3 + lib/Kconfig.debug | 9 ++ lib/Makefile | 1 + lib/region_alloc_benchmark.c | 174 +++++++++++++++++++++++++++++++++++ 4 files changed, 187 insertions(+) create mode 100644 lib/region_alloc_benchmark.c
MAINTAINERS +3 -0
@@ -4615,6 +4615,7 @@ F: lib/bitmap.c F: lib/cpumask.c F: lib/find_bit.c F: lib/find_bit_benchmark.c +F: lib/region_alloc_benchmark.c F: lib/test_bitmap.c F: lib/tests/cpumask_kunit.c F: tools/include/linux/bitfield.h
@@ -15581,6 +15582,7 @@ F: Documentation/core-api/maple_tree.rst F: include/linux/maple_tree.h F: include/trace/events/maple_tree.h F: lib/maple_tree.c +F: lib/region_alloc_benchmark.c F: lib/test_maple_tree.c F: rust/helpers/maple_tree.c F: rust/kernel/maple_tree.rs
@@ -29323,6 +29325,7 @@ F: Documentation/core-api/xarray.rst F: include/linux/idr.h F: include/linux/xarray.h F: lib/idr.c +F: lib/region_alloc_benchmark.c F: lib/test_xarray.c F: lib/xarray.c F: tools/testing/radix-tree
lib/Kconfig.debug +9 -0
@@ -2683,6 +2683,15 @@ config FIND_BIT_BENCHMARK If unsure, say N. +config REGION_ALLOC_BENCHMARK + tristate "Benchmark bitmap, IDA and Maple Tree region allocation" + help + This builds a microbenchmark comparing variable-sized region + allocation using bitmaps, IDA and Maple Tree. The benchmark + runs at module load time. + + If unsure, say N. + config FIND_BIT_BENCHMARK_RUST tristate "Test find_bit functions in Rust" depends on RUST
lib/Makefile +1 -0
@@ -64,6 +64,7 @@ obj-y += hexdump.o obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o obj-y += kstrtox.o obj-$(CONFIG_FIND_BIT_BENCHMARK) += find_bit_benchmark.o +obj-$(CONFIG_REGION_ALLOC_BENCHMARK) += region_alloc_benchmark.o obj-$(CONFIG_FIND_BIT_BENCHMARK_RUST) += find_bit_benchmark_rust.o obj-$(CONFIG_TEST_BPF) += test_bpf.o test_dhry-objs := dhry_1.o dhry_2.o dhry_run.o
lib/region_alloc_benchmark.c new file +174 -0
174 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The benchmark allocates over 9.1 MB of static arrays in `.init.data`, which bloats the kernel…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The benchmark allocates over 9.1 MB of static arrays in .init.data, which bloats the kernel image and can cause boot failures on memory-constrained architectures. --
--- Patch [1]: [PATCH v2] lib: test bitmap vs IDA vs Maple Tree performance for region allocations --- commit 3ff59b7f49e8b1a285e39778b952abbfe86e1ed9
Author: Yury Norov <[email protected]>
lib: test bitmap vs IDA vs Maple Tree performance for region allocations
This commit adds a microbenchmark to compare the cost of allocating and freeing variable-sized regions using a bitmap, IDA, and a Maple Tree. It processes a randomly generated sequence of region sizes until the configured capacity is exhausted.
Quoted 6 lines
lib/region_alloc_benchmark.c new file +0
-0@@ -0,0 +1,174 @@
[ ... ]
lib/region_alloc_benchmark.c line 19
Quoted 7 lines
lib/region_alloc_benchmark.c new file +7
-0+#define MAP_SIZE (1000000UL) +#define REGION_MAX_SIZE 32 + +static DECLARE_BITMAP(alloc_bitmap, MAP_SIZE) __initdata; +/* One more request guarantees that even an all-ones trace reaches ENOSPC. */ +static u8 region_sizes[MAP_SIZE +1 ]__initdata ; +static unsigned long region_indexes[MAP_SIZE ]__initdata ;
[Severity: Medium]
Is it safe to allocate over 9.1 MB of static arrays in .init.data here?
This inflates the uncompressed kernel image size and can cause build failures like relocation truncation or boot failures on memory-constrained architectures, especially when built-in via allmodconfig.
Could these large benchmark buffers be dynamically allocated at runtime using kvmalloc_array() or vmalloc() instead?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Onur Özkan
reviewerQuoted 25 lines · On Sat, 11 Jul 2026 02:36:01 -0400 Yury Norov <[email protected]> wrote:
Compare the cost of allocating and freeing variable-sized regions using a bitmap, IDA and a Maple Tree. All implementations process the same randomly generated sequence of region sizes, ranging from 1 to 32 entries, until the configured capacity is exhausted.
Run the benchmark at several capacities to show how the approaches scale. Report allocation and free times separately because bitmap, IDA and Maple Tree removal have substantially different costs.
On x86/kvm, the output example is:
type alloc (ns) free (ns) capacity memory (B)
bitmap 179573071 342105 1000000 125000 IDA 46555636 33931498 1000000 134864 maple 18629665 19741396 1000000 1548304 bitmap 1630912 30933 100000 12504 IDA 6144785 3354590 100000 14288 maple 1745026 1825032 100000 155408 bitmap 28448 3374 10000 1256 IDA 418978 333641 10000 1872 maple 185398 211138 10000 15632 bitmap 2253 610 1000 128 IDA 42755 36432 1000 144 maple 19728 23474 1000 1552
Quoted 4 lines
Reported IDA and Maple Tree memory figures exclude slab overhead and transient allocations. The Maple Tree figure is additionally a lower-bound estimate that assumes fully occupied leaf nodes and excludes internal nodes.
The report itself doesn't make it obvious. I think "memory (B)" can be misleading. Perhaps we should use a clearer column name or add a short note before/after the report for explaining the columns?
Regards,
Onur
Quoted 40 lines
IDA has no region-allocation API, so each region is implemented as a sequence of single-ID allocations. The IDs remain contiguous because this benchmark fills an initially empty IDA monotonically.
The benchmark is motivated by the discussion at the link below about choosing the best data structure for the channel ID pool with the capacity of 2048 IDs for the nova GPU driver.
Specifically for 2048 IDs the result is:
bitmap 3907 831 2048 256 IDA 83545 71007 2048 848 maple 34632 39710 2048 3600Signed-off-by Yury Norov <[email protected]>---
v2: add IDA benchmark (Matthew)
v1: https://lore.kernel.org/all/[email protected]/MAINTAINERS | 3 + lib/Kconfig.debug | 9 ++ lib/Makefile | 1 + lib/region_alloc_benchmark.c | 174 +++++++++++++++++++++++++++++++++++ 4 files changed, 187 insertions(+) create mode 100644 lib/region_alloc_benchmark.cMAINTAINERS +2
-0@@ -4615,6 +4615,7 @@ F: lib/bitmap.c F: lib/cpumask.c F: lib/find_bit.c F: lib/find_bit_benchmark.c +F: lib/region_alloc_benchmark.c F: lib/test_bitmap.c F: lib/tests/cpumask_kunit.c + bitmap_count = benchmark_bitmap(capacities[i]);
[...]
Quoted 17 lines
MAINTAINERS +14
-0+ ida_count = benchmark_ida(capacities[i]); + maple_count = benchmark_maple_tree(capacities[i]); + WARN_ON(bitmap_count != ida_count); + WARN_ON(bitmap_count != maple_count); + } + + /* Let the benchmark be loaded and run repeatedly without rmmod. */ + return -EINVAL; +} +module_init(region_alloc_benchmark); + +MODULE_AUTHOR("Yury Norov <[email protected]>"); +MODULE_DESCRIPTION("Benchmark bitmap, IDA and Maple Tree region allocation"); +MODULE_LICENSE("GPL");Signature · 1 line
2.53.0
Gary Guo
reviewerQuoted 141 lines · Yury Norov
Compare the cost of allocating and freeing variable-sized regions using a bitmap, IDA and a Maple Tree. All implementations process the same randomly generated sequence of region sizes, ranging from 1 to 32 entries, until the configured capacity is exhausted.
Run the benchmark at several capacities to show how the approaches scale. Report allocation and free times separately because bitmap, IDA and Maple Tree removal have substantially different costs.
On x86/kvm, the output example is:
type alloc (ns) free (ns) capacity memory (B)
bitmap 179573071 342105 1000000 125000 IDA 46555636 33931498 1000000 134864 maple 18629665 19741396 1000000 1548304 bitmap 1630912 30933 100000 12504 IDA 6144785 3354590 100000 14288 maple 1745026 1825032 100000 155408 bitmap 28448 3374 10000 1256 IDA 418978 333641 10000 1872 maple 185398 211138 10000 15632 bitmap 2253 610 1000 128 IDA 42755 36432 1000 144 maple 19728 23474 1000 1552Reported IDA and Maple Tree memory figures exclude slab overhead and transient allocations. The Maple Tree figure is additionally a lower-bound estimate that assumes fully occupied leaf nodes and excludes internal nodes.
IDA has no region-allocation API, so each region is implemented as a sequence of single-ID allocations. The IDs remain contiguous because this benchmark fills an initially empty IDA monotonically.
The benchmark is motivated by the discussion at the link below about choosing the best data structure for the channel ID pool with the capacity of 2048 IDs for the nova GPU driver.
Specifically for 2048 IDs the result is:
bitmap 3907 831 2048 256 IDA 83545 71007 2048 848 maple 34632 39710 2048 3600Signed-off-by Yury Norov <[email protected]>---
v2: add IDA benchmark (Matthew)
v1: https://lore.kernel.org/all/[email protected]/MAINTAINERS | 3 + lib/Kconfig.debug | 9 ++ lib/Makefile | 1 + lib/region_alloc_benchmark.c | 174 +++++++++++++++++++++++++++++++++++ 4 files changed, 187 insertions(+) create mode 100644 lib/region_alloc_benchmark.cMAINTAINERS +3
-0@@ -4615,6 +4615,7 @@ F: lib/bitmap.c F: lib/cpumask.c F: lib/find_bit.c F: lib/find_bit_benchmark.c +F: lib/region_alloc_benchmark.c F: lib/test_bitmap.c F: lib/tests/cpumask_kunit.c F: tools/include/linux/bitfield.h@@ -15581,6 +15582,7 @@ F: Documentation/core-api/maple_tree.rst F: include/linux/maple_tree.h F: include/trace/events/maple_tree.h F: lib/maple_tree.c +F: lib/region_alloc_benchmark.c F: lib/test_maple_tree.c F: rust/helpers/maple_tree.c F: rust/kernel/maple_tree.rs@@ -29323,6 +29325,7 @@ F: Documentation/core-api/xarray.rst F: include/linux/idr.h F: include/linux/xarray.h F: lib/idr.c +F: lib/region_alloc_benchmark.c F: lib/test_xarray.c F: lib/xarray.c F: tools/testing/radix-treelib/Kconfig.debug +9
-0@@ -2683,6 +2683,15 @@ config FIND_BIT_BENCHMARK If unsure, say N. +config REGION_ALLOC_BENCHMARK + tristate "Benchmark bitmap, IDA and Maple Tree region allocation" + help + This builds a microbenchmark comparing variable-sized region + allocation using bitmaps, IDA and Maple Tree. The benchmark + runs at module load time. + + If unsure, say N. + config FIND_BIT_BENCHMARK_RUST tristate "Test find_bit functions in Rust" depends on RUSTlib/Makefile +1
-0@@ -64,6 +64,7 @@ obj-y += hexdump.o obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o obj-y += kstrtox.o obj-$(CONFIG_FIND_BIT_BENCHMARK) += find_bit_benchmark.o +obj-$(CONFIG_REGION_ALLOC_BENCHMARK) += region_alloc_benchmark.o obj-$(CONFIG_FIND_BIT_BENCHMARK_RUST) += find_bit_benchmark_rust.o obj-$(CONFIG_TEST_BPF) += test_bpf.o test_dhry-objs := dhry_1.o dhry_2.o dhry_run.olib/region_alloc_benchmark.c new file +18
-0@@ -0,0 +1,174 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Benchmark bitmap, IDA and Maple Tree allocation of variable-sized regions. */ + +#include <linux/bitmap.h> +#include <linux/idr.h> +#include <linux/kernel.h> +#include <linux/maple_tree.h> +#include <linux/module.h> +#include <linux/printk.h> +#include <linux/random.h> +#include <linux/xarray.h> + +#define MAP_SIZE (1000000UL) +#define REGION_MAX_SIZE 32 + +static DECLARE_BITMAP(alloc_bitmap, MAP_SIZE) __initdata; +/* One more request guarantees that even an all-ones trace reaches ENOSPC. */ +static u8 region_sizes[MAP_SIZE +1 ]__initdata ;
lib/region_alloc_benchmark.c line 28
Quoted 10 lines
lib/region_alloc_benchmark.c new file +10
-0+static unsigned long region_indexes[MAP_SIZE ]__initdata ; + +static unsigned long __init benchmark_bitmap (unsigned long capacity) +{ +unsigned long count, index; +ktime_t alloc_time, free_time; +size_t memory; + +bitmap_zero (alloc_bitmap,MAP_SIZE ); + alloc_time =ktime_get ();
To eliminate possible effects of optimization messing up benchmark, please add a barrier() after start timing and a barrier() before end timing. Probably you'd want one between each loop iteration too.
Quoted 10 lines
lib/region_alloc_benchmark.c new file +10
-0+for (count =0 ; count <ARRAY_SIZE (region_sizes); count++) { + index =bitmap_find_next_zero_area (alloc_bitmap, capacity,0 , + region_sizes[count],0 ); +if (index >= capacity) +break ; + + region_indexes[count] = index; +bitmap_set (alloc_bitmap, index, region_sizes[count]); + } + alloc_time =ktime_get () - alloc_time;
lib/region_alloc_benchmark.c line 48
Quoted 10 lines
lib/region_alloc_benchmark.c new file +10
-0+ + index = count; + free_time =ktime_get (); +while (index--) +bitmap_clear (alloc_bitmap, region_indexes[index], + region_sizes[index]); + free_time =ktime_get () - free_time; + memory =BITS_TO_LONGS (capacity) *sizeof (unsigned long ); +pr_err ("%-6s %12llu %12llu %8lu %10zu\n" ,"bitmap" , alloc_time, + free_time, capacity, memory);
I wonder if the numbers could be more useful if it is somehow normalized (e.g. by how many iterations are performed?)? This number can be computed by adding up the prefix of region_sizes until it cover all capacity.
Also, the capacity should probably be made part of the report header for all 3 ID allocators. Given this is randomized benchmark the single line, when taken out of context, is not very meaningful anyway.
Something like this might be better (fake numbers below, you might also need some fix point printing):
capacity = 1000000, regions = 100000
type alloc (ns/region) free (ns/region) memory (B/capacity)
bitmap 1795.730 3.421 0.125 IDA 465.556 339.314 0.134 maple 186.296 197.413 1.548
capacity = 100000, regions = 10000
bitmap 163.091 3.093 0.125 IDA 614.478 335.459 0.142 maple 174.502 182.503 1.554
capacity = 10000, regions = 1000
bitmap 28.448 3.374 0.125 IDA 418.978 333.641 0.187 maple 185.398 211.138 1.563
capacity = 1000, regions = 100
bitmap 22.530 6.100 0.128 IDA 427.550 364.320 0.144 maple 197.280 234.740 1.552
Best,
Gary
Quoted 62 lines
lib/region_alloc_benchmark.c new file +62
-0+ +return count; +} + +static size_t __init ida_memory (unsigned long nr_ids) +{ +unsigned long entries =DIV_ROUND_UP (nr_ids,IDA_BITMAP_BITS ); +unsigned long bitmaps = nr_ids /IDA_BITMAP_BITS ; +unsigned long nodes =0 ; + +if (nr_ids %IDA_BITMAP_BITS >BITS_PER_XA_VALUE ) + bitmaps++; + +while (entries >1 ) { + entries =DIV_ROUND_UP (entries,XA_CHUNK_SIZE ); + nodes += entries; + } + +return sizeof (struct ida ) + bitmaps *sizeof (struct ida_bitmap ) + + nodes *sizeof (struct xa_node ); +} + +static unsigned long __init benchmark_ida (unsigned long capacity) +{ +struct ida ida =IDA_INIT (ida); +unsigned long count, index, offset, nr_ids =0 ; +ktime_t alloc_time, free_time; +int id = -ENOSPC ; + + alloc_time =ktime_get (); +for (count =0 ; count <ARRAY_SIZE (region_sizes); count++) { +for (offset =0 ; offset < region_sizes[count]; offset++) { + id =ida_alloc_max (&ida, capacity -1 ,GFP_KERNEL ); +if (id <0 ) +break ; +if (!offset) + region_indexes[count] = id; + } +if (id <0 ) { +while (offset--) +ida_free (&ida, region_indexes[count] + offset); +break ; + } +WARN_ON (id != region_indexes[count] + region_sizes[count] -1 ); + nr_ids += region_sizes[count]; + } + alloc_time =ktime_get () - alloc_time; +WARN_ON (id != -ENOSPC ); + + index = count; + free_time =ktime_get (); +while (index--) +for (offset =0 ; offset < region_sizes[index]; offset++) +ida_free (&ida, region_indexes[index] + offset); + free_time =ktime_get () - free_time; +pr_err ("%-6s %12llu %12llu %8lu %10zu\n" ,"IDA" , alloc_time, + free_time, capacity,ida_memory (nr_ids)); +ida_destroy (&ida); + +return count; +} +
Quoted 28 lines · Onur Özkan
Quoted 25 lines · On Sat, 11 Jul 2026 02:36:01 -0400 Yury Norov <[email protected]> wrote:
Compare the cost of allocating and freeing variable-sized regions using a bitmap, IDA and a Maple Tree. All implementations process the same randomly generated sequence of region sizes, ranging from 1 to 32 entries, until the configured capacity is exhausted.
Run the benchmark at several capacities to show how the approaches scale. Report allocation and free times separately because bitmap, IDA and Maple Tree removal have substantially different costs.
On x86/kvm, the output example is:
type alloc (ns) free (ns) capacity memory (B)
bitmap 179573071 342105 1000000 125000 IDA 46555636 33931498 1000000 134864 maple 18629665 19741396 1000000 1548304 bitmap 1630912 30933 100000 12504 IDA 6144785 3354590 100000 14288 maple 1745026 1825032 100000 155408 bitmap 28448 3374 10000 1256 IDA 418978 333641 10000 1872 maple 185398 211138 10000 15632 bitmap 2253 610 1000 128 IDA 42755 36432 1000 144 maple 19728 23474 1000 1552
Quoted 8 lines
Quoted 4 lines
Reported IDA and Maple Tree memory figures exclude slab overhead and transient allocations. The Maple Tree figure is additionally a lower-bound estimate that assumes fully occupied leaf nodes and excludes internal nodes.
The report itself doesn't make it obvious. I think "memory (B)" can be misleading. Perhaps we should use a clearer column name or add a short note before/after the report for explaining the columns?
OK, will add.
On Sat, Jul 11, 2026 at 02:51:28PM +0100, Gary Guo wrote:
...
Quoted 4 lines
Quoted 4 lines
lib/region_alloc_benchmark.c new file +4
-0+static unsigned long __init benchmark_bitmap (unsigned long capacity) +{ +unsigned long count, index; +ktime_t alloc_time, free_time;
Quoted 8 lines
lib/region_alloc_benchmark.c line 28
Quoted 4 lines
lib/region_alloc_benchmark.c new file +4
-0+size_t memory; + +bitmap_zero (alloc_bitmap,MAP_SIZE ); + alloc_time =ktime_get ();To eliminate possible effects of optimization messing up benchmark, please add a
barrier()after start timing and abarrier()before end timing. Probably you'd want one between each loop iteration too.
It was never a problem in find_bit_benchmark test, but OK.
Quoted 44 lines
Quoted 10 lines
lib/region_alloc_benchmark.c new file +10
-0+for (count =0 ; count <ARRAY_SIZE (region_sizes); count++) { + index =bitmap_find_next_zero_area (alloc_bitmap, capacity,0 , + region_sizes[count],0 ); +if (index >= capacity) +break ; + + region_indexes[count] = index; +bitmap_set (alloc_bitmap, index, region_sizes[count]); + } + alloc_time =ktime_get () - alloc_time;lib/region_alloc_benchmark.c line 48
Quoted 10 lines
lib/region_alloc_benchmark.c new file +10
-0+ + index = count; + free_time =ktime_get (); +while (index--) +bitmap_clear (alloc_bitmap, region_indexes[index], + region_sizes[index]); + free_time =ktime_get () - free_time; + memory =BITS_TO_LONGS (capacity) *sizeof (unsigned long ); +pr_err ("%-6s %12llu %12llu %8lu %10zu\n" ,"bitmap" , alloc_time, + free_time, capacity, memory);I wonder if the numbers could be more useful if it is somehow normalized (e.g. by how many iterations are performed?)? This number can be computed by adding up the prefix of
region_sizesuntil it cover all capacity.Also, the capacity should probably be made part of the report header for all 3 ID allocators. Given this is randomized benchmark the single line, when taken out of context, is not very meaningful anyway.
Something like this might be better (fake numbers below, you might also need some fix point printing):
capacity = 1000000, regions = 100000
type alloc (ns/region) free (ns/region) memory (B/capacity)bitmap 1795.730 3.421 0.125 IDA 465.556 339.314 0.134 maple 186.296 197.413 1.548capacity = 100000, regions = 10000
bitmap 163.091 3.093 0.125 IDA 614.478 335.459 0.142 maple 174.502 182.503 1.554capacity = 10000, regions = 1000
Quoted 8 lines
bitmap 28.448 3.374 0.125 IDA 418.978 333.641 0.187 maple 185.398 211.138 1.563capacity = 1000, regions = 100
bitmap 22.530 6.100 0.128 IDA 427.550 364.320 0.144 maple 197.280 234.740 1.552
I can add the 'regions' column, mostly for completeness. My experience with find_bit_benchmark is that people mostly ignore it.
Your version may look more 'human friendly', but the reality is that people understand the original version quite well. This "nice" formatting is a substantial complication for the log parsers.
Printing anything else, except for the raw test output numbers, is simply wrong. I don't want to force people to *my* analysis. If you believe that ns per region is more informative, you can do that trivial math yourself.
For more serious statistical analysis, one must take into consideration measurement errors, distribution skewness, random variables correlations and all that fancy statistical things.
In your example, you divide one random variable, ns, to another, number of regions. Each of them has its own distribution, and not necessarily normal. Simple divide operation becomes not so simple, if you do it right.
I'm not saying that this simple test requires an analysis of such complexity. I'm saying that raw output numbers are always preferred at data collection phase.
Thanks,
Yury
Quoted 17 lines · Yury Norov
On Sat, Jul 11, 2026 at 02:51:28PM +0100, Gary Guo wrote:
...
Quoted 4 lines
Quoted 4 lines
lib/region_alloc_benchmark.c new file +4
-0+static unsigned long __init benchmark_bitmap (unsigned long capacity) +{ +unsigned long count, index; +ktime_t alloc_time, free_time;Quoted 8 lines
lib/region_alloc_benchmark.c line 28
Quoted 4 lines
lib/region_alloc_benchmark.c new file +4
-0+size_t memory; + +bitmap_zero (alloc_bitmap,MAP_SIZE ); + alloc_time =ktime_get ();To eliminate possible effects of optimization messing up benchmark, please add a
barrier()after start timing and abarrier()before end timing. Probably you'd want one between each loop iteration too.
Quoted 1 line
It was never a problem in
find_bit_benchmarktest, but OK.
So, I ran some experiments: 5 times with barriers, and 5 times without, and repeated it for reverse order of capacities.
The results are:
Experiment 1,000,000 100,000
Bitmap allocate −0.28% (p=.631) −5.63% (p=.0039)
Bitmap release +0.89% (p=.247) −1.30% (p=.089)
IDA allocate −0.03% (p=.971) −19.98% (p=.035)
IDA release −0.27% (p=.315) +1.38% (p=.165)
Maple allocate +0.09% (p=.579) −1.06% (p=.529)
Maple release −0.54% (p=.315) +0.03% (p=.971)
10,000 1,000
Bitmap allocate +5.19% (p=.853) −3.17% (p=.739)
Bitmap release −2.16% (p=.853) −0.45% (p=.912)
IDA allocate −3.42% (p=.247) −6.80% (p=.190)
IDA release −1.21% (p=.315) −5.39% (p=.063)
Maple allocate −1.81% (p=.529) −0.32% (p=.529)
Maple release +1.39% (p=.739) +0.11% (p=.631)The barriers were only present in allocation loops, so release measurements serve as a negative control. Most effects are small and statistically insignificant.
Two allocation comparisons have unadjusted p < .05, but both show barriers making execution faster. Across all 24 comparisons, neither survives Holm/Bonferroni correction; the smallest adjusted p-value is approximately .093.
Codegeneration-wise, both objects have:
- Identical text size: 2,413 bytes.
- Identical instruction count.
- No additional loads, spills, or fences.
The barriers only changed scheduling of a few independent instructions:
- Bitmap: loop-counter increment moved across __bitmap_set().
- IDA: counter increment and ID accumulation swapped.
- Maple Tree: region_indexes[] store moved relative to the loop-bound comparison.
There is no code-generation change capable to produce visible performance impact.
With all that, I'll drop the barriers. Instead, I've added the WARN_ON(!xxx_empty()) after the release completion for the data
structures: to ensure the release is not optimized, and to additionally enforce integrity.
Thanks,
Yury
Quoted 12 lines · Yury Norov
Compare the cost of allocating and freeing variable-sized regions using a bitmap, IDA and a Maple Tree. All implementations process the same randomly generated sequence of region sizes, ranging from 1 to 32 entries, until the configured capacity is exhausted.
Run the benchmark at several capacities to show how the approaches scale. Report allocation and free times separately because bitmap, IDA and Maple Tree removal have substantially different costs.
On x86/kvm, the output example is:
type alloc (ns) free (ns) capacity memory (B)
Quoted 13 lines
bitmap 179573071 342105 1000000 125000 IDA 46555636 33931498 1000000 134864 maple 18629665 19741396 1000000 1548304 bitmap 1630912 30933 100000 12504 IDA 6144785 3354590 100000 14288 maple 1745026 1825032 100000 155408 bitmap 28448 3374 10000 1256 IDA 418978 333641 10000 1872 maple 185398 211138 10000 15632 bitmap 2253 610 1000 128 IDA 42755 36432 1000 144 maple 19728 23474 1000 1552
I don't understand the comparison. bitmap, IDA and maple are completely different data structures for completely different purposes.
1) IDA is 16 bytes when empty, maple is 16 bytes when empty, bitmap is $size bytes
2) bitmap is statically sized, IDA and maple have to deal with actual allocation and freeing of memory
3) xarray and maple (especially maple) are optimized for RCU usage and have different tradeoffs
4) IDA does not support ranges
5) xarray (the underlying data structure for IDA) does not support ranges in any optimal way
6) maple is not optimized for 1-sized ranges, nor ID allocation; it actually stores data, so instead of 1 bit per index you get 8 whole bytes.
7) maple and IDA both handle locking implicitly
There are also other pressing questions about the benchmark itself:
it's pretty much the ideal scenario for bitmap. doing the find in bitmap is essentially testing how far the CPU can speculate, and how large the cache is. It's pretty darn large on modern hardware. Since you never actually free the ranges in any way, every bitmap branch should predict extremely well. Fragmentation in the bitmap can very quickly turn into a nightmare, while maple will eat it up just fine.
These are very different data structures and picking between them requires understanding all the tradeoffs. I don't think this benchmark helps with that.
Signature · 1 line
Pedro
Quoted 27 lines · Pedro Falcato
Quoted 12 lines · Yury Norov
Compare the cost of allocating and freeing variable-sized regions using a bitmap, IDA and a Maple Tree. All implementations process the same randomly generated sequence of region sizes, ranging from 1 to 32 entries, until the configured capacity is exhausted.
Run the benchmark at several capacities to show how the approaches scale. Report allocation and free times separately because bitmap, IDA and Maple Tree removal have substantially different costs.
On x86/kvm, the output example is:
type alloc (ns) free (ns) capacity memory (B)
Quoted 13 lines
bitmap 179573071 342105 1000000 125000 IDA 46555636 33931498 1000000 134864 maple 18629665 19741396 1000000 1548304 bitmap 1630912 30933 100000 12504 IDA 6144785 3354590 100000 14288 maple 1745026 1825032 100000 155408 bitmap 28448 3374 10000 1256 IDA 418978 333641 10000 1872 maple 185398 211138 10000 15632 bitmap 2253 610 1000 128 IDA 42755 36432 1000 144 maple 19728 23474 1000 1552
Quoted 2 lines
I don't understand the comparison. bitmap, IDA and maple are completely different data structures for completely different purposes.
Sure. Please check this link from the patch description:
https://lore.kernel.org/all/[email protected]/
The test is very well motivated by that discussion. I'm not saying it compares apples to apples. I'm trying to quantify the answer to a very specific question: what is better for NOVA ID allocator. Or more
broadly: what's the best data structure for range ID allocators of a given capacity.
Quoted 5 lines
1) IDA is 16 bytes when empty, maple is 16 bytes when empty, bitmap is $size bytes
2) bitmap is statically sized, IDA and maple have to deal with actual allocation and freeing of memory
3) xarray and maple (especially maple) are optimized for RCU usage and
Quoted 8 lines
have different tradeoffs
4) IDA does not support ranges
5) xarray (the underlying data structure for IDA) does not support ranges in any optimal way
6) maple is not optimized for 1-sized ranges, nor ID allocation; it actually stores data, so instead of 1 bit per index you get 8 whole bytes.
7) maple and IDA both handle locking implicitly
Yes, this to some extent follows John's arguments why bitmaps are preferred for NOVA Id allocator. I'd also add serializeability, if it's needed.
Quoted 2 lines
There are also other pressing questions about the benchmark itself:
it's pretty much the ideal scenario for bitmap.
It's pretty much the worst scenario for bitmaps, because the test starts searching for a region from the beginning of the bitmap on every iteration, making it "Shlemiel the Painter" algorithm with O(N^2) complexity.
If I wanted to cheat, I'd carry the last allocated ID over the loop. It would make the complexity O(N). I could choose to start searching from a random ID, saying it's 'more realistic'. It makes the complexity converging to O(N*logN). This is how cpumask_*_distribute() works, btw.
So no, this is far from being the ideal scenario.
Quoted 3 lines
doing the find in
bitmap is essentially testing how far the CPU can speculate, and how large the cache is. It's pretty darn large on modern hardware.
Yes, it's a very effective data structure. That's why it's the 2nd most popular in the kernel, after spinlocks.
Quoted 4 lines
Since you
never actually free the ranges in any way, every bitmap branch should predict extremely well. Fragmentation in the bitmap can very quickly turn into a nightmare, while maple will eat it up just fine.
Assuming "fragmentation" is the uniform distribution of free areas, it wouldn't "turn to nightmare", it would asymptotically turn to O(N*logN).
Quoted 3 lines
These are very different data structures and picking between them requires understanding all the tradeoffs. I don't think this benchmark helps with that.
OK, I see. You seemingly think that I want to throw all and every data structure away from the kernel in favor of the bitmaps.
No, I don't.
Thanks,
Yury
Matthew Wilcox
maintainerQuoted 5 lines · Yury Norov
The test is very well motivated by that discussion. I'm not saying it compares apples to apples. I'm trying to quantify the answer to a very specific question: what is better for NOVA ID allocator. Or more
broadly: what's the best data structure for range ID allocators of a given capacity.
From my work with the IDA, there are definitely ID allocators which allocate only a single ID, less than ten or less than a hundred. Could you scale the benchmark down to 10 and 100 rather than starting at 1000?
Quoted 10 lines · Matthew Wilcox
Quoted 5 lines · Yury Norov
The test is very well motivated by that discussion. I'm not saying it compares apples to apples. I'm trying to quantify the answer to a very specific question: what is better for NOVA ID allocator. Or more
broadly: what's the best data structure for range ID allocators of a given capacity.From my work with the IDA, there are definitely ID allocators which allocate only a single ID, less than ten or less than a hundred. Could you scale the benchmark down to 10 and 100 rather than starting at 1000?
OK.