bazel icon indicating copy to clipboard operation
bazel copied to clipboard

Introduce a better hash function for Starlark

Open meteorcloudy opened this issue 1 year ago • 7 comments

Description of the feature request:

See https://github.com/bazelbuild/bazel/pull/26528#discussion_r2200777193

Which category does this issue belong to?

No response

What underlying problem are you trying to solve with this feature?

No response

Which operating system are you running Bazel on?

No response

What is the output of bazel info release?

No response

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse HEAD ?


Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

meteorcloudy avatar Jul 11 '25 13:07 meteorcloudy

Comment: this would be a fairly big undertaking: for every subclass of StarlarkValue in Bazel (of which we have probably on the order of 150), we'd need to define a guava funnel or equivalent.

Doesn't mean that it's not worth doing, of course.

tetromino avatar Jul 11 '25 23:07 tetromino

Why don't we just define the new hash function only for strings? hash also only accepts strings.

fmeum avatar Jul 12 '25 06:07 fmeum

Why don't we just define the new hash function only for strings? hash also only accepts strings.

Ah, thanks, that certainly makes things much simpler.

tetromino avatar Jul 12 '25 14:07 tetromino

  private static final int FNV_OFFSET_BASIS = 0x811c9dc5;
  private static final int FNV_PRIME = 0x01000193;

  private static int fnv1aHash(String value) {
    int hash = FNV_OFFSET_BASIS;
    for (int i = 0; i < value.length(); i++) {
      hash ^= value.charAt(i);
      hash *= FNV_PRIME;
    }
    return hash;
  }

@fmeum Do you think if replacing value.hashCode() with the above function is enough? (Recommended by AI)

meteorcloudy avatar Jul 14 '25 10:07 meteorcloudy

That is a decent hash function to implement in Starlark "userspace".

I don't think we can modify the behavior of hash at this point and if we introduce a new hash function, we may want to go for something that's guaranteed to have no collisions but is still fast, which pretty much means SHA256. Not sure though.

fmeum avatar Jul 14 '25 10:07 fmeum

Ran the numbers on this since the thread stalled on FNV-1a vs SHA256

on realistic label-like strings the current hash is fine. 400k path-shaped strings, String.hashCode gave 22 collisions, FNV-1a 32-bit gave 16, and a random 32-bit function would give 18.6. both are at the birthday bound, so at that scale the algorithm is not what limits you, the 32 bits are

where hashCode really does fall apart is short strings. across all 140,608 three-letter ASCII strings it produced 100,000 collisions, FNV-1a produced 0. "Aa" and "BB" both hash to 2112, and for short inputs the 31 multiplier never reaches the high bits

so these look like two separate questions rather than one. if the problem is short-string clustering, FNV-1a fixes it completely and costs nothing. if the problem is "guaranteed no collisions", no 32-bit function gets there and it has to be width, which is where SHA256 comes in

soo corpus was file paths from a few hundred checked-out repos, shaped like //dir/sub:file, so it is a proxy for real labels rather than the real thing...

karpovantonme avatar Aug 13 '26 22:08 karpovantonme

Could a 64-bit output be sufficient? If we chose a hash function that's 1) fast and 2) not a SHA family hadh, we could get to the point where no one has to worry about transition without folks being (mis-)led to computing hashes at analysis time.

fmeum avatar Aug 18 '26 20:08 fmeum