-
Notifications
You must be signed in to change notification settings - Fork 26
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
Hashes module produces different results in C and JS. #423
Comments
yeah, it probably should be. I ended up using this in my program: {.push overflowChecks: off.}
proc customHash*(x: string): int32 =
## JS/C backend-compatible hashing
##
## Initially based on https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0
## but JS strings are UTF16...
for i in 0 ..< x.len:
result = (31*result + ord(x[i]).int32) or 0
# echo(result, " ", x[i], " ", ord(x[i]))
{.pop.} |
How did you hash floats? |
Haven't needed that so far, but if you search around for a JS implementation you should be able to find something :) |
It's especially bizarre that hashing a float does not return an int! Moreover, it is not that good of a hash if |
@andreaferretti This |
I wonder what is the rationale for this |
What are your use-cases to have the same hashes in C and JS (and I guess the VM)? |
@andreaferretti Normalizing |
@mratsim I want to make sure that the data structure I am synchronizing is the same on the server and browser. I wanted to hash both sides and if hash is different there is a desync bug some place. |
The problem for the different Hash results is Here: |
@treeform That is a bad idea because then the details are tied to a very specific unspecificied hash algorithm that the stdlib happens to use. |
Running this code produces different results in C and JS:
in C:
in JS:
Should hashing be consistent across compiler backends?
The text was updated successfully, but these errors were encountered: