Skip to content

Minor WriteTxn optimizations - #200

Merged
joamaki merged 5 commits into
mainfrom
pr/joamaki/txn-misc-optimizations
Sep 14, 2026
Merged

joamaki merged 5 commits into
mainfrom
pr/joamaki/txn-misc-optimizations

Conversation

@joamaki

@joamaki joamaki commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor
  • Instead of deduplicating just detect duplicates and panic in WriteTxn(). This would be a very rare bug on caller side and it doesn't make sense to pay extra (allocation, memory copying) to handle it gracefully. This increases the one-insert-per-WriteTxn throughput ~6% (see commit message).
  • Drop the unused writeTxnState.duration field
  • Drop the internal.SortableMutex interface and just use the concrete type. This was leftover from when StateDB and the SortableMutex was in cilium/cilium.
  • Change index.NewKeySet to take one argument and then vararg to remove an allocation. Add index.EmptyKeySet for the empty case.
  • Co-locate tableIndex array with the tableEntry for the common number of indexes to save allocating the []tableIndex slice

Before:

BenchmarkDB_WriteTxn_1-6                         1000000              1211 ns/op            825674 objects/sec       704 B/op         17 allocs/op
BenchmarkDB_WriteTxn_10-6                        2840197               420.9 ns/op         2376025 objects/sec       386 B/op          8 allocs/op
BenchmarkDB_WriteTxn_100-6                       3218432               385.1 ns/op         2596802 objects/sec       349 B/op          7 allocs/op
BenchmarkDB_WriteTxn_1000-6                      3152139               385.6 ns/op         2593571 objects/sec       350 B/op          7 allocs/op
BenchmarkDB_WriteTxn_100_SecondaryIndex-6        1698141               595.8 ns/op         1678371 objects/sec       507 B/op         12 allocs/op
BenchmarkDB_WriteTxn_CommitOnly_100Tables-6      1231267               983.5 ns/op          1128 B/op          6 allocs/op
BenchmarkDB_WriteTxn_CommitOnly_1Table-6         2035423               575.1 ns/op           240 B/op          6 allocs/op
BenchmarkDB_NewWriteTxn-6                        2327306               545.5 ns/op           216 B/op          5 allocs/op
BenchmarkDB_WriteTxnCommit100-6                  1220266               976.9 ns/op          1112 B/op          6 allocs/op

1000000 objects reconciled in 1.38 seconds (batch size 1000)
Throughput 722359.06 objects per second
568MB total allocated, 6015215 in-use objects, 239MB bytes in use

After:

BenchmarkDB_WriteTxn_1-6                         1352152               878.4 ns/op         1138474 objects/sec       664 B/op         14 allocs/op
BenchmarkDB_WriteTxn_10-6                        3196346               398.1 ns/op         2511816 objects/sec       361 B/op          7 allocs/op
BenchmarkDB_WriteTxn_100-6                       3865876               319.0 ns/op         3135261 objects/sec       325 B/op          6 allocs/op
BenchmarkDB_WriteTxn_1000-6                      2920508               357.4 ns/op         2797619 objects/sec       326 B/op          6 allocs/op
BenchmarkDB_WriteTxn_100_SecondaryIndex-6        2353370               529.3 ns/op         1889448 objects/sec       435 B/op          9 allocs/op
BenchmarkDB_WriteTxn_CommitOnly_100Tables-6      1396680               864.0 ns/op          1112 B/op          4 allocs/op
BenchmarkDB_WriteTxn_CommitOnly_1Table-6         2435631               499.4 ns/op           224 B/op          4 allocs/op
BenchmarkDB_NewWriteTxn-6                        2788807               424.8 ns/op           200 B/op          3 allocs/op
BenchmarkDB_WriteTxnCommit100-6          1417305               857.5 ns/op          1096 B/op          4 allocs/op

1000000 objects reconciled in 1.34 seconds (batch size 1000)
Throughput 747666.87 objects per second
522MB total allocated, 6015195 in-use objects, 239MB bytes in use

AIL:3

