Skip to content

Conversation

joamaki
Copy link
Contributor

@joamaki joamaki commented Jul 30, 2025

We can do the same trick in leaf as we can do in the header and store the pointer and length to the full key instead of storing the key as []byte. This will require 10 bytes (2+8) instead of the 24 bytes we'd need for the slice. According to unsafe.Sizeof leaf is now 40 bytes instead of 56 (TIL: you can just do unsafe.Sizeof(thing{}) in your IDE and gopls will show "56 of constant unused"!)

The reconciler benchmark shows the improvement nicely (~9% less):

Before:

1225MB total allocated, 6011196 in-use objects, 384MB bytes in use

After:

1168MB total allocated, 6011151 in-use objects, 353MB bytes in use

We can do the same trick in leaf as we can do in the header and store
the pointer and length to the full key instead of storing the key as
[]byte. This will require 10 bytes (2+8) instead of the 24 bytes we'd
need for the slice. According to unsafe.Sizeof leaf is now 40 bytes
instead of 56.

The reconciler benchmark shows the improvement nicely (~9% less):

Before:
1225MB total allocated, 6011196 in-use objects, 384MB bytes in use

After:
1168MB total allocated, 6011151 in-use objects, 353MB bytes in use

Signed-off-by: Jussi Maki <jussi@isovalent.com>
@joamaki joamaki requested a review from a team as a code owner July 30, 2025 09:19
@joamaki joamaki requested review from derailed and removed request for a team July 30, 2025 09:19
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-20250522123230-2946c4940f41
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/go-viper/mapstructure/v2 v2.2.1
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/mitchellh/mapstructure v1.5.0
go: downloading github.com/sagikazarmark/slog-shim v0.1.0
go: downloading github.com/spf13/afero v1.11.0
go: downloading golang.org/x/text v0.14.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 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.426s	coverage: 83.4% of statements
ok  	github.com/cilium/statedb/index	0.005s	coverage: 28.7% of statements
ok  	github.com/cilium/statedb/internal	0.014s	coverage: 46.7% of statements
ok  	github.com/cilium/statedb/part	4.551s	coverage: 87.2% of statements
ok  	github.com/cilium/statedb/reconciler	0.206s	coverage: 88.5% 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	102.854s
ok  	github.com/cilium/statedb/index	1.013s
ok  	github.com/cilium/statedb/internal	1.024s
ok  	github.com/cilium/statedb/part	35.618s
ok  	github.com/cilium/statedb/reconciler	1.297s
?   	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: AMD EPYC 7763 64-Core Processor                
BenchmarkDB_WriteTxn_1-4                    	  506292	      2226 ns/op	    449260 objects/sec	    1416 B/op	      26 allocs/op
BenchmarkDB_WriteTxn_10-4                   	 1453927	       890.7 ns/op	   1122757 objects/sec	     481 B/op	       9 allocs/op
BenchmarkDB_WriteTxn_100-4                  	 1950566	       616.7 ns/op	   1621612 objects/sec	     400 B/op	       7 allocs/op
BenchmarkDB_WriteTxn_1000-4                 	 1828501	       656.6 ns/op	   1523075 objects/sec	     364 B/op	       7 allocs/op
BenchmarkDB_WriteTxn_100_SecondaryIndex-4   	  565724	      1996 ns/op	    500910 objects/sec	    1212 B/op	      37 allocs/op
BenchmarkDB_NewWriteTxn-4                   	 1516321	       797.3 ns/op	     544 B/op	       8 allocs/op
BenchmarkDB_NewReadTxn-4                    	480253039	         2.490 ns/op	       0 B/op	       0 allocs/op
BenchmarkDB_Modify-4                        	    1665	    721619 ns/op	   1385773 objects/sec	  423043 B/op	    8143 allocs/op
BenchmarkDB_GetInsert-4                     	    1485	    819716 ns/op	   1219935 objects/sec	  404455 B/op	    8140 allocs/op
BenchmarkDB_RandomInsert-4                  	    1820	    684040 ns/op	   1461903 objects/sec	  395510 B/op	    7139 allocs/op
BenchmarkDB_RandomReplace-4                 	     369	   3253183 ns/op	    307391 objects/sec	 1976465 B/op	   49162 allocs/op
BenchmarkDB_SequentialInsert-4              	    1872	    661824 ns/op	   1510976 objects/sec	  364294 B/op	    7114 allocs/op
BenchmarkDB_SequentialInsert_Prefix-4       	     396	   2995185 ns/op	    333869 objects/sec	 3718027 B/op	   58568 allocs/op
BenchmarkDB_Changes_Baseline-4              	    1418	    846443 ns/op	   1181415 objects/sec	  433535 B/op	   10214 allocs/op
BenchmarkDB_Changes-4                       	     807	   1510210 ns/op	    662160 objects/sec	  755608 B/op	   14420 allocs/op
BenchmarkDB_RandomLookup-4                  	   21495	     55913 ns/op	  17884951 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_SequentialLookup-4              	   25070	     47954 ns/op	  20853346 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_Prefix_SecondaryIndex-4         	    6823	    162210 ns/op	   6164843 objects/sec	  125114 B/op	    1031 allocs/op
BenchmarkDB_FullIteration_All-4             	    1134	   1070093 ns/op	  93449823 objects/sec	     320 B/op	      11 allocs/op
BenchmarkDB_FullIteration_Get-4             	     188	   6403574 ns/op	  15616279 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_FullIteration_ReadTxnGet-4      	     183	   6401689 ns/op	  15620879 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_PropagationDelay-4              	  596035	      1970 ns/op	        17.00 50th_µs	        19.00 90th_µs	        41.00 99th_µs	    1070 B/op	      22 allocs/op
BenchmarkWatchSet_4-4                       	 1307600	       916.1 ns/op	     500 B/op	       8 allocs/op
BenchmarkWatchSet_16-4                      	  500326	      2396 ns/op	    1700 B/op	       8 allocs/op
BenchmarkWatchSet_128-4                     	   61795	     19387 ns/op	   13374 B/op	       8 allocs/op
BenchmarkWatchSet_1024-4                    	    6166	    192049 ns/op	  105636 B/op	       8 allocs/op
PASS
ok  	github.com/cilium/statedb	33.725s
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: AMD EPYC 7763 64-Core Processor                
Benchmark_Uint64Map_Random-4        	    1287	    905626 ns/op	   1104209 items/sec	 2814773 B/op	   11038 allocs/op
Benchmark_Uint64Map_Sequential-4    	    1426	    834677 ns/op	   1198068 items/sec	 2591249 B/op	   11749 allocs/op
Benchmark_Insert_RootOnlyWatch-4    	    9823	    119403 ns/op	   8375022 objects/sec	   74538 B/op	    2044 allocs/op
Benchmark_Insert-4                  	    6820	    174924 ns/op	   5716776 objects/sec	  190506 B/op	    3076 allocs/op
Benchmark_Modify-4                  	   12174	     98590 ns/op	  10143032 objects/sec	   72028 B/op	    1028 allocs/op
Benchmark_GetInsert-4               	    8007	    141974 ns/op	   7043542 objects/sec	   72038 B/op	    1028 allocs/op
Benchmark_Replace-4                 	27831987	        42.97 ns/op	  23273203 objects/sec	       0 B/op	       0 allocs/op
Benchmark_Replace_RootOnlyWatch-4   	27829827	        43.44 ns/op	  23019995 objects/sec	       0 B/op	       0 allocs/op
Benchmark_txn_1-4                   	 3232782	       366.8 ns/op	   2725979 objects/sec	     280 B/op	       5 allocs/op
Benchmark_txn_10-4                  	 8548386	       139.9 ns/op	   7149781 objects/sec	      97 B/op	       2 allocs/op
Benchmark_txn_100-4                 	10449492	       114.3 ns/op	   8750613 objects/sec	      91 B/op	       2 allocs/op
Benchmark_txn_1000-4                	 9439226	       126.4 ns/op	   7909071 objects/sec	      78 B/op	       2 allocs/op
Benchmark_txn_delete_1-4            	 1539332	       777.8 ns/op	   1285630 objects/sec	    3272 B/op	       8 allocs/op
Benchmark_txn_delete_10-4           	 7190470	       165.6 ns/op	   6037364 objects/sec	     368 B/op	       2 allocs/op
Benchmark_txn_delete_100-4          	10954842	       108.0 ns/op	   9260651 objects/sec	      82 B/op	       1 allocs/op
Benchmark_txn_delete_1000-4         	11743257	       101.5 ns/op	   9855189 objects/sec	      28 B/op	       1 allocs/op
Benchmark_Get-4                     	   33778	     35461 ns/op	  28199688 objects/sec	       0 B/op	       0 allocs/op
Benchmark_Iterate-4                 	  163422	      7507 ns/op	 133211419 objects/sec	     104 B/op	       4 allocs/op
Benchmark_Hashmap_Insert-4          	   14908	     80595 ns/op	  12407758 objects/sec	   74265 B/op	      20 allocs/op
Benchmark_Hashmap_Get_Uint64-4      	  139504	      8570 ns/op	 116684190 objects/sec	       0 B/op	       0 allocs/op
Benchmark_Hashmap_Get_Bytes-4       	  110719	     10837 ns/op	  92275651 objects/sec	       0 B/op	       0 allocs/op
PASS
ok  	github.com/cilium/statedb/part	26.988s
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.54 seconds (batch size 1000)
Throughput 394344.88 objects per second
1167MB total allocated, 6011117 in-use objects, 353MB bytes in use

@dylandreimerink dylandreimerink merged commit 07e1d47 into main Jul 31, 2025
1 check passed
@dylandreimerink dylandreimerink deleted the pr/joamaki/shrink-leaf branch July 31, 2025 11:10
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