Skip to content
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

perf(core): binary search for findIndex #493

Merged
merged 14 commits into from
Oct 13, 2024
32 changes: 22 additions & 10 deletions src/core/cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type InternalCacheSnapshot, type ItemsRange } from "./types";
import { clamp, max, median, min } from "./utils";
import { clamp, floor, max, median, min } from "./utils";

type Writeable<T> = {
-readonly [key in keyof T]: Writeable<T[key]>;
Expand Down Expand Up @@ -93,22 +93,34 @@ export const computeTotalSize = (cache: Cache): number => {
};

/**
* Finds the index of an item in the cache whose computed offset is closest to the specified offset.
*
* @internal
*/
export const findIndex = (cache: Cache, offset: number, i: number): number => {
while (i >= 0 && i < cache._length) {
const itemOffset = computeOffset(cache, i);
// Find with binary search
let low = 0;
let high = cache._length - 1;

if (computeOffset(cache, i) <= offset) {
low = i; // Start searching from initialIndex -> up
} else {
high = i; // Start searching from initialIndex -> down
}

while (low <= high) {
const mid = floor((low + high) / 2);
const itemOffset = computeOffset(cache, mid);
if (itemOffset <= offset) {
if (itemOffset + getItemSize(cache, i) > offset) {
break;
} else {
i++;
if (itemOffset + getItemSize(cache, mid) > offset) {
return mid;
}
low = mid + 1;
} else {
i--;
high = mid - 1;
}
}
return clamp(i, 0, cache._length - 1);
return clamp(low, 0, cache._length - 1);
onx2 marked this conversation as resolved.
Show resolved Hide resolved
};

/**
Expand Down Expand Up @@ -185,7 +197,7 @@ export const initCache = (
* @internal
*/
export const takeCacheSnapshot = (cache: Cache): InternalCacheSnapshot => {
return [[...cache._sizes], cache._defaultItemSize];
return [cache._sizes.slice(), cache._defaultItemSize];
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export const NULL = null;

/** @internal */
export const { min, max, abs } = Math;
export const { min, max, abs, floor } = Math;
/** @internal */
export const values = Object.values;
/** @internal */
Expand Down
Loading