Skip to content

Commit

Permalink
Use MapRange() to iterate maps
Browse files Browse the repository at this point in the history
```
       │ master.bench │         fix-mapkeys.bench          │
       │    sec/op    │   sec/op     vs base               │
Map-10    3.598µ ± 1%   3.058µ ± 0%  -15.00% (p=0.002 n=6)

       │ master.bench │          fix-mapkeys.bench          │
       │     B/op     │     B/op      vs base               │
Map-10   1.844Ki ± 0%   1.328Ki ± 0%  -27.97% (p=0.002 n=6)

       │ master.bench │        fix-mapkeys.bench         │
       │  allocs/op   │ allocs/op   vs base              │
Map-10     130.0 ± 0%   128.0 ± 0%  -1.54% (p=0.002 n=6)
```
  • Loading branch information
bep committed Jan 12, 2025
1 parent 1dee3c3 commit d83ece6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.test
7 changes: 5 additions & 2 deletions hashstructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,11 @@ func (w *walker) visit(v reflect.Value, opts *visitOpts) (uint64, error) {
// Build the hash for the map. We do this by XOR-ing all the key
// and value hashes. This makes it deterministic despite ordering.
var h uint64
for _, k := range v.MapKeys() {
v := v.MapIndex(k)

iter := v.MapRange()
for iter.Next() {
k := iter.Key()
v := iter.Value()
if includeMap != nil {
incl, err := includeMap.HashIncludeMap(
opts.StructField, k.Interface(), v.Interface())
Expand Down

0 comments on commit d83ece6

Please sign in to comment.