Skip to content

Commit

Permalink
362ms -> 198ms. Replace shared_ptr's with raw pointers (#2)
Browse files Browse the repository at this point in the history
- smart pointers? more like suck pointers
  • Loading branch information
Jumbub committed Oct 2, 2021
1 parent cda613a commit 73e6448
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions results/benchmark.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
----------------------------------------------------------------------------
Benchmark Time CPU Iterations
----------------------------------------------------------------------------
BM_NextBoard/min_time:5.000 356 ms 356 ms 20
BM_RenderNextBoard/min_time:5.000 362 ms 362 ms 19
BM_NextBoard/min_time:5.000 190 ms 190 ms 36
BM_RenderNextBoard/min_time:5.000 198 ms 196 ms 35
2 changes: 1 addition & 1 deletion src/board/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ using Cell = Uint32;
const Cell ALIVE = SDL_MAX_UINT32;
const Cell DEAD = 0;

using Board = std::tuple<std::shared_ptr<Cell[]>, int, int>;
using Board = std::tuple<Cell*, int, int>;
using NeighbourPositions = std::array<int, 8>;
4 changes: 2 additions & 2 deletions src/board/generate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <tuple>

Board randomBoard(int width, int height) {
auto board = std::shared_ptr<Cell[]>(new Cell[width * height]);
auto board = new Cell[width * height];
for (int i = 0; i < height * width; ++i)
board[i] = rand() % 2 ? ALIVE : DEAD;
return {board, width, height};
Expand All @@ -25,7 +25,7 @@ Board benchmarkBoard(int width, int height) {
"Did not meet minimum height required for the benchmark board");

srand(0);
auto board = std::shared_ptr<Cell[]>(new Cell[width * height]);
auto board = new Cell[width * height];
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
const int i = y * width + x;
Expand Down
4 changes: 3 additions & 1 deletion src/board/next.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Board nextBoard(Board board) {
if (width == 0)
return board;

auto output = std::shared_ptr<Cell[]>(new Cell[width*height]);
auto output = new Cell[width*height];

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Expand Down Expand Up @@ -52,5 +52,7 @@ Board nextBoard(Board board) {
}
}

free(input);

return {output, width, height};
}
4 changes: 4 additions & 0 deletions src/entrypoints/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ static void BM_NextBoard(benchmark::State &state) {
for (auto _ : state) {
board = nextBoard(board);
}

free(get<0>(board));
}

static void BM_RenderNextBoard(benchmark::State &state) {
Expand All @@ -36,6 +38,8 @@ static void BM_RenderNextBoard(benchmark::State &state) {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();

free(get<0>(board));
}

BENCHMARK(BM_NextBoard)->Unit(benchmark::kMillisecond)->MinTime(5);
Expand Down
3 changes: 3 additions & 0 deletions src/entrypoints/interactive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ int main() {
else if (event.type == SDL_KEYDOWN &&
event.key.keysym.scancode == SDL_SCANCODE_RETURN) {

free(get<0>(board));
board = boardForSdlWindow(window);
int width, height;
SDL_GetWindowSize(window, &width, &height);
Expand All @@ -59,6 +60,8 @@ int main() {
stopProfiling(p3, "Rendered next board");
}

free(get<0>(board));

SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
Expand Down
2 changes: 1 addition & 1 deletion src/entrypoints/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Board generate(std::vector<std::vector<Cell>> input) {
const auto height = (int)input.size();
const auto width = (int)input[0].size();

auto board = std::shared_ptr<Cell[]>(new Cell[width * height]);
auto board = new Cell[width * height];
for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x)
board[y * width + x] = input[y][x] ? ALIVE : DEAD;
Expand Down

0 comments on commit 73e6448

Please sign in to comment.