Skip to content

Commit

Permalink
Changed memory size estimator to reflect optimized modulo
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <[email protected]>
  • Loading branch information
Peter Alfonsi committed Nov 27, 2023
1 parent 7d1ce12 commit 93fa4f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public int getValue() {
protected final Lock readLock = lock.readLock();
protected final Lock writeLock = lock.writeLock();
private long mostRecentByteEstimate;
private final int REFRESH_SIZE_EST_INTERVAL = 10000;
protected final int REFRESH_SIZE_EST_INTERVAL = 10000;
// Refresh size estimate every X new elements. Refreshes use the RBM's internal size estimator, which takes ~0.01 ms,
// so we don't want to do it on every get(), and it doesn't matter much if there are +- 10000 keys in this store
// in terms of storage impact
Expand Down Expand Up @@ -281,7 +281,14 @@ public boolean isCollision(Integer value1, Integer value2) {
}

static double getRBMSizeMultiplier(int numEntries, int modulo) {
double x = Math.log10((double) numEntries / modulo);
double effectiveModulo = (double) modulo / 2;
/* This model was created when we used % operator to calculate modulo. This has range (-modulo, modulo).
Now we have optimized to use a bitmask, which has range [0, modulo). So the number of possible values stored
is halved. */
if (modulo == 0) {
effectiveModulo = Math.pow(2, 32);
}
double x = Math.log10((double) numEntries / effectiveModulo);
if (x < -5) {
return 7.0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ public void testTransformationLogic() throws Exception {

public void testContains() throws Exception {
RBMIntKeyLookupStore kls = new RBMIntKeyLookupStore(RBMIntKeyLookupStore.KeystoreModuloValue.TWO_TO_TWENTY_NINE, 0L);
for (int i = 0; i < 2000; i++) {
RBMIntKeyLookupStore noModuloKls = new RBMIntKeyLookupStore(RBMIntKeyLookupStore.KeystoreModuloValue.NONE, 0L);
for (int i = 0; i < kls.REFRESH_SIZE_EST_INTERVAL + 1000; i++) {
// set upper bound > number of elements to trigger a size check, ensuring we test that too
kls.add(i);
assertTrue(kls.contains(i));
noModuloKls.add(i);
assertTrue(noModuloKls.contains(i));
}
}

Expand Down

0 comments on commit 93fa4f8

Please sign in to comment.