Skip to content

Instantly share code, notes, and snippets.

@sebastientromp
Created January 22, 2017 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastientromp/9bd65ff4bce6126e5d8d980db5c9adf9 to your computer and use it in GitHub Desktop.
Save sebastientromp/9bd65ff4bce6126e5d8d980db5c9adf9 to your computer and use it in GitHub Desktop.
class Cache {
static final int POINT_POOL_SIZE = 1000000;
static final int ACTION_POOL_SIZE = 200000;
static final int WIZARD_POOL_SIZE = 50000;
static final int SNAFFLE_POOL_SIZE = 100000;
static final int BLUDGER_POOL_SIZE = 20000;
static final int MAP_POINT_POOL_SIZE = 100000;
static final int GAME_STATE_POOL_SIZE = 100000;
static final Wizard[] wizardPool = new Wizard[WIZARD_POOL_SIZE];
static final Snaffle[] snafflePool = new Snaffle[SNAFFLE_POOL_SIZE];
static final Bludger[] bludgerPool = new Bludger[BLUDGER_POOL_SIZE];
static final Point[] pointPool = new Point[POINT_POOL_SIZE];
static final GameState[] gameStatePool = new GameState[GAME_STATE_POOL_SIZE];
static final Action[] actionPool = new Action[ACTION_POOL_SIZE];
static final MapPoint[] mapPointPool = new MapPoint[MAP_POINT_POOL_SIZE];
static int wizardIndex, snaffleIndex, bludgerIndex, pointIndex, gameStateIndex, actionIndex, mapPointIndex;
static int totalGameStateLoops, totalActionLoops, totalPointLoops, totalWizardLoops, totalSnaffleLoops,
totalBludgerLoops, totalMapPointsLoops;
static int gameStateClones, collisions, collisionTest, evals;
static long lastCollision, totalCollisionTime, lastEval, totalEvalTime;
static void initEntities() {
for (int i = 0; i < WIZARD_POOL_SIZE; i++) {
wizardPool[i] = new Wizard();
}
for (int i = 0; i < SNAFFLE_POOL_SIZE; i++) {
snafflePool[i] = new Snaffle();
}
for (int i = 0; i < BLUDGER_POOL_SIZE; i++) {
bludgerPool[i] = new Bludger();
}
for (int i = 0; i < POINT_POOL_SIZE; i++) {
pointPool[i] = new Point();
}
for (int i = 0; i < GAME_STATE_POOL_SIZE; i++) {
gameStatePool[i] = new GameState();
}
for (int i = 0; i < ACTION_POOL_SIZE; i++) {
actionPool[i] = new Action();
}
for (int i = 0; i < MAP_POINT_POOL_SIZE; i++) {
mapPointPool[i] = new MapPoint();
}
}
public static void newTurn() {
gameStateIndex = 0;
totalGameStateLoops = 0;
actionIndex = 0;
totalActionLoops = 0;
pointIndex = 0;
totalPointLoops = 0;
wizardIndex = 0;
totalWizardLoops = 0;
snaffleIndex = 0;
totalSnaffleLoops = 0;
bludgerIndex = 0;
totalBludgerLoops = 0;
mapPointIndex = 0;
totalMapPointsLoops = 0;
gameStateClones = 0;
collisions = 0;
collisionTest = 0;
lastCollision = 0;
totalCollisionTime = 0;
lastEval = 0;
totalEvalTime = 0;
}
public static void endTurn() {
// System.err.println("Used " + (gameStateIndex + 10000 *
// totalGameStateLoops) + " game states");
// System.err.println("Used " + (actionIndex + ACTION_POOL_SIZE *
// totalActionLoops) + " actions");
// System.err.println("Used " + (pointIndex + POINT_POOL_SIZE *
// totalPointLoops) + " points");
// System.err.println("Used " + (wizardIndex + WIZARD_POOL_SIZE *
// totalWizardLoops) + " wizards");
// System.err.println("Used " + (snaffleIndex + SNAFFLE_POOL_SIZE *
// totalSnaffleLoops) + " snaffles");
// System.err.println("Used " + (bludgerIndex + BLUDGER_POOL_SIZE *
// totalBludgerLoops) + " bludgers");
// System.err.println("Used " + (mapPointIndex + MAP_POINT_POOL_SIZE *
// totalMapPointsLoops) + " map points");
// System.err.println("Cloned " + gameStateClones + " gamestates");
// System.err.println("Had " + collisions + " collisions");
// System.err.println("Tested for " + collisionTest + " collision
// tests");
// System.err.println("Spend " + totalCollisionTime / 1000000 + "ms
// testing for collisions");
// System.err.println(
// "\tWhich is " + 1.0 * totalCollisionTime / 1000 / collisionTest + "
// microseconds per collision test");
//
// System.err.println("Evaluated " + evals + " game states");
// System.err.println("Spend " + totalEvalTime / 1000000 + "ms
// evaluating game states");
// System.err.println("\tWhich is " + 1.0 * totalEvalTime / 1000 / evals
// + " microseconds per eval");
}
// TODO: remova the monitoring elements, as it takes some computing time
static Wizard newWizard() {
// return new Wizard();
// return wizardPool[wizardIndex++ % 1000].clear();
wizardIndex++;
if (wizardIndex >= WIZARD_POOL_SIZE) {
wizardIndex = wizardIndex - WIZARD_POOL_SIZE;
// totalWizardLoops++;
}
return wizardPool[wizardIndex].clear();
}
static Snaffle newSnaffle() {
// return new Snaffle();
// return snafflePool[snaffleIndex++ % 1000].clear();
snaffleIndex++;
if (snaffleIndex >= SNAFFLE_POOL_SIZE) {
snaffleIndex = snaffleIndex - SNAFFLE_POOL_SIZE;
// totalSnaffleLoops++;
}
return snafflePool[snaffleIndex].clear();
}
static Bludger newBludger() {
// return new Bludger();
// return bludgerPool[bludgerIndex++ % 1000].clear();
bludgerIndex++;
if (bludgerIndex >= BLUDGER_POOL_SIZE) {
bludgerIndex = bludgerIndex - BLUDGER_POOL_SIZE;
// totalBludgerLoops++;
}
return bludgerPool[bludgerIndex].clear();
}
static Point newPoint() {
// return new Point();
// return pointPool[pointIndex++ % 10000].clear();
pointIndex++;
if (pointIndex >= POINT_POOL_SIZE) {
pointIndex = pointIndex - POINT_POOL_SIZE;
// Rmoving monitoring counters goes down from 21 ms to 14 ms to
// instantiate 5M points
// totalPointLoops++;
}
// Not calling .clear() means getting from 39 to 21 ms to instantiate 5M
// points
return pointPool[pointIndex];
}
static GameState newGameState() {
// return new GameState();
// 93ms to get 1M game states
gameStateIndex++;
if (gameStateIndex >= GAME_STATE_POOL_SIZE) {
gameStateIndex = gameStateIndex - GAME_STATE_POOL_SIZE;
// totalGameStateLoops++;
}
// We only need to clear the snaffles, as they are the only entities
// that can disappear. Down to 63 ms for 1M states
return gameStatePool[gameStateIndex].clear();
}
static Action newAction() {
// return new Action();
actionIndex++;
if (actionIndex >= ACTION_POOL_SIZE) {
actionIndex = actionIndex - ACTION_POOL_SIZE;
// totalActionLoops++;
}
return actionPool[actionIndex].clear();
}
public static Solution newSolution() {
return new Solution();
}
public static Step newStep() {
return new Step();
}
public static MapPoint newMapPoint() {
// return new MapPoint();
mapPointIndex++;
if (mapPointIndex >= MAP_POINT_POOL_SIZE) {
mapPointIndex = mapPointIndex - MAP_POINT_POOL_SIZE;
// totalMapPointsLoops++;
}
// We only need to clear the snaffles, as they are the only entities
// that can disappear. Down to 63 ms for 1M states
return mapPointPool[mapPointIndex];
}
// public static void logClone() {
// gameStateClones++;
// }
//
// public static void logCollision() {
// collisions++;
// }
//
// public static void logCollisionTest() {
// collisionTest++;
// lastCollision = System.nanoTime();
// }
//
// public static void logCollisionEnd() {
// totalCollisionTime += System.nanoTime() - lastCollision;
// }
//
// public static void logEval() {
// evals++;
// lastEval = System.nanoTime();
// }
//
// public static void logEvalEnd() {
// totalEvalTime += System.nanoTime() - lastEval;
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment