forked from PrismarineJS/prismarine-web-client
-
Notifications
You must be signed in to change notification settings - Fork 146
feat: add long-term chunk geometry caching #477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FELMONON
wants to merge
12
commits into
zardoy:next
Choose a base branch
from
FELMONON:feat/chunk-geometry-caching
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d6257fd
feat: add long-term chunk geometry caching
7d8fd3d
fix: resolve ESLint errors in chunkCacheIntegration
02f39d9
fix: resolve ESLint errors in chunkGeometryCache
f4b01ab
Fix CodeRabbit issues: buffer handling, cache validation, WebCrypto g…
b656c20
Fix CodeRabbit issues in chunk geometry caching
4536592
fix: properly disable eslint for ArrayLike spread workaround
96ce484
feat: implement server-side chunk caching protocol
ab0a94c
refactor: migrate chunk cache from IndexedDB to browserfs fs storage
5277d59
refactor: address remaining PR feedback for chunk caching
ea2534a
fix: address lint errors and improve packet serialization
2e40bae
fix: add cache recovery and fix ArrayBuffer alignment
e9642be
fix: prevent memory leak in pendingChunkHashes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| /** | ||
| * Chunk Geometry Cache Integration | ||
| * | ||
| * This module provides integration between the chunk geometry cache | ||
| * and the world renderer system. It handles: | ||
| * - Computing block hashes for cache keys | ||
| * - Checking cache before requesting geometry from workers | ||
| * - Saving generated geometry to cache | ||
| */ | ||
|
|
||
| import type { MesherGeometryOutput } from './mesher/shared' | ||
|
|
||
| // Store for block state IDs by section for hash computation | ||
| const sectionBlockStates = new Map<string, Uint16Array>() | ||
|
|
||
| /** | ||
| * Store block state IDs for a section (called when chunk data is loaded) | ||
| */ | ||
| export function storeSectionBlockStates ( | ||
| sectionKey: string, | ||
| blockStateIds: Uint16Array | number[] | ||
| ): void { | ||
| const data = blockStateIds instanceof Uint16Array | ||
| ? blockStateIds | ||
| : new Uint16Array(blockStateIds) | ||
| sectionBlockStates.set(sectionKey, data) | ||
| } | ||
|
|
||
| /** | ||
| * Get stored block state IDs for a section | ||
| */ | ||
| export function getSectionBlockStates (sectionKey: string): Uint16Array | null { | ||
| return sectionBlockStates.get(sectionKey) || null | ||
| } | ||
|
|
||
| /** | ||
| * Clear block state data for a section | ||
| */ | ||
| export function clearSectionBlockStates (sectionKey: string): void { | ||
| sectionBlockStates.delete(sectionKey) | ||
| } | ||
|
|
||
| /** | ||
| * Clear all stored block state data | ||
| */ | ||
| export function clearAllBlockStates (): void { | ||
| sectionBlockStates.clear() | ||
| } | ||
|
|
||
| /** | ||
| * Compute a simple hash from block state IDs | ||
| * Uses a fast non-cryptographic hash for performance | ||
| */ | ||
| export function computeBlockHash (blockStateIds: Uint16Array): string { | ||
| // Use FNV-1a hash for fast hashing | ||
| let hash = 2_166_136_261 // FNV offset basis | ||
| for (const stateId of blockStateIds) { | ||
| hash ^= stateId | ||
| hash = Math.imul(hash, 16_777_619) // FNV prime | ||
| } | ||
| // Convert to unsigned 32-bit and then to hex | ||
| return (hash >>> 0).toString(16).padStart(8, '0') | ||
| } | ||
|
|
||
| /** | ||
| * Generate a simple hash from block state IDs (async version using crypto.subtle) | ||
| * Use this for more secure hashing when persistent storage is used | ||
| */ | ||
| export async function computeBlockHashAsync (blockStateIds: Uint16Array): Promise<string> { | ||
| if (globalThis.crypto?.subtle) { | ||
| try { | ||
| // Pass the typed array view directly (not .buffer which includes the entire ArrayBuffer) | ||
| const viewBytes = new Uint8Array(blockStateIds.buffer, blockStateIds.byteOffset, blockStateIds.byteLength) | ||
| const buffer = await crypto.subtle.digest('SHA-256', viewBytes) | ||
| const hashArray = [...new Uint8Array(buffer)] | ||
| // Use first 8 bytes for a shorter hash | ||
| return hashArray.slice(0, 8).map(b => b.toString(16).padStart(2, '0')).join('') | ||
| } catch { | ||
| // Fall back to simple hash | ||
| return computeBlockHash(blockStateIds) | ||
| } | ||
| } | ||
| return computeBlockHash(blockStateIds) | ||
| } | ||
|
|
||
| /** | ||
| * Check if geometry data is valid and can be cached | ||
| */ | ||
| export function isGeometryCacheable (geometry: MesherGeometryOutput): boolean { | ||
| // Don't cache empty geometry or geometry with errors | ||
| return Boolean(geometry.positions?.length) && !geometry.hadErrors | ||
| } | ||
|
|
||
| /** | ||
| * Get section coordinates from section key | ||
| */ | ||
| export function parseSectionKey (sectionKey: string): { x: number; y: number; z: number } | null { | ||
| const parts = sectionKey.split(',') | ||
| if (parts.length !== 3) return null | ||
| const [x, y, z] = parts.map(Number) | ||
| if (Number.isNaN(x) || Number.isNaN(y) || Number.isNaN(z)) return null | ||
| return { x, y, z } | ||
| } | ||
|
|
||
| /** | ||
| * Create a section key from coordinates | ||
| */ | ||
| export function createSectionKey (x: number, y: number, z: number): string { | ||
| return `${x},${y},${z}` | ||
| } | ||
|
|
||
| /** | ||
| * Create a chunk key from coordinates | ||
| */ | ||
| export function createChunkKey (x: number, z: number): string { | ||
| return `${x},${z}` | ||
| } | ||
|
|
||
| /** | ||
| * Compute a hash from raw chunk data (ArrayBuffer or array) | ||
| * Uses FNV-1a for fast hashing | ||
| */ | ||
| export function computeChunkDataHash (chunkData: ArrayBuffer | ArrayLike<number>): string { | ||
| // Convert to Uint8Array - works with both ArrayBuffer and ArrayLike<number> | ||
| const data = new Uint8Array( | ||
| // eslint-disable-next-line unicorn/prefer-spread -- ArrayLike is not Iterable | ||
| chunkData instanceof ArrayBuffer ? chunkData : Array.from(chunkData) | ||
| ) | ||
|
|
||
| // Use FNV-1a hash | ||
| let hash = 2_166_136_261 // FNV offset basis | ||
| for (const byte of data) { | ||
| hash ^= byte | ||
| hash = Math.imul(hash, 16_777_619) // FNV prime | ||
| } | ||
| return (hash >>> 0).toString(16).padStart(8, '0') | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite understand how this cache is used, don't we already have cache in the world renderer? We better not introduce unscoped variables (it's fine to split some methods into its file like you done).