Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add util.HasherMap #7363

Merged
merged 1 commit into from
Feb 14, 2025
Merged

Add util.HasherMap #7363

merged 1 commit into from
Feb 14, 2025

Conversation

anderseknert
Copy link
Member

This is a simpler version of util.TypedHashMap where the keys implement a .Hash() method and as such won't need one to be passed in, and where the values are largely ignored by the map. These maps are smaller / more performant, but most importantly, they are nicer to work with.

Perf wise, this saves about 600k+ allocs and 40 MB allocated memory in regal lint bundle:

1207614875 ns/op	3293454016 B/op	64802095 allocs/op
1197978125 ns/op	3256960504 B/op	64164871 allocs/op

Also:

  • Use strings.Builder instead of fmt.Sprintf in one location
  • Remove ValueMap.Copy as it was only used in a test

nil,
ruleHash,
nil,
nil,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the one place we can't use HasherMap, as Rule doesn't implement Hash. A good reminder however of why the HasherMap is nicer to work with :)

This is a simpler version of util.TypedHashMap where the keys
implement a `.Hash()` method and as such won't need one to be passed
in, and where the values are largely ignored by the map. These maps
are smaller / more performant, but most importantly, they are nicer
to work with.

Perf wise, this saves about 600k+ allocs and 40 MB allocated memory
in `regal lint bundle`:
```
1207614875 ns/op	3293454016 B/op	64802095 allocs/op
1197978125 ns/op	3256960504 B/op	64164871 allocs/op
```

Also:
- Use `strings.Builder` instead of `fmt.Sprintf` in one location
- Remove `ValueMap.Copy` as it was only used in a test

Signed-off-by: Anders Eknert <anders@styra.com>
@anderseknert anderseknert merged commit 3d7fc9f into open-policy-agent:main Feb 14, 2025
28 checks passed
@anderseknert anderseknert deleted the perf branch February 14, 2025 13:00
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