Skip to content

Conversation

@joamaki
Copy link
Contributor

@joamaki joamaki commented Nov 10, 2025

The 'results' map was getting allocated fresh for each round and with large batch size the cpu profile showed bunch of time being spent on rehashing.

Benchmark before:

1000000 objects reconciled in 1.44 seconds (batch size 1000) Throughput 694132.66 objects per second
1135MB total allocated, 6014828 in-use objects, 338MB bytes in use 2.18user 0.22system 0:01.53elapsed 157%CPU (0avgtext+0avgdata 678224maxresident)k 0inputs+0outputs (0major+208598minor)pagefaults 0swaps

Benchmark after:

1000000 objects reconciled in 1.44 seconds (batch size 1000) Throughput 696145.36 objects per second
886MB total allocated, 6014843 in-use objects, 338MB bytes in use 1.90user 0.19system 0:01.53elapsed 136%CPU (0avgtext+0avgdata 662688maxresident)k 0inputs+0outputs (0major+211455minor)pagefaults 0swaps

The 'results' map was getting allocated fresh for each round and with large
batch size the cpu profile showed bunch of time being spent on rehashing.

Benchmark before:

1000000 objects reconciled in 1.44 seconds (batch size 1000)
Throughput 694132.66 objects per second
1135MB total allocated, 6014828 in-use objects, 338MB bytes in use
2.18user 0.22system 0:01.53elapsed 157%CPU (0avgtext+0avgdata 678224maxresident)k
0inputs+0outputs (0major+208598minor)pagefaults 0swaps

Benchmark after:

1000000 objects reconciled in 1.44 seconds (batch size 1000)
Throughput 696145.36 objects per second
886MB total allocated, 6014843 in-use objects, 338MB bytes in use
1.90user 0.19system 0:01.53elapsed 136%CPU (0avgtext+0avgdata 662688maxresident)k
0inputs+0outputs (0major+211455minor)pagefaults 0swaps

Signed-off-by: Jussi Maki <[email protected]>
@joamaki joamaki requested a review from a team as a code owner November 10, 2025 11:00
@joamaki joamaki requested review from pippolo84 and removed request for a team November 10, 2025 11:00
@github-actions
Copy link

$ make
go build ./...
go: downloading go1.24.0 (linux/amd64)
go: downloading go.yaml.in/yaml/v3 v3.0.3
go: downloading github.com/cilium/hive v0.0.0-20250731144630-28e7a35ed227
go: downloading golang.org/x/time v0.5.0
go: downloading github.com/spf13/cobra v1.8.0
go: downloading github.com/spf13/pflag v1.0.5
go: downloading github.com/cilium/stream v0.0.0-20240209152734-a0792b51812d
go: downloading github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de
go: downloading github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
go: downloading github.com/mitchellh/mapstructure v1.5.0
go: downloading go.uber.org/dig v1.17.1
go: downloading golang.org/x/term v0.16.0
go: downloading github.com/spf13/viper v1.18.2
go: downloading golang.org/x/sys v0.17.0
go: downloading golang.org/x/tools v0.17.0
go: downloading github.com/spf13/cast v1.6.0
go: downloading github.com/fsnotify/fsnotify v1.7.0
go: downloading github.com/sagikazarmark/slog-shim v0.1.0
go: downloading github.com/spf13/afero v1.11.0
go: downloading github.com/subosito/gotenv v1.6.0
go: downloading github.com/hashicorp/hcl v1.0.0
go: downloading gopkg.in/ini.v1 v1.67.0
go: downloading github.com/magiconair/properties v1.8.7
go: downloading github.com/pelletier/go-toml/v2 v2.1.0
go: downloading gopkg.in/yaml.v3 v3.0.1
go: downloading golang.org/x/text v0.14.0
go test ./... -cover -vet=all -test.count 1
go: downloading github.com/stretchr/testify v1.8.4
go: downloading go.uber.org/goleak v1.3.0
go: downloading golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
go: downloading github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2
ok  	github.com/cilium/statedb	21.775s	coverage: 80.8% of statements
ok  	github.com/cilium/statedb/index	0.014s	coverage: 28.7% of statements
ok  	github.com/cilium/statedb/internal	0.027s	coverage: 46.7% of statements
ok  	github.com/cilium/statedb/part	4.326s	coverage: 88.3% of statements
ok  	github.com/cilium/statedb/reconciler	0.245s	coverage: 89.1% of statements
	github.com/cilium/statedb/reconciler/benchmark		coverage: 0.0% of statements
	github.com/cilium/statedb/reconciler/example		coverage: 0.0% of statements
go test -race ./... -test.count 1
ok  	github.com/cilium/statedb	108.319s
ok  	github.com/cilium/statedb/index	1.013s
ok  	github.com/cilium/statedb/internal	1.034s
ok  	github.com/cilium/statedb/part	32.641s
ok  	github.com/cilium/statedb/reconciler	1.358s
?   	github.com/cilium/statedb/reconciler/benchmark	[no test files]
?   	github.com/cilium/statedb/reconciler/example	[no test files]
go test ./... -bench . -benchmem -test.run xxx
goos: linux
goarch: amd64
pkg: github.com/cilium/statedb
cpu: Intel(R) Xeon(R) Platinum 8370C CPU @ 2.80GHz
BenchmarkDB_WriteTxn_1-4                    	  542863	      1971 ns/op	    507281 objects/sec	    1288 B/op	      24 allocs/op
BenchmarkDB_WriteTxn_10-4                   	 1379109	       898.3 ns/op	   1113220 objects/sec	     677 B/op	       9 allocs/op
BenchmarkDB_WriteTxn_100-4                  	 1892676	       698.3 ns/op	   1432134 objects/sec	     419 B/op	       7 allocs/op
BenchmarkDB_WriteTxn_1000-4                 	 1844871	       663.3 ns/op	   1507550 objects/sec	     363 B/op	       7 allocs/op
BenchmarkDB_WriteTxn_100_SecondaryIndex-4   	  585412	      1922 ns/op	    520409 objects/sec	    1229 B/op	      37 allocs/op
BenchmarkDB_NewWriteTxn-4                   	 1500297	       800.5 ns/op	     544 B/op	       8 allocs/op
BenchmarkDB_NewReadTxn-4                    	769796583	         1.610 ns/op	       0 B/op	       0 allocs/op
BenchmarkDB_Modify-4                        	    1735	    746467 ns/op	   1339645 objects/sec	  421796 B/op	    8130 allocs/op
BenchmarkDB_GetInsert-4                     	    1618	    815095 ns/op	   1226852 objects/sec	  405756 B/op	    8129 allocs/op
BenchmarkDB_RandomInsert-4                  	    1864	    710333 ns/op	   1407791 objects/sec	  397264 B/op	    7129 allocs/op
BenchmarkDB_RandomReplace-4                 	     396	   3066885 ns/op	    326064 objects/sec	 1724165 B/op	   46155 allocs/op
BenchmarkDB_SequentialInsert-4              	    2025	    680661 ns/op	   1469159 objects/sec	  363417 B/op	    7101 allocs/op
BenchmarkDB_SequentialInsert_Prefix-4       	     441	   2808982 ns/op	    356001 objects/sec	 3475218 B/op	   56557 allocs/op
BenchmarkDB_Changes_Baseline-4              	    1453	    887339 ns/op	   1126965 objects/sec	  436305 B/op	   10193 allocs/op
BenchmarkDB_Changes-4                       	     818	   1610889 ns/op	    620775 objects/sec	  760631 B/op	   14377 allocs/op
BenchmarkDB_RandomLookup-4                  	   23898	     49711 ns/op	  20116416 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_SequentialLookup-4              	   31075	     38535 ns/op	  25950220 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_Prefix_SecondaryIndex-4         	    7467	    144706 ns/op	   6910575 objects/sec	  125114 B/op	    1031 allocs/op
BenchmarkDB_FullIteration_All-4             	    1258	    989746 ns/op	 101036003 objects/sec	     320 B/op	      11 allocs/op
BenchmarkDB_FullIteration_Get-4             	     229	   5209598 ns/op	  19195339 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_FullIteration_ReadTxnGet-4      	     246	   4850827 ns/op	  20615043 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_PropagationDelay-4              	  623749	      2108 ns/op	        17.00 50th_µs	        21.00 90th_µs	       130.0 99th_µs	    1440 B/op	      22 allocs/op
BenchmarkWatchSet_4-4                       	 2140526	       560.6 ns/op	     320 B/op	       5 allocs/op
BenchmarkWatchSet_16-4                      	  608238	      1695 ns/op	    1096 B/op	       5 allocs/op
BenchmarkWatchSet_128-4                     	   84067	     14542 ns/op	    8904 B/op	       5 allocs/op
BenchmarkWatchSet_1024-4                    	    8748	    140149 ns/op	   73744 B/op	       5 allocs/op
PASS
ok  	github.com/cilium/statedb	34.561s
PASS
ok  	github.com/cilium/statedb/index	0.003s
PASS
ok  	github.com/cilium/statedb/internal	0.003s
goos: linux
goarch: amd64
pkg: github.com/cilium/statedb/part
cpu: Intel(R) Xeon(R) Platinum 8370C CPU @ 2.80GHz
Benchmark_Uint64Map_Random-4                  	    1418	    801776 ns/op	   1247231 items/sec	 2573701 B/op	    7060 allocs/op
Benchmark_Uint64Map_Sequential-4              	    1800	    692248 ns/op	   1444570 items/sec	 2266257 B/op	    6755 allocs/op
Benchmark_Uint64Map_Sequential_Insert-4       	    1749	    636507 ns/op	   1571075 items/sec	 2258257 B/op	    5754 allocs/op
Benchmark_Uint64Map_Sequential_Txn_Insert-4   	   10000	    108007 ns/op	   9258665 items/sec	   89068 B/op	    2035 allocs/op
Benchmark_Uint64Map_Random_Insert-4           	    1653	    729423 ns/op	   1370947 items/sec	 2563316 B/op	    6042 allocs/op
Benchmark_Uint64Map_Random_Txn_Insert-4       	    6634	    186149 ns/op	   5372051 items/sec	  139551 B/op	    2597 allocs/op
Benchmark_Insert_RootOnlyWatch-4              	   12235	     98141 ns/op	  10189456 objects/sec	   74270 B/op	    2041 allocs/op
Benchmark_Insert-4                            	    8445	    146195 ns/op	   6840171 objects/sec	  190386 B/op	    3074 allocs/op
Benchmark_Modify-4                            	   14593	     86764 ns/op	  11525575 objects/sec	   71007 B/op	    1017 allocs/op
Benchmark_GetInsert-4                         	   10000	    107967 ns/op	   9262096 objects/sec	   71018 B/op	    1017 allocs/op
Benchmark_Replace-4                           	42608322	        27.74 ns/op	  36045410 objects/sec	       0 B/op	       0 allocs/op
Benchmark_Replace_RootOnlyWatch-4             	43170369	        27.81 ns/op	  35953818 objects/sec	       0 B/op	       0 allocs/op
Benchmark_txn_1-4                             	 5378364	       220.4 ns/op	   4537687 objects/sec	     216 B/op	       4 allocs/op
Benchmark_txn_10-4                            	10805846	       110.9 ns/op	   9017644 objects/sec	      90 B/op	       2 allocs/op
Benchmark_txn_100-4                           	13110589	        91.44 ns/op	  10936672 objects/sec	      89 B/op	       2 allocs/op
Benchmark_txn_1000-4                          	11086810	       102.0 ns/op	   9803973 objects/sec	      77 B/op	       2 allocs/op
Benchmark_txn_delete_1-4                      	 1930861	       659.6 ns/op	   1516058 objects/sec	    2952 B/op	       5 allocs/op
Benchmark_txn_delete_10-4                     	 8956024	       135.5 ns/op	   7379679 objects/sec	     336 B/op	       1 allocs/op
Benchmark_txn_delete_100-4                    	15085370	        81.60 ns/op	  12254626 objects/sec	      79 B/op	       1 allocs/op
Benchmark_txn_delete_1000-4                   	16598912	        72.39 ns/op	  13813525 objects/sec	      27 B/op	       1 allocs/op
Benchmark_Get-4                               	   49908	     23762 ns/op	  42083418 objects/sec	       0 B/op	       0 allocs/op
Benchmark_Iterate-4                           	  189880	      6249 ns/op	 160037136 objects/sec	     104 B/op	       4 allocs/op
Benchmark_Hashmap_Insert-4                    	   19586	     60611 ns/op	  16498676 objects/sec	   74265 B/op	      20 allocs/op
Benchmark_Hashmap_Get_Uint64-4                	  175610	      6835 ns/op	 146309151 objects/sec	       0 B/op	       0 allocs/op
Benchmark_Hashmap_Get_Bytes-4                 	  139180	      8612 ns/op	 116122521 objects/sec	       0 B/op	       0 allocs/op
Benchmark_Delete_Random-4                     	      16	  70730136 ns/op	   1413825 objects/sec	71860455 B/op	  137797 allocs/op
PASS
ok  	github.com/cilium/statedb/part	32.657s
PASS
ok  	github.com/cilium/statedb/reconciler	0.004s
?   	github.com/cilium/statedb/reconciler/benchmark	[no test files]
?   	github.com/cilium/statedb/reconciler/example	[no test files]
go run ./reconciler/benchmark -quiet
1000000 objects reconciled in 2.20 seconds (batch size 1000)
Throughput 454117.43 objects per second
885MB total allocated, 6014798 in-use objects, 338MB bytes in use

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.

2 participants