Skip to content

Commit

Permalink
rename maxCachesize, remove comments and refactor to simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
interim17 committed Aug 22, 2024
1 parent e324f91 commit 46b5e45
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 38 deletions.
2 changes: 1 addition & 1 deletion examples/src/Viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ class Viewer extends React.Component<InputParams, ViewerState> {
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
/>
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion src/controller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
26 changes: 9 additions & 17 deletions src/simularium/VisData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand All @@ -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,
Expand Down Expand Up @@ -140,7 +145,6 @@ class VisData {
return frame;
}

// linked list to do is this sufficient?
public gotoNextFrame(): void {
if (!this.atLatestFrame()) {
this.currentFrameNumber += 1;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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(
Expand Down
12 changes: 4 additions & 8 deletions src/simularium/VisDataCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
Expand All @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions src/simularium/VisDataParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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++];
}

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/viewport/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultProps>;

const defaultProps = {
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/visGeometry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, VisAgent>();
for (let i = 0; i < agentCount; i++) {
Expand Down
1 change: 0 additions & 1 deletion src/visGeometry/workers/visDataWorker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { parseVisDataMessage } from "../../simularium/VisDataParse";

//linked list to do make sure this actually works
self.addEventListener(
"message",
(e: MessageEvent) => {
Expand Down

0 comments on commit 46b5e45

Please sign in to comment.