-
Notifications
You must be signed in to change notification settings - Fork 29k
SPARK-7450 Use UNSAFE.getLong() to speed up BitSetMethods#anySet() #5897
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
Changes from 3 commits
3e9b691
4ca0ef6
63ee050
093b7a4
855374b
75a467b
817e3f9
83f9f87
b51dcaf
1719c5b
473bf9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |
| public final class BitSetMethods { | ||
|
|
||
| private static final long WORD_SIZE = 8; | ||
| private static final int SIZE_OF_LONG = Long.SIZE; | ||
|
|
||
| private BitSetMethods() { | ||
| // Make the default constructor private, since this only holds static methods. | ||
|
|
@@ -71,7 +72,13 @@ public static boolean isSet(Object baseObject, long baseOffset, int index) { | |
| * Returns {@code true} if any bit is set. | ||
| */ | ||
| public static boolean anySet(Object baseObject, long baseOffset, long bitSetWidthInBytes) { | ||
| for (int i = 0; i <= bitSetWidthInBytes; i++) { | ||
| long widthInLong = bitSetWidthInBytes / SIZE_OF_LONG; | ||
|
Contributor
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. width needs to be an int too :) |
||
| for (int i = 0; i <= widthInLong; i++) { | ||
| if (PlatformDependent.UNSAFE.getLong(baseObject, baseOffset + i) != 0) { | ||
|
Contributor
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. I don't think that this is correct, since Reynold pointed out that there might be faster micro-optimizations tricks that can be played here; see https://groups.google.com/d/msg/mechanical-sympathy/k0qd7dLHFQE/gsTFSjP6MVMJ |
||
| return true; | ||
| } | ||
| } | ||
| for (int i = (int)(SIZE_OF_LONG * widthInLong); i < bitSetWidthInBytes; i++) { | ||
|
Contributor
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. It looks like this block now contains two
Contributor
Author
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. I don't think so.
Contributor
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. Ah, sorry for overlooking that. I guess I was thinking of optimizing for the case where the bitset width was a multiple of the word size.
Contributor
Author
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. That case is covered by the first loop.
Contributor
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. do we need the 2nd loop? I think the assumption is that all bitsets are word-aligned. We should definitely document that though. |
||
| if (PlatformDependent.UNSAFE.getByte(baseObject, baseOffset + i) != 0) { | ||
| return true; | ||
| } | ||
|
|
||
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.
Here, bitSetWidthInBytes should be changed to bitSetWidthInWords and the rest of the code should be updated accordingly.