From 26d4332745c00f85f30a9c6a535436f4b759cb57 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 3 Apr 2024 20:58:58 +0200 Subject: [PATCH] Use map[T]struct{} instead of map[T]bool for internal set types --- fuzz_test.go | 26 +++++++++++--------------- reconciler/example/ops.go | 10 +++++----- table.go | 8 ++++---- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/fuzz_test.go b/fuzz_test.go index 8e4bf60..bc02843 100644 --- a/fuzz_test.go +++ b/fuzz_test.go @@ -116,16 +116,16 @@ func (a *realActionLog) validate(db *statedb.DB, t *testing.T) { defer a.Unlock() // Collapse the log down to objects that are alive at the end. - alive := map[statedb.Table[fuzzObj]]map[uint64]bool{} + alive := map[statedb.Table[fuzzObj]]map[uint64]struct{}{} for _, e := range a.log { aliveThis, ok := alive[e.table] if !ok { - aliveThis = map[uint64]bool{} + aliveThis = map[uint64]struct{}{} alive[e.table] = aliveThis } switch e.act { case actInsert: - aliveThis[e.id] = true + aliveThis[e.id] = struct{}{} case actDelete: delete(aliveThis, e.id) case actDeleteAll: @@ -136,31 +136,27 @@ func (a *realActionLog) validate(db *statedb.DB, t *testing.T) { for table, expected := range alive { txn := db.ReadTxn() iter, _ := table.All(txn) - actual := map[uint64]bool{} + actual := map[uint64]struct{}{} for obj, _, ok := iter.Next(); ok; obj, _, ok = iter.Next() { - actual[obj.id] = true + actual[obj.id] = struct{}{} } require.Equal(t, expected, actual, "validate failed, mismatching ids: %v", setSymmetricDifference(actual, expected)) } } -func setSymmetricDifference[T comparable, M map[T]bool](s1, s2 M) M { +func setSymmetricDifference[T comparable, M map[T]struct{}](s1, s2 M) M { counts := make(map[T]int, len(s1)+len(s2)) - for k1, v1 := range s1 { - if v1 { - counts[k1] = 1 - } + for k1 := range s1 { + counts[k1] = 1 } - for k2, v2 := range s2 { - if v2 { - counts[k2]++ - } + for k2 := range s2 { + counts[k2]++ } result := M{} for k, count := range counts { if count == 1 { - result[k] = true + result[k] = struct{}{} } } return result diff --git a/reconciler/example/ops.go b/reconciler/example/ops.go index 3eb20e2..9f77dd2 100644 --- a/reconciler/example/ops.go +++ b/reconciler/example/ops.go @@ -44,19 +44,19 @@ func (ops *MemoOps) Delete(ctx context.Context, txn statedb.ReadTxn, memo *Memo) // Prune unexpected memos. func (ops *MemoOps) Prune(ctx context.Context, txn statedb.ReadTxn, iter statedb.Iterator[*Memo]) error { - expected := map[string]bool{} + expected := map[string]struct{}{} for memo, _, ok := iter.Next(); ok; memo, _, ok = iter.Next() { - expected[memo.Name] = true + expected[memo.Name] = struct{}{} } // Find unexpected files - unexpected := map[string]bool{} + unexpected := map[string]struct{}{} if entries, err := os.ReadDir(ops.directory); err != nil { return err } else { for _, entry := range entries { - if !expected[entry.Name()] { - unexpected[entry.Name()] = true + if _, ok := expected[entry.Name()]; !ok { + unexpected[entry.Name()] = struct{}{} } } } diff --git a/table.go b/table.go index 43d3cdc..8149137 100644 --- a/table.go +++ b/table.go @@ -71,13 +71,13 @@ func NewTable[Obj any]( } // Validate that indexes have unique ids. - indexNames := map[string]bool{} - indexNames[primaryIndexer.indexName()] = true + indexNames := map[string]struct{}{} + indexNames[primaryIndexer.indexName()] = struct{}{} for _, indexer := range secondaryIndexers { - if indexNames[indexer.indexName()] { + if _, ok := indexNames[indexer.indexName()]; ok { return nil, tableError(tableName, fmt.Errorf("index %q: %w", indexer.indexName(), ErrDuplicateIndex)) } - indexNames[indexer.indexName()] = true + indexNames[indexer.indexName()] = struct{}{} } for name := range indexNames { if strings.HasPrefix(name, reservedIndexPrefix) {