提交 37f5de84 authored 作者: Thomas Mueller's avatar Thomas Mueller

A persistent multi-version map: small performance optimization (for write-heavy operations)

上级 56fe701d
...@@ -341,15 +341,27 @@ public class DataUtils { ...@@ -341,15 +341,27 @@ public class DataUtils {
if (len <= 32) { if (len <= 32) {
return 0; return 0;
} }
int x = len; int code = Integer.numberOfLeadingZeros(len);
int shift = 0; int remaining = len << (code + 1);
while (x > 3) { code += code;
shift++; if ((remaining & (1 << 31)) != 0) {
x = (x >> 1) + (x & 1); code--;
} }
shift = Math.max(0, shift - 4); if ((remaining << 1) != 0) {
int code = (shift << 1) + (x & 1); code--;
return Math.min(31, code); }
code = Math.min(31, 52 - code);
// alternative code (slower):
// int x = len;
// int shift = 0;
// while (x > 3) {
// shift++;
// x = (x >>> 1) + (x & 1);
// }
// shift = Math.max(0, shift - 4);
// int code = (shift << 1) + (x & 1);
// code = Math.min(31, code);
return code;
} }
/** /**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论