@joamaki
joamaki requested a review from a team as a code owner September 9, 2026 09:41
@joamaki
joamaki requested review from derailed and removed request for a team September 9, 2026 09:41
@github-actions

github-actions Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
$ make
go build ./...
go: downloading github.com/cilium/hive v1.0.4
go: downloading golang.org/x/time v0.15.0
go: downloading go.yaml.in/yaml/v3 v3.0.4
go: downloading github.com/spf13/cobra v1.10.2
go: downloading github.com/spf13/pflag v1.0.10
go: downloading github.com/cilium/stream v0.0.1
go: downloading github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de
go: downloading github.com/spf13/viper v1.18.2
go: downloading go.uber.org/dig v1.17.1
go: downloading golang.org/x/term v0.16.0
go: downloading github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
go: downloading github.com/mitchellh/mapstructure v1.5.0
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
STATEDB_VALIDATE=1 go test ./... -cover -vet=all -test.count 1
go: downloading github.com/stretchr/testify v1.11.1
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	297.495s	coverage: 79.5% of statements
ok  	github.com/cilium/statedb/index	0.005s	coverage: 48.6% of statements
ok  	github.com/cilium/statedb/internal	0.016s	coverage: 45.2% of statements
ok  	github.com/cilium/statedb/lpm	4.466s	coverage: 76.4% of statements
ok  	github.com/cilium/statedb/part	73.648s	coverage: 86.4% of statements
ok  	github.com/cilium/statedb/reconciler	0.278s	coverage: 93.3% 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	40.189s
ok  	github.com/cilium/statedb/index	1.014s
ok  	github.com/cilium/statedb/internal	1.025s
ok  	github.com/cilium/statedb/lpm	2.852s
ok  	github.com/cilium/statedb/part	38.554s
ok  	github.com/cilium/statedb/reconciler	1.371s
?   	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                      	  835876	      1320 ns/op	    757700 objects/sec	     664 B/op	      14 allocs/op
BenchmarkDB_WriteTxn_10-4                     	 2053500	       581.5 ns/op	   1719674 objects/sec	     361 B/op	       7 allocs/op
BenchmarkDB_WriteTxn_100-4                    	 2485747	       480.8 ns/op	   2079875 objects/sec	     325 B/op	       6 allocs/op
BenchmarkDB_WriteTxn_1000-4                   	 2277399	       535.4 ns/op	   1867883 objects/sec	     326 B/op	       6 allocs/op
BenchmarkDB_WriteTxn_100_SecondaryIndex-4     	 1483285	       806.3 ns/op	   1240205 objects/sec	     435 B/op	       9 allocs/op
BenchmarkDB_WriteTxn_CommitOnly_100Tables-4   	 1000000	      1156 ns/op	    1112 B/op	       4 allocs/op
BenchmarkDB_WriteTxn_CommitOnly_1Table-4      	 1812550	       663.7 ns/op	     224 B/op	       4 allocs/op
BenchmarkDB_NewWriteTxn-4                     	 2079746	       573.6 ns/op	     200 B/op	       3 allocs/op
BenchmarkDB_WriteTxnCommit100-4               	 1000000	      1137 ns/op	    1096 B/op	       4 allocs/op
BenchmarkDB_NewReadTxn-4                      	547529245	         2.218 ns/op	       0 B/op	       0 allocs/op
BenchmarkDB_Modify-4                          	    2025	    578896 ns/op	   1727427 objects/sec	  350553 B/op	    7078 allocs/op
BenchmarkDB_GetInsert-4                       	    1821	    654565 ns/op	   1527733 objects/sec	  334547 B/op	    7078 allocs/op
BenchmarkDB_RandomInsert-4                    	    2208	    533816 ns/op	   1873304 objects/sec	  326542 B/op	    6078 allocs/op
BenchmarkDB_RandomReplace-4                   	    1153	   1033103 ns/op	    967958 objects/sec	  448471 B/op	   10107 allocs/op
BenchmarkDB_SequentialInsert-4                	    2302	    523533 ns/op	   1910099 objects/sec	  326542 B/op	    6078 allocs/op
BenchmarkDB_SequentialInsert_Prefix-4         	     516	   2308692 ns/op	    433146 objects/sec	 2816358 B/op	   44534 allocs/op
BenchmarkDB_Changes_Baseline-4                	    1905	    627621 ns/op	   1593318 objects/sec	  396531 B/op	    7197 allocs/op
BenchmarkDB_Changes-4                         	     994	   1212287 ns/op	    824887 objects/sec	  612528 B/op	   10367 allocs/op
BenchmarkDB_RandomLookup-4                    	   23977	     50076 ns/op	  19969829 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_SequentialLookup-4                	   29409	     40877 ns/op	  24463612 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_Prefix_SecondaryIndex-4           	    7088	    163357 ns/op	   6121575 objects/sec	  124952 B/op	    1026 allocs/op
BenchmarkDB_FullIteration_All-4               	     981	   1227902 ns/op	  81439720 objects/sec	     104 B/op	       4 allocs/op
BenchmarkDB_FullIteration_Prefix-4            	     861	   1254140 ns/op	  79735900 objects/sec	     136 B/op	       5 allocs/op
BenchmarkDB_FullIteration_Get-4               	     225	   5414430 ns/op	  18469164 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_FullIteration_Get_Secondary-4     	      94	  11899543 ns/op	   8403684 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_FullIteration_ReadTxnGet-4        	     198	   5957587 ns/op	  16785319 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_PropagationDelay-4                	  782505	      1464 ns/op	        12.00 50th_µs	        14.00 90th_µs	        46.00 99th_µs	     826 B/op	      18 allocs/op
BenchmarkDB_WriteTxn_100_LPMIndex-4           	  530859	      2292 ns/op	    436392 objects/sec	    1582 B/op	      36 allocs/op
BenchmarkDB_WriteTxn_1_LPMIndex-4             	  136552	     13859 ns/op	     72155 objects/sec	   13669 B/op	      79 allocs/op
BenchmarkDB_LPMIndex_Get-4                    	     426	   3146638 ns/op	   3177995 objects/sec	       0 B/op	       0 allocs/op
BenchmarkWatchSet_4-4                         	 2320267	       512.6 ns/op	     296 B/op	       4 allocs/op
BenchmarkWatchSet_16-4                        	  723944	      1556 ns/op	    1096 B/op	       5 allocs/op
BenchmarkWatchSet_128-4                       	   87046	     13414 ns/op	    8904 B/op	       5 allocs/op
BenchmarkWatchSet_1024-4                      	    8749	    133604 ns/op	   73743 B/op	       5 allocs/op
PASS
ok  	github.com/cilium/statedb	45.399s
PASS
ok  	github.com/cilium/statedb/index	0.004s
goos: linux
goarch: amd64
pkg: github.com/cilium/statedb/internal
cpu: AMD EPYC 7763 64-Core Processor                
Benchmark_SortableMutex-4   	 6281302	       190.1 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/cilium/statedb/internal	1.198s
goos: linux
goarch: amd64
pkg: github.com/cilium/statedb/lpm
cpu: AMD EPYC 7763 64-Core Processor                
Benchmark_txn_insert/batchSize=1-4         	    1902	    631228 ns/op	   1584215 objects/sec	  822408 B/op	   13975 allocs/op
Benchmark_txn_insert/batchSize=10-4        	    3218	    370921 ns/op	   2695990 objects/sec	  369195 B/op	    6668 allocs/op
Benchmark_txn_insert/batchSize=100-4       	    3500	    347863 ns/op	   2874694 objects/sec	  329613 B/op	    6027 allocs/op
Benchmark_txn_delete/batchSize=1-4         	    1548	    764262 ns/op	   1308451 objects/sec	 1270473 B/op	   13976 allocs/op
Benchmark_txn_delete/batchSize=10-4        	    3274	    364947 ns/op	   2740121 objects/sec	  356418 B/op	    5769 allocs/op
Benchmark_txn_delete/batchSize=100-4       	    3620	    328351 ns/op	   3045518 objects/sec	  270754 B/op	    5038 allocs/op
Benchmark_LPM_Lookup-4                     	    9138	    129952 ns/op	   7695153 objects/sec	       0 B/op	       0 allocs/op
Benchmark_LPM_All-4                        	  142927	      9142 ns/op	 109387328 objects/sec	      32 B/op	       1 allocs/op
Benchmark_LPM_Prefix-4                     	  139167	      8886 ns/op	 112531501 objects/sec	      32 B/op	       1 allocs/op
Benchmark_LPM_LowerBound-4                 	  255192	      4691 ns/op	 106585277 objects/sec	     288 B/op	       2 allocs/op
PASS
ok  	github.com/cilium/statedb/lpm	12.119s
goos: linux
goarch: amd64
pkg: github.com/cilium/statedb/part
cpu: AMD EPYC 7763 64-Core Processor                
Benchmark_Set_Singleton_Create-4              	21763986	        55.01 ns/op	      24 B/op	       1 allocs/op
Benchmark_Set_Singleton_Has-4                 	100000000	        11.22 ns/op	       0 B/op	       0 allocs/op
Benchmark_StringMap_Txn_Insert-4              	    8052	    142014 ns/op	   7041544 items/sec	   98250 B/op	    1306 allocs/op
Benchmark_Uint64Map_Random-4                  	    1838	    638519 ns/op	   1566123 items/sec	 1323228 B/op	    6039 allocs/op
Benchmark_Uint64Map_Sequential-4              	    1896	    629425 ns/op	   1588751 items/sec	 1703091 B/op	    5753 allocs/op
Benchmark_Uint64Map_Sequential_Insert-4       	    2106	    610805 ns/op	   1637184 items/sec	 1695085 B/op	    4752 allocs/op
Benchmark_Uint64Map_Sequential_Txn_Insert-4   	   10000	    117225 ns/op	   8530620 items/sec	   90464 B/op	    2031 allocs/op
Benchmark_Uint64Map_Random_Insert-4           	    2107	    558453 ns/op	   1790660 items/sec	 1313553 B/op	    5043 allocs/op
Benchmark_Uint64Map_Random_Txn_Insert-4       	    6318	    185858 ns/op	   5380449 items/sec	  117276 B/op	    2409 allocs/op
Benchmark_Insert_RootOnlyWatch-4              	    9586	    120829 ns/op	   8276184 objects/sec	   75552 B/op	    2036 allocs/op
Benchmark_Insert-4                            	    8586	    141834 ns/op	   7050487 objects/sec	   84256 B/op	    3067 allocs/op
Benchmark_WatchReplace-4                      	    9831	    119006 ns/op	   8402948 objects/sec	   65346 B/op	    2011 allocs/op
Benchmark_Modify-4                            	   12669	     94546 ns/op	  10576914 objects/sec	   58056 B/op	    1007 allocs/op
Benchmark_GetInsert-4                         	   10000	    119009 ns/op	   8402757 objects/sec	   58056 B/op	    1007 allocs/op
Benchmark_Replace-4                           	33531774	        35.35 ns/op	  28285759 objects/sec	       0 B/op	       0 allocs/op
Benchmark_Replace_RootOnlyWatch-4             	34023486	        35.24 ns/op	  28380255 objects/sec	       0 B/op	       0 allocs/op
Benchmark_txn_1-4                             	 7856659	       152.4 ns/op	   6561692 objects/sec	      64 B/op	       3 allocs/op
Benchmark_txn_10-4                            	10686026	       112.7 ns/op	   8869431 objects/sec	      76 B/op	       2 allocs/op
Benchmark_txn_100-4                           	11877378	       100.9 ns/op	   9911210 objects/sec	      67 B/op	       2 allocs/op
Benchmark_txn_1000-4                          	10372112	       113.6 ns/op	   8803919 objects/sec	      65 B/op	       2 allocs/op
Benchmark_txn_delete_1-4                      	 5297407	       228.6 ns/op	   4374963 objects/sec	     632 B/op	       3 allocs/op
Benchmark_txn_delete_10-4                     	10939876	       108.2 ns/op	   9239460 objects/sec	     103 B/op	       1 allocs/op
Benchmark_txn_delete_100-4                    	12559380	        89.37 ns/op	  11189379 objects/sec	      35 B/op	       1 allocs/op
Benchmark_txn_delete_1000-4                   	13444818	        88.70 ns/op	  11273867 objects/sec	      28 B/op	       1 allocs/op
Benchmark_Get-4                               	   43892	     27292 ns/op	  36641135 objects/sec	       0 B/op	       0 allocs/op
Benchmark_GetWatch-4                          	   41415	     28952 ns/op	  34539438 objects/sec	       0 B/op	       0 allocs/op
Benchmark_All-4                               	  141802	      8491 ns/op	 117778472 objects/sec	       0 B/op	       0 allocs/op
Benchmark_Iterator_All-4                      	  157492	      7602 ns/op	 131551188 objects/sec	       0 B/op	       0 allocs/op
Benchmark_Iterator_Next-4                     	  149664	      7928 ns/op	 126128093 objects/sec	     896 B/op	       1 allocs/op
Benchmark_Hashmap_Insert-4                    	   14499	     83428 ns/op	  11986317 objects/sec	   74264 B/op	      20 allocs/op
Benchmark_Hashmap_Get_Uint64-4                	  134826	      8857 ns/op	 112900559 objects/sec	       0 B/op	       0 allocs/op
Benchmark_Hashmap_Get_Bytes-4                 	  109160	     10945 ns/op	  91366403 objects/sec	       0 B/op	       0 allocs/op
Benchmark_Delete_Random-4                     	      64	  21836859 ns/op	   4579413 objects/sec	 2539396 B/op	  102756 allocs/op
Benchmark_find16-4                            	212990751	         5.631 ns/op	       0 B/op	       0 allocs/op
Benchmark_findIndex16-4                       	82265368	        13.46 ns/op	       0 B/op	       0 allocs/op
Benchmark_find64-4                            	295319250	         4.060 ns/op	       0 B/op	       0 allocs/op
Benchmark_findIndex64_hit-4                   	296252992	         4.054 ns/op	       0 B/op	       0 allocs/op
Benchmark_findIndex64_miss-4                  	295436700	         4.054 ns/op	       0 B/op	       0 allocs/op
Benchmark_find4-4                             	421298127	         2.852 ns/op	       0 B/op	       0 allocs/op
Benchmark_findIndex4-4                        	319927662	         3.746 ns/op	       0 B/op	       0 allocs/op
BenchmarkSmallWriteTxn/updates_1-4            	  456478	      2552 ns/op	    3529 B/op	       4 allocs/op
BenchmarkSmallWriteTxn/updates_2-4            	  304449	      3577 ns/op	    4742 B/op	       6 allocs/op
BenchmarkSmallWriteTxn/updates_4-4            	  203355	      5723 ns/op	    7155 B/op	      11 allocs/op
BenchmarkSmallWriteTxn/updates_8-4            	  121077	      9531 ns/op	   11927 B/op	      20 allocs/op
BenchmarkSmallWriteTxn/updates_16-4           	   70990	     17179 ns/op	   21289 B/op	      38 allocs/op
PASS
ok  	github.com/cilium/statedb/part	55.271s
PASS
ok  	github.com/cilium/statedb/reconciler	0.005s
?   	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 1.69 seconds (batch size 1000)
Throughput 592933.41 objects per second
522MB total allocated, 6015184 in-use objects, 239MB bytes in use

