From d5dbdb015a674d1f31e2aff58b39fb9b4cf2b6e6 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 26 Mar 2026 06:48:57 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Optimize=20BloomPostProcessor=20ini?= =?UTF-8?q?tialization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `BloomPostProcessor.init` method was fetching shader files sequentially, leading to unnecessary latency. This change parallelizes the fetching of threshold, blur, and composite shaders using `Promise.all`. Benchmark results (simulated network delay of 100ms): - Baseline: ~300ms - Optimized: ~100ms - Improvement: ~66% faster initialization phase. Affected files: - `public/utils/bloomPostProcessor.ts` - `utils/bloomPostProcessor.ts` Co-authored-by: ford442 <9397845+ford442@users.noreply.github.com> --- public/utils/bloomPostProcessor.ts | 20 ++++++++++---------- utils/bloomPostProcessor.ts | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/public/utils/bloomPostProcessor.ts b/public/utils/bloomPostProcessor.ts index 716ba0b..a75842a 100644 --- a/public/utils/bloomPostProcessor.ts +++ b/public/utils/bloomPostProcessor.ts @@ -60,16 +60,16 @@ export class BloomPostProcessor { // Call once after construction public async init() { - // Try to load shader code if not supplied - if (!this.thresholdShaderCode) { - this.thresholdShaderCode = await this.tryFetch(`${this.baseUrl}/shaders/bloom_threshold.wgsl`); - } - if (!this.blurShaderCode) { - this.blurShaderCode = await this.tryFetch(`${this.baseUrl}/shaders/bloom_blur.wgsl`); - } - if (!this.compositeShaderCode) { - this.compositeShaderCode = await this.tryFetch(`${this.baseUrl}/shaders/bloom_composite.wgsl`); - } + // Try to load shader code if not supplied, fetching concurrently if needed + const [t, b, c] = await Promise.all([ + this.thresholdShaderCode ? Promise.resolve(this.thresholdShaderCode) : this.tryFetch(`${this.baseUrl}/shaders/bloom_threshold.wgsl`), + this.blurShaderCode ? Promise.resolve(this.blurShaderCode) : this.tryFetch(`${this.baseUrl}/shaders/bloom_blur.wgsl`), + this.compositeShaderCode ? Promise.resolve(this.compositeShaderCode) : this.tryFetch(`${this.baseUrl}/shaders/bloom_composite.wgsl`) + ]); + + this.thresholdShaderCode = t; + this.blurShaderCode = b; + this.compositeShaderCode = c; // Create textures const width = this.canvas.width; diff --git a/utils/bloomPostProcessor.ts b/utils/bloomPostProcessor.ts index c252d85..7eb4ab9 100644 --- a/utils/bloomPostProcessor.ts +++ b/utils/bloomPostProcessor.ts @@ -60,16 +60,16 @@ export class BloomPostProcessor { // Call once after construction public async init() { - // Try to load shader code if not supplied - if (!this.thresholdShaderCode) { - this.thresholdShaderCode = await this.tryFetch(`${this.baseUrl}/shaders/bloom_threshold.wgsl`); - } - if (!this.blurShaderCode) { - this.blurShaderCode = await this.tryFetch(`${this.baseUrl}/shaders/bloom_blur.wgsl`); - } - if (!this.compositeShaderCode) { - this.compositeShaderCode = await this.tryFetch(`${this.baseUrl}/shaders/bloom_composite.wgsl`); - } + // Try to load shader code if not supplied, fetching concurrently if needed + const [t, b, c] = await Promise.all([ + this.thresholdShaderCode ? Promise.resolve(this.thresholdShaderCode) : this.tryFetch(`${this.baseUrl}/shaders/bloom_threshold.wgsl`), + this.blurShaderCode ? Promise.resolve(this.blurShaderCode) : this.tryFetch(`${this.baseUrl}/shaders/bloom_blur.wgsl`), + this.compositeShaderCode ? Promise.resolve(this.compositeShaderCode) : this.tryFetch(`${this.baseUrl}/shaders/bloom_composite.wgsl`) + ]); + + this.thresholdShaderCode = t; + this.blurShaderCode = b; + this.compositeShaderCode = c; // Create textures const width = this.canvas.width;