-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Number normalization for Crystal::Hasher #5276
Conversation
/cc @funny-falcon @RX14 @asterite P.S.: price of hash normalization is minimal: i8-32 has no price, i64/128 has one div, floats need one frexp op (but float32/float64 use fast integer IEEE754 path). No price for others, |
43c6611
to
4dfa15a
Compare
src/crystal/hasher.cr
Outdated
|
||
private HASH_BITS = 61 | ||
private HASH_MODULUS = (1_i64 << HASH_BITS) - 1 | ||
private HASH_NAN = 0_u64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it a formatting issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it is aligned with _
of below 314159_u64
. I think it is expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO it should should have single space after =
.
@akzhan Roger, I'll try to do this soon. |
As declared by Crystal language reference, 1i32.hash should equal to 1f64.hash. Extracted from crystal-lang#4675, also replaces crystal-lang#4581.
a6fc65f
to
c46503e
Compare
spec/std/crystal/hasher_spec.cr
Outdated
@@ -51,6 +52,15 @@ describe "Crystal::Hasher" do | |||
2.hash.should eq(2_u64.hash) | |||
end | |||
|
|||
it "Big i64 numbers should be hashed ok" do | |||
Int64::MAX.hash.should eq (Int64::MAX.hash) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove spaces between eq
and the value that is expected to be equal to.
spec/std/crystal/hasher_spec.cr
Outdated
end | ||
|
||
pending "128bit types should be hashed ok" do | ||
1.to_i128.hash.should eq (1_i8.hash) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above. Please remove spaces between eq
and the arguments given to it.
Hello @akzhan, Thank you for the PR! I read the link you provided about Thank you. |
Hello @luislavena - Crystal language reference was my starting point. There is definition:
Current Crystal (any version) doesn't follow this definition ( Nice point how it is in Python: https://github.com/python/cpython/blob/master/Python/pyhash.c#L1 P.S.: spacing fixed. |
Is it GTG? |
src/big/big_float.cr
Outdated
# :nodoc: | ||
struct Crystal::Hasher | ||
def float(value : BigFloat) | ||
permute(float_normalize_wrap(value) do |value| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please assign the result of the float_normalize_wrap
to a variable which is passed to permute
. Having a multi-line block inside a method call arguments is just bad style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@akzhan You missed that one ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sija Thanks!
src/crystal/hasher.cr
Outdated
end | ||
|
||
def float(value : Float32) | ||
permute(float_normalize_wrap(value) do |value| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
src/crystal/hasher.cr
Outdated
end | ||
|
||
def float(value : Float64) | ||
permute(float_normalize_wrap(value) do |value| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
src/crystal/hasher.cr
Outdated
end | ||
|
||
def float(value : Float) | ||
permute(float_normalize_wrap(value) do |value| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
Apart from that, I think this is gtg. |
Changes applied |
I know that few of team known this area, but please merge it. It's required to get Crystal stable in terms of production use. may be @mverzilli ? P.S.: It is part of Hashing #4675. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All my comments have been addressed. 👍
Thanks @akzhan! |
Conditionally run this on 64bits platforms. Ref #5276
Conditionally run this on 64bits platforms. Ref crystal-lang#5276
As declared by Crystal language reference,
1_i32.hash
should be equal to1_f64.hash
.With this Pull request it's true.
Extracted from #4675, also replaces #4581.
Highly inspired by Python hashes, and has great optimizations for some cases by @funny-falcon.