Skip to content

Commit

Permalink
Improve cell speed
Browse files Browse the repository at this point in the history
  • Loading branch information
blaumeise20 committed Aug 18, 2021
1 parent c71da82 commit f5dc19f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 36 deletions.
63 changes: 45 additions & 18 deletions src/renderer/core/cellUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@
import { Position } from "../utils/positions";
import { CellType, Direction } from "./cell";
import { Cell, CellType, Direction } from "./cell";
import { CellGrid } from "./grid";

export function doStep(grid: CellGrid) {
// TODO: add all cells

console.time("update");
for (const updateType of updateOrder) {
console.log(updateType);
console.time("something");
switch (updateType[1]) {
case UpdateType.Directional:
for (const dir of directionalUpdateOrder) {
grid.cellList
.filter(c => c.type == updateType[0] && c.direction == dir)
.orderBy(c => orderFn(c.pos, dir))
.forEach(c => c.update());
// linked list
let cells: ListNode<Cell> = null;
for (const cell of grid.cellList) {
if (cell.type == updateType[0] && cell.direction == dir) {
const cellPosition = order[dir](cell.pos);

if (cells == null) cells = { e: cell, o: cellPosition, n: null };
else {
let p: ListNode<Cell> = null as any;
let c: ListNode<Cell> = cells as any;

while (c != null && c.o < cellPosition) {
p = c;
c = c.n;
}

if (p) p.n = { e: cell, o: cellPosition, n: c };
else cells = { e: cell, o: cellPosition, n: c };
}
}
}

while (cells) {
cells.e.update();
cells = cells.n;
}
}

break;
case UpdateType.Random:
grid.cellList
.filter(c => c.type == updateType[0])
.forEach(c => c.update());
for (const cell of grid.cellList) if (cell.type == updateType[0]) cell.update();
// grid.cellList
// .filter(c => c.type == updateType[0])
// .forEach(c => c.update());
break;
}
console.timeEnd("something");
}
console.timeEnd("update");
}

export enum UpdateType {
Expand All @@ -40,13 +62,11 @@ export const directionalUpdateOrder = [
Direction.Down,
]

export function orderFn(pos: Position, dir: Direction) {
switch (dir) {
case Direction.Right: return -pos.x;
case Direction.Down: return pos.y;
case Direction.Left: return pos.x;
case Direction.Up: return -pos.y;
}
export const order = {
[Direction.Right]: (pos: Position) => -pos.x,
[Direction.Down]: (pos: Position) => pos.y,
[Direction.Left]: (pos: Position) => pos.x,
[Direction.Up]: (pos: Position) => -pos.y,
}

export const updateOrder: [CellType, UpdateType][] = [
Expand All @@ -55,3 +75,10 @@ export const updateOrder: [CellType, UpdateType][] = [
[CellType.CCWrotator, UpdateType.Random],
[CellType.Mover, UpdateType.Directional],
]


export type ListNode<T> = {
e: T,
o: number,
n: ListNode<T>,
} | null;
61 changes: 43 additions & 18 deletions test.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,56 @@
*rotator:*
2.669921875
3.489990234375
2.593017578125
2.645751953125
2.669921875 ms
3.489990234375 ms
2.593017578125 ms
2.645751953125 ms
---
2.998046875
2.211181640625
2.77294921875
2.510009765625
2.998046875 ms
2.211181640625 ms
2.77294921875 ms
2.510009765625 ms

*generator:*
57.296142578125
58.430908203125
61.273193359375
57.257080078125
57.296142578125 ms
58.430908203125 ms
61.273193359375 ms
57.257080078125 ms
---
57.089111328125
55.9609375
55.7788085937
55.278076171875
57.089111328125 ms
55.9609375 ms
55.7788085937 ms
55.278076171875 ms

**code:**

*total:*
99.848876953125 ms
98.705810546875 ms
79.242919921875 ms
64.0419921875 ms
64.703857421875 ms
95.8212890625 ms
84.55615234375 ms
96.125244140625 ms
88.8779296875 ms
96.297119140625 ms
---
61.031982421875 ms
64.552001953125 ms
61.676025390625 ms
61.530029296875 ms
64.60498046875 ms
60.756103515625 ms
67.674072265625 ms
60.9208984375 ms
61.15087890625 ms
63.807861328125 ms

**nuke code:**
V3;1q;1q;{(0(+$)Ai2{(0(1m)S0{(0(+&);;



### v1 cells

V1;10;20;0.0;;;

V1;100;100;;0.0.0.0,3.0.7.0,1.0.0.7;;
Expand All @@ -41,4 +67,3 @@ V1;20;20;;4.0.0.0;; slide
V1;20;20;;7.0.0.0;; enemy
V1;20;20;;8.0.0.0;; trash
V1;20;20;;6.0.0.0;; immovable

0 comments on commit f5dc19f

Please sign in to comment.