The deduplication of table names in WriteTxn is costly and we can consider
duplicate tables as a caller bug. Let's cheaply check for duplication in
SortableMutexes.Lock and panic if the same table mutex is seen twice.

BenchmarkDB_NewWriteTxn-6 (arm64, 15 alternating 1s samples):
  time/op    501.7ns -> 453.7ns  (-9.57%, p=0.000)
  bytes/op   216B -> 200B        (-7.41%)
  allocs/op  5 -> 4              (-20.00%)

BenchmarkDB_WriteTxn_1-6 (arm64, 15 alternating 1s samples):
  time/op       939.1ns -> 886.0ns    (-5.65%, p=0.000)
  objects/sec   1.065M -> 1.129M      (+6.00%)
  bytes/op      704B -> 688B          (-2.27%)
  allocs/op     17 -> 16              (-5.88%)

AIL:3
Signed-off-by: Jussi Maki <jussi@isovalent.com>
Remove the transaction duration field and its atomic stores from Commit and
Abort. Nothing reads the field; transaction metrics derive their duration
directly from acquiredAt.

BenchmarkDB_NewWriteTxn (arm64, 12 alternating 1s samples):
  time/op    595.0ns -> 566.0ns  (-4.87%, p=0.014)
  bytes/op   216B -> 216B
  allocs/op  5 -> 5

