mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-22 12:36:29 +00:00
Change the ring buffer to Uint32Array
Store only TileRef instead of packed tile+state values
This commit is contained in:
@@ -662,11 +662,15 @@ export class ClientGameRunner {
|
||||
|
||||
if (this.tileRingViews) {
|
||||
const MAX_TILE_UPDATES_PER_RENDER = 100000;
|
||||
const tileRefs: TileRef[] = [];
|
||||
drainTileUpdates(
|
||||
this.tileRingViews,
|
||||
MAX_TILE_UPDATES_PER_RENDER,
|
||||
combinedPackedTileUpdates,
|
||||
tileRefs,
|
||||
);
|
||||
for (const ref of tileRefs) {
|
||||
combinedPackedTileUpdates.push(BigInt(ref));
|
||||
}
|
||||
} else {
|
||||
for (const gu of batch) {
|
||||
gu.packedTileUpdates.forEach((tu) => {
|
||||
|
||||
@@ -37,7 +37,7 @@ export async function createGameRunner(
|
||||
clientID: ClientID,
|
||||
mapLoader: GameMapLoader,
|
||||
callBack: (gu: GameUpdateViewData | ErrorUpdate) => void,
|
||||
tileUpdateSink?: (update: bigint) => void,
|
||||
tileUpdateSink?: (tile: TileRef) => void,
|
||||
sharedStateBuffer?: SharedArrayBuffer,
|
||||
): Promise<GameRunner> {
|
||||
const config = await getConfig(gameStart.config, null);
|
||||
@@ -105,7 +105,7 @@ export class GameRunner {
|
||||
public game: Game,
|
||||
private execManager: Executor,
|
||||
private callBack: (gu: GameUpdateViewData | ErrorUpdate) => void,
|
||||
private tileUpdateSink?: (update: bigint) => void,
|
||||
private tileUpdateSink?: (tile: TileRef) => void,
|
||||
) {}
|
||||
|
||||
init() {
|
||||
@@ -186,7 +186,8 @@ export class GameRunner {
|
||||
const tileUpdates = updates[GameUpdateType.Tile];
|
||||
if (this.tileUpdateSink !== undefined) {
|
||||
for (const u of tileUpdates) {
|
||||
this.tileUpdateSink(u.update);
|
||||
const tileRef = Number(u.update >> 16n) as TileRef;
|
||||
this.tileUpdateSink(tileRef);
|
||||
}
|
||||
packedTileUpdates = new BigUint64Array();
|
||||
} else {
|
||||
|
||||
@@ -522,7 +522,7 @@ export class GameView implements GameMap {
|
||||
this.updatedTiles = [];
|
||||
if (this.usesSharedTileState) {
|
||||
this.lastUpdate.packedTileUpdates.forEach((tu) => {
|
||||
const tileRef = Number(tu >> 16n);
|
||||
const tileRef = Number(tu);
|
||||
this.updatedTiles.push(tileRef);
|
||||
});
|
||||
} else {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { TileRef } from "../game/GameMap";
|
||||
|
||||
export interface SharedTileRingBuffers {
|
||||
header: SharedArrayBuffer;
|
||||
data: SharedArrayBuffer;
|
||||
@@ -5,7 +7,7 @@ export interface SharedTileRingBuffers {
|
||||
|
||||
export interface SharedTileRingViews {
|
||||
header: Int32Array;
|
||||
buffer: BigUint64Array;
|
||||
buffer: Uint32Array;
|
||||
capacity: number;
|
||||
}
|
||||
|
||||
@@ -18,9 +20,7 @@ export function createSharedTileRingBuffers(
|
||||
capacity: number,
|
||||
): SharedTileRingBuffers {
|
||||
const header = new SharedArrayBuffer(3 * Int32Array.BYTES_PER_ELEMENT);
|
||||
const data = new SharedArrayBuffer(
|
||||
capacity * BigUint64Array.BYTES_PER_ELEMENT,
|
||||
);
|
||||
const data = new SharedArrayBuffer(capacity * Uint32Array.BYTES_PER_ELEMENT);
|
||||
return { header, data };
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ export function createSharedTileRingViews(
|
||||
buffers: SharedTileRingBuffers,
|
||||
): SharedTileRingViews {
|
||||
const header = new Int32Array(buffers.header);
|
||||
const buffer = new BigUint64Array(buffers.data);
|
||||
const buffer = new Uint32Array(buffers.data);
|
||||
return {
|
||||
header,
|
||||
buffer,
|
||||
@@ -38,7 +38,7 @@ export function createSharedTileRingViews(
|
||||
|
||||
export function pushTileUpdate(
|
||||
views: SharedTileRingViews,
|
||||
value: bigint,
|
||||
value: TileRef,
|
||||
): void {
|
||||
const { header, buffer, capacity } = views;
|
||||
|
||||
@@ -60,7 +60,7 @@ export function pushTileUpdate(
|
||||
export function drainTileUpdates(
|
||||
views: SharedTileRingViews,
|
||||
maxItems: number,
|
||||
out: bigint[],
|
||||
out: TileRef[],
|
||||
): void {
|
||||
const { header, buffer, capacity } = views;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import version from "../../../resources/version.txt";
|
||||
import { createGameRunner, GameRunner } from "../GameRunner";
|
||||
import { FetchGameMapLoader } from "../game/FetchGameMapLoader";
|
||||
import { TileRef } from "../game/GameMap";
|
||||
import { ErrorUpdate, GameUpdateViewData } from "../game/GameUpdates";
|
||||
import {
|
||||
createSharedTileRingViews,
|
||||
@@ -83,7 +84,7 @@ ctx.addEventListener("message", async (e: MessageEvent<MainThreadMessage>) => {
|
||||
mapLoader,
|
||||
gameUpdate,
|
||||
sharedTileRing
|
||||
? (update: bigint) => pushTileUpdate(sharedTileRing!, update)
|
||||
? (tile: TileRef) => pushTileUpdate(sharedTileRing!, tile)
|
||||
: undefined,
|
||||
message.sharedStateBuffer,
|
||||
).then((gr) => {
|
||||
|
||||
Reference in New Issue
Block a user