-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grid): rename HexCache to CoordinatesCache and expand API, add t…
…oString() to hex The CoordinatesCache uses hex coordinates as keys in its internal map (hence the name). It has an API very similar to JS's native Map (and Set) class. Traversing a grid once using CoordinatesCache decreases performance about ~40% compared to using no cache. The turning point where caching starts improving performance is at around traversing a ~300 hex grid twice (or a ~130 hex grid thrice). The main purpose of the cache isn't improving performance (maybe I shouldn't call it a cache then?), but keeping state (hexes).
- Loading branch information
1 parent
64ec33b
commit 6a0ca15
Showing
9 changed files
with
89 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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,64 @@ | ||
import { toString } from '../hex/functions' | ||
import { Hex, HexCoordinates } from '../hex/types' | ||
|
||
export interface Cache<T> { | ||
readonly last: T | undefined | ||
readonly size: number | ||
clear(): void | ||
delete(id: unknown): boolean | ||
forEach(fn: (item: T, id: unknown, cache: this) => void): void | ||
get(id: unknown): T | undefined | ||
has(id: unknown): boolean | ||
set(id: unknown): this | ||
} | ||
|
||
// todo: experiment with cache that serializes and deserializes hexes (users should probably implement it) | ||
// todo: add "dummy cache" that doesn't cache (for the purpose of performance) | ||
export class CoordinatesCache<T extends Hex> implements Cache<T> { | ||
static of<T extends Hex>(hexes?: T[]) { | ||
return new CoordinatesCache(hexes) | ||
} | ||
|
||
private cache = new Map<string, T>() | ||
|
||
get last() { | ||
return Array.from(this.cache.values()).pop() | ||
} | ||
|
||
get size() { | ||
return this.cache.size | ||
} | ||
|
||
constructor(hexes: T[] = []) { | ||
this.cache = new Map(hexes.map((hex) => [hex.toString(), hex])) | ||
} | ||
|
||
clear() { | ||
this.cache.clear() | ||
} | ||
|
||
delete(coordinates: string | HexCoordinates) { | ||
return this.cache.delete(stringifyCoordinates(coordinates)) | ||
} | ||
|
||
forEach(fn: (hex: T, id: string, cache: this) => void) { | ||
this.cache.forEach((hex, id) => fn(hex, id, this)) | ||
} | ||
|
||
get(coordinates: string | HexCoordinates) { | ||
return this.cache.get(stringifyCoordinates(coordinates)) | ||
} | ||
|
||
has(coordinates: string | HexCoordinates) { | ||
return this.cache.has(stringifyCoordinates(coordinates)) | ||
} | ||
|
||
set(hex: T) { | ||
this.cache.set(stringifyCoordinates(hex), hex) | ||
return this | ||
} | ||
} | ||
|
||
function stringifyCoordinates(coordinates: string | HexCoordinates) { | ||
return typeof coordinates === 'string' ? coordinates : toString(coordinates) | ||
} |
This file contains 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 @@ | ||
export * from './coordinatesCache' |
This file contains 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
This file contains 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
This file contains 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
This file contains 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,3 @@ | ||
import { HexCoordinates } from '../types' | ||
|
||
export const toString = ({ q, r }: HexCoordinates) => `${q},${r}` |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.