AIL:3
Signed-off-by: Jussi Maki <jussi@isovalent.com>
Replace the internal SortableMutex interface with the concrete SortableMutex
type. The mutex implementation is internal and has a single implementation,
so dynamic dispatch and interface storage are unnecessary.

Benchmark_SortableMutex-6 (arm64, 15 alternating 1s samples):
  time/op    166.2ns -> 158.4ns  (-4.69%, p=0.000)
  bytes/op   0B -> 0B
  allocs/op  0 -> 0

AIL:3
Signed-off-by: Jussi Maki <jussi@isovalent.com>
@joamaki
joamaki force-pushed the pr/joamaki/txn-misc-optimizations branch from 512cfe6 to 5a1cef7 Compare September 10, 2026 13:13
Co-allocate table entries with exact-sized index backing arrays for tables
with zero through three secondary indexes. This removes the separate index
array allocation while avoiding allocator-class waste for smaller tables.
Tables with more than three secondary indexes retain the general cloning
path.

Compared with the initial six-slot co-allocation, exact sizing changes the
transaction-opening benchmark as follows:

name                                    old time/op  new time/op  delta
DB_NewWriteTxn_SecondaryIndexes/0-6       421.7ns      417.7ns   -0.95%
DB_NewWriteTxn_SecondaryIndexes/1-6       417.5ns      419.3ns      ~
DB_NewWriteTxn_SecondaryIndexes/2-6       418.3ns      422.7ns   +1.05%
DB_NewWriteTxn_SecondaryIndexes/3-6       437.1ns      417.4ns   -4.51%

