-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
PERF: using murmur hash for float64 khash-tables #36729
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
498a4c5
adding asv-benchmark hash_functions
realead 937c9ef
using murmur2 for probing step
realead 3c32be6
[PERF] using murmur2 hash for float64 klib-hash-tables
realead 450268b
fixing test cases: because the values were the same, the order was no…
realead 641eda1
fixing doctest: the order of 1,4,2 depends on hash and is not unique
realead 309c9d6
adding whatsnew-note
realead f249638
requested doc changes
realead e743c79
use Series rather than pd.Series
realead File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import numpy as np | ||
|
||
import pandas as pd | ||
|
||
|
||
class IsinAlmostFullWithRandomInt: | ||
params = [ | ||
[np.float64, np.int64, np.uint64, np.object], | ||
range(10, 21), | ||
] | ||
param_names = ["dtype", "exponent"] | ||
|
||
def setup(self, dtype, exponent): | ||
M = 3 * 2 ** (exponent - 2) | ||
# 0.77-the maximal share of occupied buckets | ||
np.random.seed(42) | ||
self.s = pd.Series(np.random.randint(0, M, M)).astype(dtype) | ||
self.values = np.random.randint(0, M, M).astype(dtype) | ||
self.values_outside = self.values + M | ||
|
||
def time_isin(self, dtype, exponent): | ||
self.s.isin(self.values) | ||
|
||
def time_isin_outside(self, dtype, exponent): | ||
self.s.isin(self.values_outside) | ||
|
||
|
||
class IsinWithRandomFloat: | ||
params = [ | ||
[np.float64, np.object], | ||
[ | ||
1_300, | ||
2_000, | ||
7_000, | ||
8_000, | ||
70_000, | ||
80_000, | ||
750_000, | ||
900_000, | ||
], | ||
] | ||
param_names = ["dtype", "M"] | ||
|
||
def setup(self, dtype, M): | ||
np.random.seed(42) | ||
self.values = np.random.rand(M) | ||
self.s = pd.Series(self.values).astype(dtype) | ||
np.random.shuffle(self.values) | ||
self.values_outside = self.values + 0.1 | ||
|
||
def time_isin(self, dtype, M): | ||
self.s.isin(self.values) | ||
|
||
def time_isin_outside(self, dtype, M): | ||
self.s.isin(self.values_outside) | ||
|
||
|
||
class IsinWithArangeSorted: | ||
params = [ | ||
[np.float64, np.int64, np.uint64, np.object], | ||
[ | ||
1_000, | ||
2_000, | ||
8_000, | ||
100_000, | ||
1_000_000, | ||
], | ||
] | ||
param_names = ["dtype", "M"] | ||
|
||
def setup(self, dtype, M): | ||
self.s = pd.Series(np.arange(M)).astype(dtype) | ||
self.values = np.arange(M).astype(dtype) | ||
|
||
def time_isin(self, dtype, M): | ||
self.s.isin(self.values) | ||
|
||
|
||
class IsinWithArange: | ||
params = [ | ||
[np.float64, np.int64, np.uint64, np.object], | ||
[ | ||
1_000, | ||
2_000, | ||
8_000, | ||
], | ||
[-2, 0, 2], | ||
] | ||
param_names = ["dtype", "M", "offset_factor"] | ||
|
||
def setup(self, dtype, M, offset_factor): | ||
offset = int(M * offset_factor) | ||
np.random.seed(42) | ||
tmp = pd.Series(np.random.randint(offset, M + offset, 10 ** 6)) | ||
self.s = tmp.astype(dtype) | ||
self.values = np.arange(M).astype(dtype) | ||
|
||
def time_isin(self, dtype, M, offset_factor): | ||
self.s.isin(self.values) | ||
|
||
|
||
class Float64GroupIndex: | ||
# GH28303 | ||
def setup(self): | ||
self.df = pd.date_range( | ||
start="1/1/2018", end="1/2/2018", periods=1e6 | ||
).to_frame() | ||
self.group_index = np.round(self.df.index.astype(int) / 1e9) | ||
|
||
def time_groupby(self): | ||
self.df.groupby(self.group_index).last() | ||
|
||
|
||
class UniqueAndFactorizeArange: | ||
params = range(4, 16) | ||
param_names = ["exponent"] | ||
|
||
def setup(self, exponent): | ||
a = np.arange(10 ** 4, dtype="float64") | ||
self.a2 = (a + 10 ** exponent).repeat(100) | ||
|
||
def time_factorize(self, exponent): | ||
pd.factorize(self.a2) | ||
|
||
def time_unique(self, exponent): | ||
pd.unique(self.a2) | ||
|
||
|
||
class NumericSeriesIndexing: | ||
|
||
params = [ | ||
(pd.Int64Index, pd.UInt64Index, pd.Float64Index), | ||
(10 ** 4, 10 ** 5, 5 * 10 ** 5, 10 ** 6, 5 * 10 ** 6), | ||
] | ||
param_names = ["index_dtype", "N"] | ||
|
||
def setup(self, index, N): | ||
vals = np.array(list(range(55)) + [54] + list(range(55, N - 1))) | ||
indices = index(vals) | ||
self.data = pd.Series(np.arange(N), index=indices) | ||
|
||
def time_loc_slice(self, index, N): | ||
# trigger building of mapping | ||
self.data.loc[:800] | ||
|
||
|
||
class NumericSeriesIndexingShuffled: | ||
|
||
params = [ | ||
(pd.Int64Index, pd.UInt64Index, pd.Float64Index), | ||
(10 ** 4, 10 ** 5, 5 * 10 ** 5, 10 ** 6, 5 * 10 ** 6), | ||
] | ||
param_names = ["index_dtype", "N"] | ||
|
||
def setup(self, index, N): | ||
vals = np.array(list(range(55)) + [54] + list(range(55, N - 1))) | ||
np.random.seed(42) | ||
np.random.shuffle(vals) | ||
indices = index(vals) | ||
self.data = pd.Series(np.arange(N), index=indices) | ||
|
||
def time_loc_slice(self, index, N): | ||
# trigger building of mapping | ||
self.data.loc[:800] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,25 +13,31 @@ | |
// is 64 bits the truncation causes collission issues. Given all that, we use our own | ||
// simple hash, viewing the double bytes as an int64 and using khash's default | ||
// hash for 64 bit integers. | ||
// GH 13436 | ||
// GH 13436 showed that _Py_HashDouble doesn't work well with khash | ||
// GH 28303 showed, that the simple xoring-version isn't good enough | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a tiny bit more content here on why this appropach vs the CPython appropach. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was a late comment, pls update in a followon. |
||
// See GH 36729 for evaluation of the currently used murmur2-hash version | ||
|
||
khint64_t PANDAS_INLINE asint64(double key) { | ||
khint64_t val; | ||
memcpy(&val, &key, sizeof(double)); | ||
return val; | ||
khint64_t val; | ||
memcpy(&val, &key, sizeof(double)); | ||
return val; | ||
} | ||
|
||
// correct for all inputs but not -0.0 and NaNs | ||
#define kh_float64_hash_func_0_NAN(key) (khint32_t)((asint64(key))>>33^(asint64(key))^(asint64(key))<<11) | ||
|
||
// correct for all inputs but not NaNs | ||
#define kh_float64_hash_func_NAN(key) ((key) == 0.0 ? \ | ||
kh_float64_hash_func_0_NAN(0.0) : \ | ||
kh_float64_hash_func_0_NAN(key)) | ||
#define ZERO_HASH 0 | ||
#define NAN_HASH 0 | ||
|
||
// correct for all | ||
#define kh_float64_hash_func(key) ((key) != (key) ? \ | ||
kh_float64_hash_func_NAN(Py_NAN) : \ | ||
kh_float64_hash_func_NAN(key)) | ||
khint32_t PANDAS_INLINE kh_float64_hash_func(double val){ | ||
// 0.0 and -0.0 should have the same hash: | ||
if (val == 0.0){ | ||
return ZERO_HASH; | ||
} | ||
// all nans should have the same hash: | ||
if ( val!=val ){ | ||
return NAN_HASH; | ||
} | ||
khint64_t as_int = asint64(val); | ||
return murmur2_64to32(as_int); | ||
} | ||
|
||
#define kh_float64_hash_equal(a, b) ((a) == (b) || ((b) != (b) && (a) != (a))) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.