-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Performance improvements for complex traces
- Loading branch information
1 parent
ebe440d
commit 1e8b55c
Showing
9 changed files
with
171 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,113 @@ | ||
import { useRendererInstance } from "components/inspector/TraceRenderer"; | ||
import { floor, slice } from "lodash"; | ||
import { useEffect, useMemo } from "react"; | ||
import { ComponentEntry } from "renderer"; | ||
import { useRendererInstance } from "components/inspector/TraceRenderer"; | ||
/** | ||
* For distinguish between persisted views like grid, mesh, tree, map | ||
* and non-persisted views like tile and multiagent | ||
* if true, then use split nodelists and draw every history event | ||
* if false, use a single nodelist and draw current event | ||
*/ | ||
|
||
function generateNumberArray(n: number) { | ||
const numberArray = [0]; | ||
const digits = n.toString(2).split("").map(Number); | ||
|
||
for (let i = 0; i < digits.length; i++) { | ||
const power = digits.length - i - 1; | ||
const newNumber = digits.slice(0, i + 1).join("") + "0".repeat(power); | ||
numberArray.push(parseInt(newNumber, 2)); | ||
} | ||
|
||
return numberArray; | ||
} | ||
|
||
function pairwise<T, U>(arr: T[], func: (x: T, y: T) => U) { | ||
const out: U[] = []; | ||
for (let i = 0; i < arr.length - 1; i++) { | ||
out.push(func(arr[i], arr[i + 1])); | ||
} | ||
return out; | ||
} | ||
|
||
export type NodeListProps = { | ||
nodes?: ComponentEntry[][]; | ||
start?: number; | ||
end?: number; | ||
}; | ||
|
||
export type LazyNodeListProps = NodeListProps & { | ||
step?: number; | ||
export type NodeList2Props = { | ||
nodes?: ComponentEntry[]; | ||
}; | ||
|
||
export function NodeList({ nodes }: NodeListProps) { | ||
export type LazyNodeListProps = NodeListProps; | ||
|
||
export function NodeList({ | ||
nodes, | ||
start = 0, | ||
end: step = nodes?.length ?? 0, | ||
}: NodeListProps) { | ||
const { renderer } = useRendererInstance(); | ||
useEffect(() => { | ||
if (renderer && nodes?.length) { | ||
return renderer.add(nodes.flat()); | ||
return renderer.add(slice(nodes, start, step).flat()); | ||
} | ||
}, [renderer, nodes, start, step]); | ||
|
||
return <></>; | ||
} | ||
export function NodeList2({ nodes }: NodeList2Props) { | ||
const { renderer } = useRendererInstance(); | ||
useEffect(() => { | ||
if (renderer && nodes?.length) { | ||
return renderer.add(nodes); | ||
} | ||
}, [renderer, nodes]); | ||
|
||
return <></>; | ||
} | ||
// export function LazyNodeList({ nodes, start = 0, end = 0 }: LazyNodeListProps) { | ||
// return pairwise(range(start, end, 1), (x, y) => ( | ||
// <NodeList key={`${x}::${y}`} nodes={nodes} start={x} end={y} /> | ||
// )); | ||
// const c2 = generateNumberArray(end); | ||
// return pairwise(c2, (start, end) => { | ||
// const end2 = end + 1; | ||
// const cacheSize = 2; | ||
// const run = end2 - start; | ||
// // number of nodes needed to be cached | ||
// const threshold = floor((run ?? 0) / 2 / cacheSize) * cacheSize; | ||
// return [ | ||
// !!threshold && ( | ||
// <NodeList | ||
// key={`${start}::${start + threshold}`} | ||
// nodes={nodes} | ||
// start={start} | ||
// end={start + threshold} | ||
// /> | ||
// ), | ||
// !!(end2 - (start + threshold)) && ( | ||
// <NodeList | ||
// key={`${start + threshold}::${end2}`} | ||
// nodes={nodes} | ||
// start={start + threshold} | ||
// end={end2} | ||
// /> | ||
// ), | ||
// ]; | ||
// }).flat(); | ||
// } | ||
|
||
export function LazyNodeList({ nodes, step }: LazyNodeListProps) { | ||
const cacheSize = 200; | ||
export function LazyNodeList({ nodes, end }: LazyNodeListProps) { | ||
const cacheSize = 100; | ||
|
||
// number of nodes needed to be cached | ||
const threshold = floor((step ?? 0) / cacheSize) * cacheSize; | ||
const threshold = floor((end ?? 0) / cacheSize) * cacheSize; | ||
|
||
const cached = useMemo(() => slice(nodes, 0, threshold), [nodes, threshold]); | ||
const uncached = useMemo( | ||
() => slice(nodes, threshold, (step ?? 0) + 1), | ||
[nodes, threshold, step] | ||
() => slice(nodes, threshold, (end ?? 0) + 1), | ||
[nodes, threshold, end] | ||
); | ||
return ( | ||
<> | ||
{!!threshold && <NodeList nodes={cached} />} | ||
<NodeList nodes={uncached} /> | ||
{uncached.map((c, i) => ( | ||
<NodeList2 key={threshold + i} nodes={c}></NodeList2> | ||
))} | ||
</> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.