The zero- and one-secondary-index cases decrease from 216 B/op to 184
and 200 B/op respectively. The two-secondary-index case is unchanged at
216 B/op and 3 allocs/op. The three-secondary-index case stays at 232
B/op while improving from 4 to 3 allocs/op. The initial co-allocation also
reduced transaction opening from 4 to 3 allocs/op for zero through two
secondary indexes.

AIL:3
Signed-off-by: Jussi Maki <jussi@isovalent.com>
Make the first NewKeySet argument non-variadic so the common singleton
case does not need a variadic backing array. Add EmptyKeySet for callers
that need to return no keys and adapt slice-based key producers.

name                                      old time/op  new time/op   delta
DB_WriteTxn_1-6                              857.0ns      841.3ns    -1.83%
DB_WriteTxn_10-6                             371.1ns      350.1ns    -5.66%
DB_WriteTxn_100_SecondaryIndex-6             554.8ns      474.8ns   -14.42%

WriteTxn_1 improves from 14 to 13 allocs/op and 712 to 688 B/op,
WriteTxn_10 from 7 to 6 allocs/op and 379 to 355 B/op, and the secondary
index benchmark from 10 to 7 allocs/op and 491 to 419 B/op. A focused
NewKeySet benchmark improves from 24 B/op and 1 alloc/op to zero.

AIL:3
Signed-off-by: Jussi Maki <jussi@isovalent.com>
@giorio94
giorio94 self-requested a review September 11, 2026 12:52
@giorio94

Copy link
Copy Markdown
Member

Nice! The optimizations seem reasonable to me.

@joamaki
joamaki merged commit 2fbf3fe into main Sep 14, 2026
1 check passed
@joamaki
joamaki deleted the pr/joamaki/txn-misc-optimizations branch September 14, 2026 08:12
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