From 06056908c995749cf63a91b49340cd64eba7be5f Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Sat, 23 Jul 2022 13:35:40 +0900 Subject: [PATCH 01/13] Fixes nan --- Lib/test/test_float.py | 2 -- vm/src/builtins/float.rs | 6 +++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 73e5cab2b..8b61f4feb 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -582,8 +582,6 @@ class GeneralFloatCases(unittest.TestCase): self.assertEqual(hash(float('inf')), sys.hash_info.inf) self.assertEqual(hash(float('-inf')), -sys.hash_info.inf) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_hash_nan(self): value = float('nan') self.assertEqual(hash(value), object.__hash__(value)) diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index ee4837090..0e047d499 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -539,7 +539,11 @@ impl Comparable for PyFloat { impl Hashable for PyFloat { #[inline] fn hash(zelf: &crate::Py, _vm: &VirtualMachine) -> PyResult { - Ok(hash::hash_float(zelf.to_f64())) + if zelf.to_f64().is_nan() { + Ok(zelf.get_id() as hash::PyHash) + } else { + Ok(hash::hash_float(zelf.to_f64())) + } } } From 98d3f38476a784378e3e1730cf4b1efded4bd572 Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Sun, 24 Jul 2022 11:36:07 +0900 Subject: [PATCH 02/13] Reflect feedbacks --- common/src/hash.rs | 31 ++++++++++++++++++++++++------- vm/src/builtins/float.rs | 7 +++---- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/common/src/hash.rs b/common/src/hash.rs index ba3d6048a..6f699e03d 100644 --- a/common/src/hash.rs +++ b/common/src/hash.rs @@ -94,17 +94,18 @@ impl HashSecret { } } -pub fn hash_float(value: f64) -> PyHash { +#[inline] +pub fn hash_float(value: f64) -> Option { // cpython _Py_HashDouble if !value.is_finite() { return if value.is_infinite() { if value > 0.0 { - INF + Some(INF) } else { - -INF + Some(-INF) } } else { - NAN + None }; } @@ -136,12 +137,12 @@ pub fn hash_float(value: f64) -> PyHash { }; x = ((x << e) & MODULUS) | x >> (BITS32 - e); - fix_sentinel(x as PyHash * value.signum() as PyHash) + Some(fix_sentinel(x as PyHash * value.signum() as PyHash)) } pub fn hash_complex(value: &Complex64) -> PyHash { - let re_hash = hash_float(value.re); - let im_hash = hash_float(value.im); + let re_hash = hash_float(value.re).unwrap(); + let im_hash = hash_float(value.im).unwrap(); let Wrapping(ret) = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(IMAG); fix_sentinel(ret) } @@ -192,3 +193,19 @@ pub fn lcg_urandom(mut x: u32, buf: &mut [u8]) { *b = ((x >> 16) & 0xff) as u8; } } + +pub fn hash_pointer_raw(p: *const libc::c_void) -> PyHash { + // TODO: Use commented logic when below issue resolved. + // Ref: https://github.com/RustPython/RustPython/pull/3951#issuecomment-1193108966 + + // let mut y = p as usize; + // /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid + // excessive hash collisions for dicts and sets */ + // y = (y >> 4) | (y << (8 * std::mem::size_of::() - 4)); + // y as PyHash + p as PyHash +} + +pub fn hash_pointer(p: *const libc::c_void) -> PyHash { + fix_sentinel(hash_pointer_raw(p)) +} diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index 0e047d499..dfa932a42 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -539,10 +539,9 @@ impl Comparable for PyFloat { impl Hashable for PyFloat { #[inline] fn hash(zelf: &crate::Py, _vm: &VirtualMachine) -> PyResult { - if zelf.to_f64().is_nan() { - Ok(zelf.get_id() as hash::PyHash) - } else { - Ok(hash::hash_float(zelf.to_f64())) + match hash::hash_float(zelf.to_f64()) { + Some(value) => Ok(value), + None => Ok(hash::hash_pointer(zelf as *const _ as *const libc::c_void)), } } } From 8a97c0b8fa17a41f082aed7b60611a3ef4d9cc2a Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Sun, 24 Jul 2022 12:53:22 +0900 Subject: [PATCH 03/13] `libc::c_void` -> `std::ffi::c_void` --- common/src/hash.rs | 4 ++-- vm/src/builtins/float.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/common/src/hash.rs b/common/src/hash.rs index 6f699e03d..667e4afa9 100644 --- a/common/src/hash.rs +++ b/common/src/hash.rs @@ -194,7 +194,7 @@ pub fn lcg_urandom(mut x: u32, buf: &mut [u8]) { } } -pub fn hash_pointer_raw(p: *const libc::c_void) -> PyHash { +pub fn hash_pointer_raw(p: *const std::ffi::c_void) -> PyHash { // TODO: Use commented logic when below issue resolved. // Ref: https://github.com/RustPython/RustPython/pull/3951#issuecomment-1193108966 @@ -206,6 +206,6 @@ pub fn hash_pointer_raw(p: *const libc::c_void) -> PyHash { p as PyHash } -pub fn hash_pointer(p: *const libc::c_void) -> PyHash { +pub fn hash_pointer(p: *const std::ffi::c_void) -> PyHash { fix_sentinel(hash_pointer_raw(p)) } diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index dfa932a42..0211e042a 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -541,7 +541,7 @@ impl Hashable for PyFloat { fn hash(zelf: &crate::Py, _vm: &VirtualMachine) -> PyResult { match hash::hash_float(zelf.to_f64()) { Some(value) => Ok(value), - None => Ok(hash::hash_pointer(zelf as *const _ as *const libc::c_void)), + None => Ok(hash::hash_pointer(zelf as *const _ as *const std::ffi::c_void)), } } } From d388a131207f964b4cc4955ff79251a86767a198 Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Sun, 24 Jul 2022 13:06:02 +0900 Subject: [PATCH 04/13] Fix `hash_complex`'s hash function --- common/src/hash.rs | 8 ++++---- vm/src/builtins/complex.rs | 7 ++++++- vm/src/builtins/float.rs | 4 +++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/common/src/hash.rs b/common/src/hash.rs index 667e4afa9..2f2f071dc 100644 --- a/common/src/hash.rs +++ b/common/src/hash.rs @@ -140,11 +140,11 @@ pub fn hash_float(value: f64) -> Option { Some(fix_sentinel(x as PyHash * value.signum() as PyHash)) } -pub fn hash_complex(value: &Complex64) -> PyHash { - let re_hash = hash_float(value.re).unwrap(); - let im_hash = hash_float(value.im).unwrap(); +pub fn hash_complex(value: &Complex64) -> Option { + let re_hash = hash_float(value.re)?; + let im_hash = hash_float(value.im)?; let Wrapping(ret) = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(IMAG); - fix_sentinel(ret) + Some(fix_sentinel(ret)) } pub fn hash_iter_unordered<'a, T: 'a, I, F, E>(iter: I, hashf: F) -> Result diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index 333269e4d..642248e1a 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -417,7 +417,12 @@ impl Comparable for PyComplex { impl Hashable for PyComplex { #[inline] fn hash(zelf: &crate::Py, _vm: &VirtualMachine) -> PyResult { - Ok(hash::hash_complex(&zelf.value)) + match hash::hash_complex(&zelf.value) { + Some(value) => Ok(value), + None => Ok(hash::hash_pointer( + zelf as *const _ as *const std::ffi::c_void, + )), + } } } diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index 0211e042a..0262b6933 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -541,7 +541,9 @@ impl Hashable for PyFloat { fn hash(zelf: &crate::Py, _vm: &VirtualMachine) -> PyResult { match hash::hash_float(zelf.to_f64()) { Some(value) => Ok(value), - None => Ok(hash::hash_pointer(zelf as *const _ as *const std::ffi::c_void)), + None => Ok(hash::hash_pointer( + zelf as *const _ as *const std::ffi::c_void, + )), } } } From 3c0f3cacdd452ead2ba2fac9bf288e4a9e69951b Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Sun, 24 Jul 2022 13:07:35 +0900 Subject: [PATCH 05/13] Apply inlining --- common/src/hash.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/common/src/hash.rs b/common/src/hash.rs index 2f2f071dc..f1762c2eb 100644 --- a/common/src/hash.rs +++ b/common/src/hash.rs @@ -140,6 +140,7 @@ pub fn hash_float(value: f64) -> Option { Some(fix_sentinel(x as PyHash * value.signum() as PyHash)) } +#[inline] pub fn hash_complex(value: &Complex64) -> Option { let re_hash = hash_float(value.re)?; let im_hash = hash_float(value.im)?; From 6098eb66ed4604bd655254055437496121b01113 Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Sun, 24 Jul 2022 14:04:34 +0900 Subject: [PATCH 06/13] Fix wrong implementation of `hash_complex` --- common/src/hash.rs | 18 ++++++++++++++---- vm/src/builtins/complex.rs | 7 +------ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/common/src/hash.rs b/common/src/hash.rs index f1762c2eb..3447f9193 100644 --- a/common/src/hash.rs +++ b/common/src/hash.rs @@ -141,11 +141,21 @@ pub fn hash_float(value: f64) -> Option { } #[inline] -pub fn hash_complex(value: &Complex64) -> Option { - let re_hash = hash_float(value.re)?; - let im_hash = hash_float(value.im)?; +pub fn hash_complex(value: &Complex64) -> PyHash { + let re_hash = (match hash_float(value.re) { + Some(value) => Some(value), + None => Some(hash_pointer(value as *const _ as *const std::ffi::c_void)), + }) + .unwrap(); + + let im_hash = (match hash_float(value.im) { + Some(value) => Some(value), + None => Some(hash_pointer(value as *const _ as *const std::ffi::c_void)), + }) + .unwrap(); + let Wrapping(ret) = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(IMAG); - Some(fix_sentinel(ret)) + fix_sentinel(ret) } pub fn hash_iter_unordered<'a, T: 'a, I, F, E>(iter: I, hashf: F) -> Result diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index 642248e1a..333269e4d 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -417,12 +417,7 @@ impl Comparable for PyComplex { impl Hashable for PyComplex { #[inline] fn hash(zelf: &crate::Py, _vm: &VirtualMachine) -> PyResult { - match hash::hash_complex(&zelf.value) { - Some(value) => Ok(value), - None => Ok(hash::hash_pointer( - zelf as *const _ as *const std::ffi::c_void, - )), - } + Ok(hash::hash_complex(&zelf.value)) } } From d317c8b08ce4b67581086bae058aaf1f9ae1a1d5 Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Mon, 25 Jul 2022 17:39:39 +0900 Subject: [PATCH 07/13] Move `hash_complex` implementation from `hash.rs` to `complex.rs` --- common/src/hash.rs | 20 -------------------- vm/src/builtins/complex.rs | 22 +++++++++++++++++++++- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/common/src/hash.rs b/common/src/hash.rs index 3447f9193..d28f8a6ab 100644 --- a/common/src/hash.rs +++ b/common/src/hash.rs @@ -1,10 +1,8 @@ use num_bigint::BigInt; -use num_complex::Complex64; use num_traits::ToPrimitive; use siphasher::sip::SipHasher24; use std::{ hash::{BuildHasher, Hash, Hasher}, - num::Wrapping, }; pub type PyHash = i64; @@ -140,24 +138,6 @@ pub fn hash_float(value: f64) -> Option { Some(fix_sentinel(x as PyHash * value.signum() as PyHash)) } -#[inline] -pub fn hash_complex(value: &Complex64) -> PyHash { - let re_hash = (match hash_float(value.re) { - Some(value) => Some(value), - None => Some(hash_pointer(value as *const _ as *const std::ffi::c_void)), - }) - .unwrap(); - - let im_hash = (match hash_float(value.im) { - Some(value) => Some(value), - None => Some(hash_pointer(value as *const _ as *const std::ffi::c_void)), - }) - .unwrap(); - - let Wrapping(ret) = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(IMAG); - fix_sentinel(ret) -} - pub fn hash_iter_unordered<'a, T: 'a, I, F, E>(iter: I, hashf: F) -> Result where I: IntoIterator, diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index 333269e4d..14e668ebb 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -13,6 +13,7 @@ use crate::{ types::{AsNumber, Comparable, Constructor, Hashable, PyComparisonOp}, AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, }; +use std::num::Wrapping; use num_complex::Complex64; use num_traits::Zero; use rustpython_common::{float_ops, hash}; @@ -417,7 +418,26 @@ impl Comparable for PyComplex { impl Hashable for PyComplex { #[inline] fn hash(zelf: &crate::Py, _vm: &VirtualMachine) -> PyResult { - Ok(hash::hash_complex(&zelf.value)) + let value = zelf.value; + + let re_hash = (match hash::hash_float(value.re) { + Some(value) => Some(value), + None => Some(hash::hash_pointer( + zelf as *const _ as *const std::ffi::c_void, + )), + }) + .unwrap(); + + let im_hash = (match hash::hash_float(value.im) { + Some(value) => Some(value), + None => Some(hash::hash_pointer( + zelf as *const _ as *const std::ffi::c_void, + )), + }) + .unwrap(); + + let Wrapping(ret) = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(hash::IMAG); + Ok(hash::fix_sentinel(ret)) } } From dc3c7a971262ec8106ae06715fbd6854eb06d12f Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Mon, 25 Jul 2022 17:41:39 +0900 Subject: [PATCH 08/13] Fix lint --- common/src/hash.rs | 4 +--- vm/src/builtins/complex.rs | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/common/src/hash.rs b/common/src/hash.rs index d28f8a6ab..c91f1a5eb 100644 --- a/common/src/hash.rs +++ b/common/src/hash.rs @@ -1,9 +1,7 @@ use num_bigint::BigInt; use num_traits::ToPrimitive; use siphasher::sip::SipHasher24; -use std::{ - hash::{BuildHasher, Hash, Hasher}, -}; +use std::hash::{BuildHasher, Hash, Hasher}; pub type PyHash = i64; pub type PyUHash = u64; diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index 14e668ebb..4bbd4eed6 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -13,10 +13,10 @@ use crate::{ types::{AsNumber, Comparable, Constructor, Hashable, PyComparisonOp}, AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, }; -use std::num::Wrapping; use num_complex::Complex64; use num_traits::Zero; use rustpython_common::{float_ops, hash}; +use std::num::Wrapping; /// Create a complex number from a real part and an optional imaginary part. /// From 01a5fa1ba9f8513a15c4b9a2dcaaa33c2f342368 Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Mon, 25 Jul 2022 17:42:29 +0900 Subject: [PATCH 09/13] Reflect feedback --- common/src/hash.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/common/src/hash.rs b/common/src/hash.rs index c91f1a5eb..edfa6f537 100644 --- a/common/src/hash.rs +++ b/common/src/hash.rs @@ -95,11 +95,7 @@ pub fn hash_float(value: f64) -> Option { // cpython _Py_HashDouble if !value.is_finite() { return if value.is_infinite() { - if value > 0.0 { - Some(INF) - } else { - Some(-INF) - } + Some(if value > 0.0 { INF } else { -INF }) } else { None }; From 320265c97310b0151c0fbba75240a415ab219c18 Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Mon, 25 Jul 2022 19:03:41 +0900 Subject: [PATCH 10/13] Reflect feedbacks --- vm/src/builtins/complex.rs | 18 ++++-------------- vm/src/builtins/float.rs | 8 ++------ 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index 4bbd4eed6..49d89b620 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -420,21 +420,11 @@ impl Hashable for PyComplex { fn hash(zelf: &crate::Py, _vm: &VirtualMachine) -> PyResult { let value = zelf.value; - let re_hash = (match hash::hash_float(value.re) { - Some(value) => Some(value), - None => Some(hash::hash_pointer( - zelf as *const _ as *const std::ffi::c_void, - )), - }) - .unwrap(); + let re_hash = hash::hash_float(value.re) + .unwrap_or_else(|| hash::hash_pointer(zelf as *const _ as *const std::ffi::c_void)); - let im_hash = (match hash::hash_float(value.im) { - Some(value) => Some(value), - None => Some(hash::hash_pointer( - zelf as *const _ as *const std::ffi::c_void, - )), - }) - .unwrap(); + let im_hash = hash::hash_float(value.im) + .unwrap_or_else(|| hash::hash_pointer(zelf as *const _ as *const std::ffi::c_void)); let Wrapping(ret) = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(hash::IMAG); Ok(hash::fix_sentinel(ret)) diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index 0262b6933..e7951f575 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -539,12 +539,8 @@ impl Comparable for PyFloat { impl Hashable for PyFloat { #[inline] fn hash(zelf: &crate::Py, _vm: &VirtualMachine) -> PyResult { - match hash::hash_float(zelf.to_f64()) { - Some(value) => Ok(value), - None => Ok(hash::hash_pointer( - zelf as *const _ as *const std::ffi::c_void, - )), - } + Ok(hash::hash_float(zelf.to_f64()) + .unwrap_or_else(|| hash::hash_pointer(zelf as *const _ as *const libc::c_void))) } } From 187fc5d872128fecc680087c3d8e3088d948fcdc Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Tue, 26 Jul 2022 11:42:25 +0900 Subject: [PATCH 11/13] `hash_pointer` -> `hash_object_id` --- common/src/hash.rs | 8 ++++---- vm/src/builtins/complex.rs | 8 ++++---- vm/src/builtins/float.rs | 3 +-- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/common/src/hash.rs b/common/src/hash.rs index edfa6f537..c9c76fa25 100644 --- a/common/src/hash.rs +++ b/common/src/hash.rs @@ -179,11 +179,11 @@ pub fn lcg_urandom(mut x: u32, buf: &mut [u8]) { } } -pub fn hash_pointer_raw(p: *const std::ffi::c_void) -> PyHash { +pub fn hash_object_id_raw(p: usize) -> PyHash { // TODO: Use commented logic when below issue resolved. // Ref: https://github.com/RustPython/RustPython/pull/3951#issuecomment-1193108966 - // let mut y = p as usize; + // let mut y = p; // /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid // excessive hash collisions for dicts and sets */ // y = (y >> 4) | (y << (8 * std::mem::size_of::() - 4)); @@ -191,6 +191,6 @@ pub fn hash_pointer_raw(p: *const std::ffi::c_void) -> PyHash { p as PyHash } -pub fn hash_pointer(p: *const std::ffi::c_void) -> PyHash { - fix_sentinel(hash_pointer_raw(p)) +pub fn hash_object_id(p: usize) -> PyHash { + fix_sentinel(hash_object_id_raw(p)) } diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index 49d89b620..97b31bcf1 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -420,11 +420,11 @@ impl Hashable for PyComplex { fn hash(zelf: &crate::Py, _vm: &VirtualMachine) -> PyResult { let value = zelf.value; - let re_hash = hash::hash_float(value.re) - .unwrap_or_else(|| hash::hash_pointer(zelf as *const _ as *const std::ffi::c_void)); + let re_hash = + hash::hash_float(value.re).unwrap_or_else(|| hash::hash_object_id(zelf.get_id())); - let im_hash = hash::hash_float(value.im) - .unwrap_or_else(|| hash::hash_pointer(zelf as *const _ as *const std::ffi::c_void)); + let im_hash = + hash::hash_float(value.im).unwrap_or_else(|| hash::hash_object_id(zelf.get_id())); let Wrapping(ret) = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(hash::IMAG); Ok(hash::fix_sentinel(ret)) diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index e7951f575..2584b5209 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -539,8 +539,7 @@ impl Comparable for PyFloat { impl Hashable for PyFloat { #[inline] fn hash(zelf: &crate::Py, _vm: &VirtualMachine) -> PyResult { - Ok(hash::hash_float(zelf.to_f64()) - .unwrap_or_else(|| hash::hash_pointer(zelf as *const _ as *const libc::c_void))) + Ok(hash::hash_float(zelf.to_f64()).unwrap_or_else(|| hash::hash_object_id(zelf.get_id()))) } } From 60c4db78dde3f96e6dc4caf4050ece6de19773b4 Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Wed, 27 Jul 2022 13:36:15 +0900 Subject: [PATCH 12/13] Reflect feedbacks --- common/src/hash.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/common/src/hash.rs b/common/src/hash.rs index c9c76fa25..c836741d3 100644 --- a/common/src/hash.rs +++ b/common/src/hash.rs @@ -179,18 +179,18 @@ pub fn lcg_urandom(mut x: u32, buf: &mut [u8]) { } } +#[inline] pub fn hash_object_id_raw(p: usize) -> PyHash { // TODO: Use commented logic when below issue resolved. // Ref: https://github.com/RustPython/RustPython/pull/3951#issuecomment-1193108966 - // let mut y = p; - // /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid - // excessive hash collisions for dicts and sets */ - // y = (y >> 4) | (y << (8 * std::mem::size_of::() - 4)); - // y as PyHash + /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid + excessive hash collisions for dicts and sets */ + // p.rotate_right(4) as PyHash p as PyHash } +#[inline] pub fn hash_object_id(p: usize) -> PyHash { fix_sentinel(hash_object_id_raw(p)) } From d7f17d82ec86c3d80d077f73ba666cf415954429 Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Wed, 27 Jul 2022 13:37:34 +0900 Subject: [PATCH 13/13] Fix formatter --- common/src/hash.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/hash.rs b/common/src/hash.rs index c836741d3..708a633cf 100644 --- a/common/src/hash.rs +++ b/common/src/hash.rs @@ -185,7 +185,7 @@ pub fn hash_object_id_raw(p: usize) -> PyHash { // Ref: https://github.com/RustPython/RustPython/pull/3951#issuecomment-1193108966 /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid - excessive hash collisions for dicts and sets */ + excessive hash collisions for dicts and sets */ // p.rotate_right(4) as PyHash p as PyHash }