small optimizations

This commit is contained in:
2025-07-06 17:11:35 +02:00
parent bf5085431a
commit 8aad6554ed
4 changed files with 144 additions and 90 deletions

View File

@ -46,7 +46,7 @@ interface WorkerResponse {
import { LRUCache } from './utils/LRUCache';
import { calculateColorDirect } from './utils/colorModes';
import { PERFORMANCE, COLOR_TABLE_SIZE } from './utils/constants';
import { PERFORMANCE, COLOR_TABLE_SIZE, RENDER_MODES, RENDER_MODE_INDEX } from './utils/constants';
type ShaderFunction = (...args: number[]) => number;
@ -59,7 +59,7 @@ class ShaderWorker {
private compilationCache: LRUCache<string, ShaderFunction> = new LRUCache(
PERFORMANCE.COMPILATION_CACHE_SIZE
);
private colorTables: Map<string, Uint8Array> = new Map();
private colorTables: Uint8Array[] = [];
private feedbackBuffer: Float32Array | null = null;
constructor() {
@ -73,19 +73,11 @@ class ShaderWorker {
private initializeColorTables(): void {
const tableSize = COLOR_TABLE_SIZE;
// Pre-compute color tables for each render mode
const modes = [
'classic',
'grayscale',
'red',
'green',
'blue',
'rgb',
'hsv',
'rainbow',
];
// Pre-compute color tables for each render mode using array indexing
this.colorTables = new Array(RENDER_MODES.length);
for (const mode of modes) {
for (let modeIndex = 0; modeIndex < RENDER_MODES.length; modeIndex++) {
const mode = RENDER_MODES[modeIndex];
const colorTable = new Uint8Array(tableSize * 3); // RGB triplets
for (let i = 0; i < tableSize; i++) {
@ -95,7 +87,7 @@ class ShaderWorker {
colorTable[i * 3 + 2] = b;
}
this.colorTables.set(mode, colorTable);
this.colorTables[modeIndex] = colorTable;
}
}
@ -228,44 +220,10 @@ class ShaderWorker {
}
private isStaticExpression(code: string): boolean {
// Check if code contains any variables
const variables = [
'x',
'y',
't',
'i',
'mouseX',
'mouseY',
'mousePressed',
'mouseVX',
'mouseVY',
'mouseClickTime',
'touchCount',
'touch0X',
'touch0Y',
'touch1X',
'touch1Y',
'pinchScale',
'pinchRotation',
'accelX',
'accelY',
'accelZ',
'gyroX',
'gyroY',
'gyroZ',
'audioLevel',
'bassLevel',
'midLevel',
'trebleLevel',
];
for (const variable of variables) {
if (code.includes(variable)) {
return false;
}
}
return true;
// Check if code contains any variables using regex for better accuracy
const variablePattern = /\b(x|y|t|i|r|a|u|v|c|f|d|n|b|mouse[XY]|mousePressed|mouseV[XY]|mouseClickTime|touchCount|touch[01][XY]|pinchScale|pinchRotation|accel[XYZ]|gyro[XYZ]|audioLevel|bassLevel|midLevel|trebleLevel)\b/;
return !variablePattern.test(code);
}
private evaluateStaticExpression(code: string): number {
@ -836,14 +794,15 @@ class ShaderWorker {
break;
}
// Use pre-computed color table if available
const colorTable = this.colorTables.get(renderMode);
if (colorTable) {
// Use pre-computed color table with O(1) array indexing
const modeIndex = RENDER_MODE_INDEX[renderMode];
if (modeIndex !== undefined && this.colorTables[modeIndex]) {
const colorTable = this.colorTables[modeIndex];
const index = Math.floor(processedValue) * 3;
return [colorTable[index], colorTable[index + 1], colorTable[index + 2]];
}
// Fallback to direct calculation
// Fallback to direct calculation for unknown render modes
return calculateColorDirect(processedValue, renderMode);
}