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

x/tools/go/types/typeutil: gut Hasher #69407

Open
adonovan opened this issue Sep 11, 2024 · 4 comments
Open

x/tools/go/types/typeutil: gut Hasher #69407

adonovan opened this issue Sep 11, 2024 · 4 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented Sep 11, 2024

Thinking about #67161, I wanted to know whether the the new API should include Hasher. It complicates the API because it turns reads (lookups in typeutil.Map) into writes (memoization of hash values). Is it an effective optimization, or does it increase memory contention (and potentially the need for locking)?

I gutted its implementation, removing both memoization maps, and simplifying the implementation of Hasher.hashPtr to just reflect.ValueOf(ptr).Pointer() (which assumes a non-moving GC) and Hasher.hashTypeParam to a hash of t.Index(), making Hasher a stateless empty struct. Then I ran BenchmarkRTA, modified to analyze all of gopls, not just net/http.

The results:

before
200805483 ns/op
203918225
206312775
207155800
209421367
219960417

after
201373042
203830142
204368642
208747200
206170792
208873431

N	min		max		sum		mean		stddev
6	2.00805e+08	2.1996e+08	1.24757e+09	2.07929e+08	6.58822e+06 // before
6	2.01373e+08	2.08873e+08	1.23336e+09	2.05561e+08	2.94797e+06 // after

It is slightly faster and has lower variance. Whatever problems a stateful Hasher once solved, we have no further need for it.

Let's make it an empty struct, and disregard it when considering #67161.

@timothy-king @findleyr

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Sep 11, 2024
@gopherbot gopherbot added this to the Unreleased milestone Sep 11, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/612496 mentions this issue: go/types/typeutil: make Hasher stateless

@timothy-king
Copy link
Contributor

timothy-king commented Sep 11, 2024

#54670 was accepted. Once it is available, can we use that?

@timothy-king timothy-king added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 11, 2024
@findleyr
Copy link
Contributor

@timothy-king do you mean to use Comparable for hashPtr, rather than relying on a non-moving GC?

@adonovan
Copy link
Member Author

adonovan commented Sep 11, 2024

@timothy-king do you mean to use Comparable for hashPtr, rather than relying on a non-moving GC?

maphash.Comparable does the same thing as our code (reflect.Value.Pointer). The only difference is that, being std code, it is morally permitted to assume a non-moving GC, but once the updated #67161 lands, we can use types.Hash, which will have equal moral license.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants