diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | lib/binstruct/structs.go | 27 |
3 files changed, 15 insertions, 16 deletions
@@ -54,7 +54,7 @@ go-mod-tidy: go-mod-tidy: go-mod-tidy/main go-mod-tidy/main: rm -f go.sum - go mod tidy -go $(goversion) -compat $(goversion) + go mod tidy -go 1.20 -compat $(goversion) .PHONY: go-mod-tidy/main go-mod-tidy: $(patsubst tools/src/%/go.mod,go-mod-tidy/tools/%,$(wildcard tools/src/*/go.mod)) @@ -4,7 +4,7 @@ module git.lukeshu.com/btrfs-progs-ng -go 1.19 +go 1.20 require ( git.lukeshu.com/go/lowmemjson v0.3.4 diff --git a/lib/binstruct/structs.go b/lib/binstruct/structs.go index 7eea600..52e5406 100644 --- a/lib/binstruct/structs.go +++ b/lib/binstruct/structs.go @@ -10,6 +10,8 @@ import ( "strconv" "strings" + "git.lukeshu.com/go/typedsync" + "git.lukeshu.com/btrfs-progs-ng/lib/binstruct/binutil" ) @@ -178,21 +180,18 @@ func genStructHandler(structInfo reflect.Type) (structHandler, error) { return ret, nil } -var structCache = make(map[reflect.Type]structHandler) +var structCache typedsync.CacheMap[reflect.Type, structHandler] func getStructHandler(typ reflect.Type) structHandler { - h, ok := structCache[typ] - if ok { + ret, _ := structCache.LoadOrCompute(typ, func(typ reflect.Type) structHandler { + h, err := genStructHandler(typ) + if err != nil { + panic(&InvalidTypeError{ + Type: typ, + Err: err, + }) + } return h - } - - h, err := genStructHandler(typ) - if err != nil { - panic(&InvalidTypeError{ - Type: typ, - Err: err, - }) - } - structCache[typ] = h - return h + }) + return ret } |