From 46b5e45efb9d875f3bafc793054b325d05767f3f Mon Sep 17 00:00:00 2001 From: Joe Heffernan Date: Wed, 21 Aug 2024 17:47:34 -0700 Subject: [PATCH] rename maxCachesize, remove comments and refactor to simplify --- examples/src/Viewer.tsx | 2 +- src/controller/index.ts | 1 - src/simularium/VisData.ts | 26 ++++++++---------------- src/simularium/VisDataCache.ts | 12 ++++------- src/simularium/VisDataParse.ts | 12 +++++------ src/viewport/index.tsx | 6 +++--- src/visGeometry/index.ts | 2 +- src/visGeometry/workers/visDataWorker.ts | 1 - 8 files changed, 24 insertions(+), 38 deletions(-) diff --git a/examples/src/Viewer.tsx b/examples/src/Viewer.tsx index f9ede691..7e040363 100644 --- a/examples/src/Viewer.tsx +++ b/examples/src/Viewer.tsx @@ -1041,7 +1041,7 @@ class Viewer extends React.Component { backgroundColor={[0, 0, 0]} lockedCamera={false} disableCache={false} - // maxCacheLength={2000} // linked list to do test cache trimming + // maxCacheSize={2000} // in bytes, 1MB = 1000000, 1GB = 1000000000 /> diff --git a/src/controller/index.ts b/src/controller/index.ts index 58165cb1..00f31a94 100644 --- a/src/controller/index.ts +++ b/src/controller/index.ts @@ -380,7 +380,6 @@ export default class SimulariumController { } // start the simulation paused and get first frame - // linked list to do, confused here, it says "start sim paused" but this.start() sets isPaused to false if (this.simulator) { return this.start() .then(() => { diff --git a/src/simularium/VisData.ts b/src/simularium/VisData.ts index bf6034e4..1ade1b66 100644 --- a/src/simularium/VisData.ts +++ b/src/simularium/VisData.ts @@ -48,7 +48,6 @@ class VisData { new URL("../visGeometry/workers/visDataWorker", import.meta.url), { type: "module" } ); - // linked list to do make sure event is of type CachedData this.webWorker.onmessage = (event) => { this.linkedListCache.addFrame(event.data); }; @@ -60,8 +59,14 @@ class VisData { this.setupWebWorker(); } this.currentFrameNumber = -1; + /** + * if cache is not enabled only one current frame is ever stored + * if cache is enabled and maxSize is -1, unlimited frames are stored + * if cache is enabled and maxSize is set above -1, cache will be trimmed to below setting before adding frames + * if a maxCacheSize prop has been provided, it will override this value + */ this.enableCache = true; - // this.maxCacheSize = 10000000; // todo define defaults / constants for different browser environments + // this.maxCacheSize = 10000000; this.maxCacheSize = -1; this.linkedListCache = new LinkedListCache( this.maxCacheSize, @@ -140,7 +145,6 @@ class VisData { return frame; } - // linked list to do is this sufficient? public gotoNextFrame(): void { if (!this.atLatestFrame()) { this.currentFrameNumber += 1; @@ -155,7 +159,7 @@ class VisData { this.lockedForFrame = true; } - public setMaxCacheLength(cacheLength: number | undefined): void { + public setMaxCacheSize(cacheLength: number | undefined): void { if (cacheLength === undefined || cacheLength < 0) { this.maxCacheSize = -1; return; @@ -164,7 +168,6 @@ class VisData { this.maxCacheSize = cacheLength > 0 ? cacheLength : 1; } - // linked list to do make sure we are still covering all these bases when we "clear" public clearCache(): void { this.linkedListCache.clear(); this.currentFrameNumber = -1; @@ -223,7 +226,6 @@ class VisData { ) { this.webWorker.postMessage(visDataMsg); } else { - // to do linked list can this be more than one frame? const frame = parseVisDataMessage(visDataMsg); this.linkedListCache.addFrame(frame); } @@ -232,20 +234,13 @@ class VisData { public parseAgentsFromFrameData(msg: VisDataMessage | ArrayBuffer): void { if (msg instanceof ArrayBuffer) { const frame = VisData.parseOneBinaryFrame(msg); - if ( - // linked list to do - // this isn't actually the same check as before, its asking if there is agent dat awhich we maybe shuoldnt do - // frame.agentData.length > 0 && - // this is asking if the first frame in the new data is frame 0 - frame.frameNumber === 0 - ) { + if (frame.frameNumber === 0) { this.clearCache(); // new data has arrived } this.linkedListCache.addFrame(frame); return; } - // linked list to do: handle VisDataMessage properly this.parseAgentsFromVisDataMessage(msg); } @@ -268,14 +263,12 @@ class VisData { // // for use w/ a drag-and-drop trajectory file // // save a file for playback // // will be caught by controller.changeFile(...).catch() - // linked list to do confirm this is still used and working public cacheJSON(visDataMsg: VisDataMessage): void { if (!this.linkedListCache.isEmpty()) { throw new Error( "cache not cleared before cacheing a new drag-and-drop file" ); } - // linked list to do can this be more than one frame? const frame = parseVisDataMessage(visDataMsg); this.linkedListCache.addFrame(frame); } @@ -306,7 +299,6 @@ class VisData { } // will be caught by controller.changeFile(...).catch() - // linked list to do TODO: check if this code is still used public checkTypeMapping(typeMappingFromFile: EncodedTypeMapping): number[] { if (!typeMappingFromFile) { throw new Error( diff --git a/src/simularium/VisDataCache.ts b/src/simularium/VisDataCache.ts index 460c07d0..2366c2f2 100644 --- a/src/simularium/VisDataCache.ts +++ b/src/simularium/VisDataCache.ts @@ -93,16 +93,12 @@ class LinkedListCache { return this.tail ? this.tail.data : null; } - // linked list to do check if this is working well public getLastFrameNumber(): number { - if (this.tail && this.tail.data) { - return this.tail.data.frameNumber; - } - return -1; + return this.tail?.data.frameNumber || -1; } public getLastFrameTime(): number { - return this.tail ? this.tail.data.time : -1; + return this.tail?.data.time || -1; } public addFrame(data: CachedFrame): void { @@ -128,7 +124,7 @@ class LinkedListCache { this.tail = newNode; } this.numFrames++; - // linked list to do: trim cache if necessary + this.size += data.size; if (this.cacheSizeLimited && this.size > this.maxSize) { this.trimCache(); @@ -149,7 +145,7 @@ class LinkedListCache { this.head = newNode; } this.numFrames++; - // linked list to do: trim cache if necessary + this.size += data.size; if (this.cacheSizeLimited && this.size > this.maxSize) { this.trimCache(); diff --git a/src/simularium/VisDataParse.ts b/src/simularium/VisDataParse.ts index e2cb1fde..1be080c9 100644 --- a/src/simularium/VisDataParse.ts +++ b/src/simularium/VisDataParse.ts @@ -119,7 +119,7 @@ import { FrontEndError, ErrorLevel } from "./FrontEndError"; // "nSubPoints", // ]; const HEADER_SIZE = 3; // frameNumber, time, agentCount -const AGENT_HEADER_SIZE = AGENT_OBJECT_KEYS.length; +const FRAME_DATA_SIZE = AGENT_OBJECT_KEYS.length; function parseVisDataMessage(visDataMsg: VisDataMessage): CachedFrame { // Assuming visDataMsg.bundleData has only one frame for simplicity @@ -136,14 +136,14 @@ function parseVisDataMessage(visDataMsg: VisDataMessage): CachedFrame { // Write header data view[0] = frame.frameNumber; view[1] = frame.time; - view[2] = frame.data.length / (AGENT_HEADER_SIZE + 1); // Estimating agent count + view[2] = frame.data.length / (FRAME_DATA_SIZE + 1); // Estimating agent count let writeIndex = HEADER_SIZE; let readIndex = 0; while (readIndex < frame.data.length) { // Copy agent data - for (let i = 0; i < AGENT_HEADER_SIZE; i++) { + for (let i = 0; i < FRAME_DATA_SIZE; i++) { view[writeIndex++] = frame.data[readIndex++]; } @@ -182,10 +182,10 @@ function calculateBufferSize(data: number[]): number { let index = 0; while (index < data.length) { - size += AGENT_HEADER_SIZE * 4; // Agent header size in bytes - const nSubPoints = data[index + AGENT_HEADER_SIZE - 1]; + size += FRAME_DATA_SIZE * 4; // Agent header size in bytes + const nSubPoints = data[index + FRAME_DATA_SIZE - 1]; size += nSubPoints * 4; // Subpoints size in bytes - index += AGENT_HEADER_SIZE + nSubPoints; + index += FRAME_DATA_SIZE + nSubPoints; } return size; diff --git a/src/viewport/index.tsx b/src/viewport/index.tsx index fac0e2fd..5f219b7e 100644 --- a/src/viewport/index.tsx +++ b/src/viewport/index.tsx @@ -47,7 +47,7 @@ type ViewportProps = { onRecordedMovie?: (blob: Blob) => void; // providing this callback enables movie recording disableCache?: boolean; onFollowObjectChanged?: (agentData: AgentData) => void; // passes agent data about the followed agent to the front end - maxCacheLength?: number; + maxCacheSize?: number; } & Partial; const defaultProps = { @@ -127,8 +127,8 @@ class Viewport extends React.Component< this.handleTimeChange = this.handleTimeChange.bind(this); this.visGeometry = new VisGeometry(loggerLevel); - this.props.simulariumController.visData.setMaxCacheLength( - this.props.maxCacheLength + this.props.simulariumController.visData.setMaxCacheSize( + this.props.maxCacheSize ); this.props.simulariumController.visData.clearCache(); this.props.simulariumController.visData.setCacheEnabled( diff --git a/src/visGeometry/index.ts b/src/visGeometry/index.ts index 09e50338..7a6b71fa 100644 --- a/src/visGeometry/index.ts +++ b/src/visGeometry/index.ts @@ -1562,7 +1562,7 @@ class VisGeometry { for (let i = 0; i < MAX_MESHES && i < this.visAgents.length; i++) { this.visAgents[i].hideAndDeactivate(); } - // to do this is a naming conflict + let offset = AGENT_HEADER_SIZE; const newVisAgentInstances = new Map(); for (let i = 0; i < agentCount; i++) { diff --git a/src/visGeometry/workers/visDataWorker.ts b/src/visGeometry/workers/visDataWorker.ts index 1be59635..9cd812db 100644 --- a/src/visGeometry/workers/visDataWorker.ts +++ b/src/visGeometry/workers/visDataWorker.ts @@ -1,6 +1,5 @@ import { parseVisDataMessage } from "../../simularium/VisDataParse"; -//linked list to do make sure this actually works self.addEventListener( "message", (e: MessageEvent) => {