Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
inokawa committed Apr 15, 2023
1 parent eaa18ce commit 253e2b4
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ export const computeTotalSize = (cache: Writeable<Cache>): number => {
return cache._offsets[lastIndex]! + getItemSize(cache, lastIndex);
}

let top = cache._offsets[cache._measuredOffsetIndex]!;
for (let i = cache._measuredOffsetIndex; i <= lastIndex; i++) {
let i = cache._measuredOffsetIndex;
let top = cache._offsets[i]!;
while (i <= lastIndex) {
cache._offsets[i] = top;
top += getItemSize(cache, i);
i++;
}

cache._measuredOffsetIndex = lastIndex;
Expand All @@ -48,9 +50,8 @@ const findIndex = (cache: Cache, i: number, distance: number): number => {
if (distance >= 0) {
// search forward
while (i < cache._length - 1) {
const h = getItemSize(cache, i);
const h = getItemSize(cache, i++);
sum += h;
i++;
if (sum >= distance) {
if (sum - h / 2 >= distance) {
i--;
Expand All @@ -61,8 +62,7 @@ const findIndex = (cache: Cache, i: number, distance: number): number => {
} else {
// search backward
while (i > 0) {
i--;
const h = getItemSize(cache, i);
const h = getItemSize(cache, --i);
sum -= h;
if (sum <= distance) {
if (sum + h / 2 < distance) {
Expand Down Expand Up @@ -109,13 +109,15 @@ export const computeStartOffset = (
return cache._offsets[index]!;
}

let top = cache._offsets[cache._measuredOffsetIndex]!;
for (let i = cache._measuredOffsetIndex; i <= index; i++) {
let i = cache._measuredOffsetIndex;
let top = cache._offsets[i]!;
while (i <= index) {
cache._offsets[i] = top;
if (i === index) {
break;
}
top += getItemSize(cache, i);
i++;
}
cache._measuredOffsetIndex = index;
return top;
Expand Down

0 comments on commit 253e2b4

Please sign in to comment.