Skip to content

Commit 20f6e3d

Browse files
committed
Use useMemo a few places to speed up UI interactions
1 parent b05ec33 commit 20f6e3d

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

extensions/ql-vscode/src/view/compare-performance/ComparePerformance.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ChangeEvent } from "react";
2-
import { Fragment, useMemo, useState } from "react";
2+
import { Fragment, useMemo, useRef, useState } from "react";
33
import type {
44
SetPerformanceComparisonQueries,
55
ToComparePerformanceViewMessage,
@@ -411,9 +411,9 @@ function ComparePerformanceWithData(props: {
411411
[from, to],
412412
);
413413

414-
let hasCacheHitMismatch = false;
414+
const hasCacheHitMismatch = useRef<boolean>(false);
415415

416-
const rows = Array.from(nameSet)
416+
const rows = useMemo(() => Array.from(nameSet)
417417
.map((name) => {
418418
const before = from.getTupleCountInfo(name);
419419
const after = to.getTupleCountInfo(name);
@@ -426,7 +426,7 @@ function ComparePerformanceWithData(props: {
426426
before.absentReason === AbsentReason.CacheHit ||
427427
after.absentReason === AbsentReason.CacheHit
428428
) {
429-
hasCacheHitMismatch = true;
429+
hasCacheHitMismatch.current = true;
430430
if (hideCacheHits) {
431431
return undefined!;
432432
}
@@ -435,7 +435,9 @@ function ComparePerformanceWithData(props: {
435435
return { name, before, after, diff };
436436
})
437437
.filter((x) => !!x)
438-
.sort(getSortOrder(sortOrder));
438+
.sort(getSortOrder(sortOrder)),
439+
[nameSet, from, to, metric, hideCacheHits, sortOrder],
440+
);
439441

440442
let totalBefore = 0;
441443
let totalAfter = 0;
@@ -447,12 +449,15 @@ function ComparePerformanceWithData(props: {
447449
totalDiff += row.diff;
448450
}
449451

450-
const rowNames = abbreviateRANames(rows.map((row) => row.name));
452+
const rowNames = useMemo(
453+
() => abbreviateRANames(rows.map((row) => row.name)),
454+
[rows],
455+
);
451456

452457
return (
453458
<>
454459
<ViewTitle>Performance comparison</ViewTitle>
455-
{hasCacheHitMismatch && (
460+
{hasCacheHitMismatch.current && (
456461
<WarningBox>
457462
<strong>Inconsistent cache hits</strong>
458463
<br />

0 commit comments

Comments
 (0)