-
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 2 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 (long i = 0; i <= widthInLong; 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. actually we should use int instead of long for i. the reason is that JIT would inject a safepoint for loops over longs which prevents loop unrolling and incurs an extra check for every iteration
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. Addressed the above comment. |
||
| 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 (long i = SIZE_OF_LONG * widthInLong; i < bitSetWidthInBytes; i++) { | ||
| 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.