state: fix redundant reconciliation in typeDecodeDatabase.Lookup()

Simple microbenchmark for save+load of a pgalloc.memAcctSet:
Before this CL:

```
goos: linux
goarch: amd64
pkg: pkg/sentry/pgalloc/pgalloc
cpu: Intel(R) Xeon(R) CPU @ 2.60GHz
BenchmarkMemAcctSetSaveLoad
BenchmarkMemAcctSetSaveLoad/1000
BenchmarkMemAcctSetSaveLoad/1000-48                  362           3313192 ns/op
BenchmarkMemAcctSetSaveLoad/10000
BenchmarkMemAcctSetSaveLoad/10000-48                  33          35237918 ns/op
BenchmarkMemAcctSetSaveLoad/100000
BenchmarkMemAcctSetSaveLoad/100000-48                  3         360797304 ns/op
PASS
```

After this CL:

```
goos: linux
goarch: amd64
pkg: pkg/sentry/pgalloc/pgalloc
cpu: Intel(R) Xeon(R) CPU @ 2.60GHz
BenchmarkMemAcctSetSaveLoad
BenchmarkMemAcctSetSaveLoad/1000
BenchmarkMemAcctSetSaveLoad/1000-48                  417           2837200 ns/op
BenchmarkMemAcctSetSaveLoad/10000
BenchmarkMemAcctSetSaveLoad/10000-48                  38          30813388 ns/op
BenchmarkMemAcctSetSaveLoad/100000
BenchmarkMemAcctSetSaveLoad/100000-48                  4         311275972 ns/op
PASS
```
PiperOrigin-RevId: 663494805
This commit is contained in:
Jamie Liu
2024-08-15 16:31:39 -07:00
committed by gVisor bot
parent 3b28deddf4
commit 04b2c4631d
+1 -1
View File
@@ -204,7 +204,7 @@ var singleFieldOrder = []int{0}
//
// This method never returns nil.
func (tbd *typeDecodeDatabase) Lookup(id typeID, typ reflect.Type) *reconciledTypeEntry {
if len(tbd.byID) > int(id) && tbd.byID[id-1] != nil {
if len(tbd.byID) >= int(id) && tbd.byID[id-1] != nil {
// Already reconciled.
return tbd.byID[id-1]
}