Skip to content

Commit 84be796

Browse files
committed
Update to the newer and faster variant of the code
1 parent 1f72d20 commit 84be796

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

src/main/java/org/truffleruby/core/rope/LazyIntRope.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,20 @@ protected LazyIntRope(int value, Encoding encoding, int length) {
4040
9999999,
4141
99999999,
4242
999999999,
43-
2147483647 };
43+
Integer.MAX_VALUE };
4444

45+
// From https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/master/2021/05/28/digitcount.java
4546
private static int length(int value) {
46-
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, value == 0)) {
47-
return 1;
48-
}
49-
5047
final int sign;
51-
5248
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, value < 0)) {
5349
sign = 1;
5450
value = -value;
5551
} else {
5652
sign = 0;
5753
}
5854

59-
final int bits = 31 - Integer.numberOfLeadingZeros(value);
60-
int digits = ((77 * bits) >>> 8);
55+
final int bits = 31 - Integer.numberOfLeadingZeros(value | 1);
56+
int digits = ((9 * bits) >>> 5);
6157

6258
if (value > LENGTH_TABLE[digits]) {
6359
digits += 1;

0 commit comments

Comments
 (0)