-
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 search trace parsing
- Loading branch information
1 parent
0880d8f
commit 03a1cd6
Showing
15 changed files
with
205 additions
and
147 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
import { Properties as Props } from "protocol"; | ||
import { Context, PropMap } from "./Context"; | ||
import { mapProperties } from "./mapProperties"; | ||
import { normalize } from "./normalize"; | ||
import { normalizeConstant } from "./normalize"; | ||
|
||
export function applyScope<T extends Props>( | ||
scope: PropMap<T>, | ||
props: PropMap<T> | ||
): PropMap<T> { | ||
const scopedComponent = mapProperties( | ||
props, | ||
(prop) => (provided: Context<T>) => | ||
prop(applyScope(normalize(provided), scope)) | ||
return Object.setPrototypeOf( | ||
mapProperties( | ||
props, | ||
(prop) => (provided: Context<T>) => | ||
prop(applyScope(normalizeConstant(provided), scope)) | ||
), | ||
scope | ||
); | ||
return { ...scope, ...scopedComponent }; | ||
} |
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,20 +1,21 @@ | ||
import { mapValues, ObjectIterator } from "lodash"; | ||
import { ObjectIterator } from "lodash"; | ||
import { Properties as Props } from "protocol"; | ||
import { Context } from "./Context"; | ||
|
||
/** | ||
* Iterates over the properties of a scope or component, | ||
* ignoring the component name property `$`. | ||
* @param context The properties to iterate against. | ||
* @param iterator | ||
* @param f | ||
* @returns | ||
*/ | ||
export function mapProperties<T extends Props, TResult>( | ||
context: Context<T> = {}, | ||
iterator: ObjectIterator<T, TResult> | ||
f: ObjectIterator<Context<T>, TResult> | ||
): Context<T> & { $: string } { | ||
const { $, ...props } = context; | ||
return { | ||
...mapValues(props, iterator), | ||
$, | ||
}; | ||
const out: any = {}; | ||
for (const key of Object.keys(context)) { | ||
out[key] = key === "$" ? context[key] : f(context[key], key, context); | ||
} | ||
return Object.setPrototypeOf(out, context); | ||
} |
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
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
123 changes: 32 additions & 91 deletions
123
client/src/components/renderer/parser/parseTrace.worker.ts
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,104 +1,45 @@ | ||
import { chain, findLast, mapValues, negate } from "lodash"; | ||
import { chunk, flatMap, map } from "lodash"; | ||
import { usingWorkerTask } from "workers/usingWorker"; | ||
import { | ||
CompiledComponent, | ||
EventContext, | ||
ParsedComponent, | ||
Trace, | ||
TraceEvent, | ||
} from "protocol"; | ||
import { ComponentEntry } from "renderer"; | ||
import { mapProperties } from "./mapProperties"; | ||
import { normalizeConstant } from "./normalize"; | ||
import { parse as parseComponents } from "./parse"; | ||
|
||
const isNullish = (x: KeyRef): x is Exclude<KeyRef, Key> => | ||
x === undefined || x === null; | ||
|
||
type Key = string | number; | ||
|
||
type KeyRef = Key | null | undefined; | ||
ParseTraceWorkerParameters, | ||
ParseTraceWorkerReturnType, | ||
} from "./parseTraceSlave.worker"; | ||
import parseTraceWorkerUrl from "./parseTraceSlave.worker.ts?worker&url"; | ||
|
||
export class ParseTraceWorker extends Worker { | ||
constructor() { | ||
super(parseTraceWorkerUrl, { type: "module" }); | ||
} | ||
} | ||
|
||
const isPersistent = (c: CompiledComponent<string, Record<string, any>>) => | ||
c.display !== "transient"; | ||
const parseTraceWorker = usingWorkerTask< | ||
ParseTraceWorkerParameters, | ||
ParseTraceWorkerReturnType | ||
>(ParseTraceWorker); | ||
|
||
function parse({ | ||
async function parse({ | ||
trace, | ||
context, | ||
view = "main", | ||
}: ParseTraceWorkerParameters): ParseTraceWorkerReturnType { | ||
const parsed = parseComponents( | ||
trace?.render?.views?.[view]?.components ?? [], | ||
trace?.render?.components ?? {} | ||
}: ParseTraceWorkerParameters): Promise<ParseTraceWorkerReturnType> { | ||
const chunks = chunk(trace?.events, (trace?.events?.length ?? 0) / 8); | ||
|
||
const outs = await Promise.all( | ||
map(chunks, (chunk1) => | ||
parseTraceWorker({ | ||
trace: { ...trace, events: chunk1 }, | ||
context, | ||
view, | ||
}) | ||
) | ||
); | ||
|
||
const apply = ( | ||
event: TraceEvent, | ||
ctx?: EventContext | ||
): CompiledComponent<string, Record<string, any>>[] => | ||
parsed.map((p) => | ||
mapProperties< | ||
ParsedComponent<string, any>, | ||
CompiledComponent<string, Record<string, any>> | ||
>(p, (c) => | ||
c( | ||
normalizeConstant({ | ||
alpha: 1, | ||
...context, | ||
...ctx, | ||
event, | ||
events: trace?.events, | ||
}) | ||
) | ||
) | ||
); | ||
|
||
const isVisible = (c: CompiledComponent<string, { alpha?: number }>) => | ||
c && Object.hasOwn(c, "alpha") ? c!.alpha! > 0 : true; | ||
|
||
const makeEntryIteratee = | ||
(step: number) => | ||
(component: CompiledComponent<string, Record<string, any>>) => ({ | ||
component, | ||
meta: { source: "trace", step: step }, | ||
}); | ||
|
||
const r = chain(trace?.events) | ||
.map((c, i) => ({ step: i, id: c.id, data: c, pId: c.pId })) | ||
.groupBy("id") | ||
.value(); | ||
|
||
const steps = chain(trace?.events) | ||
.map((e, i, esx) => { | ||
const component = apply(e, { | ||
step: i, | ||
parent: !isNullish(e.pId) | ||
? esx[findLast(r[e.pId], (x) => x.step <= i)?.step ?? 0] | ||
: undefined, | ||
}); | ||
const persistent = component.filter(isPersistent); | ||
const transient = component.filter(negate(isPersistent)); | ||
return { persistent, transient }; | ||
}) | ||
.map((c) => mapValues(c, (b) => b.filter(isVisible))) | ||
.map((c, i) => mapValues(c, (b) => b.map(makeEntryIteratee(i)))) | ||
.value(); | ||
return { | ||
stepsPersistent: steps.map((c) => c.persistent), | ||
stepsTransient: steps.map((c) => c.transient), | ||
stepsPersistent: flatMap(outs, "stepsPersistent"), | ||
stepsTransient: flatMap(outs, "stepsTransient"), | ||
}; | ||
} | ||
|
||
export type ParseTraceWorkerParameters = { | ||
trace?: Trace; | ||
context: EventContext; | ||
view?: string; | ||
}; | ||
|
||
export type ParseTraceWorkerReturnType = { | ||
stepsPersistent: ComponentEntry[][]; | ||
stepsTransient: ComponentEntry[][]; | ||
}; | ||
|
||
onmessage = ({ data }: MessageEvent<ParseTraceWorkerParameters>) => { | ||
postMessage(parse(data)); | ||
onmessage = async ({ data }: MessageEvent<ParseTraceWorkerParameters>) => { | ||
postMessage(await parse(data)); | ||
}; |
Oops, something went wrong.