Skip to content

Commit

Permalink
build(tiledit): add benchmark target to check for performance regress…
Browse files Browse the repository at this point in the history
…ions

- Enable LTO for release builds for performance reasons
- Flatten SircImatge pixelData to a 1D array to make it easier to work with in STL algorithms
- General refactoring to pull out duplicated logic
  • Loading branch information
NoxHarmonium committed Aug 5, 2024
1 parent aea8f3e commit 8bc442c
Show file tree
Hide file tree
Showing 22 changed files with 3,792 additions and 155 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/sirc-tiledit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,16 @@ jobs:
- name: Configure Project
run: |
meson --version
meson setup --buildtype release build-release/
meson setup --buildtype release build-release/ -Db_lto=true
- name: Compile Project (Release)
run: |
cd build-release
meson compile -v
# The results won't be very useful but it will ensure the benchmarks still compile and run
- name: Run Benchmark (Release)
run: |
cd build-release
ninja benchmark
- name: Create release
run: |
cd build-release
Expand Down
1 change: 1 addition & 0 deletions sirc-tiledit/.clang-format-ignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
libs/shared/tests/catch2/*
libs/shared/benchmarks/nanobench/*
1 change: 1 addition & 0 deletions sirc-tiledit/.clang-tidy-ignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
libs/shared/tests/catch2/*
libs/shared/benchmarks/nanobench/*
2 changes: 1 addition & 1 deletion sirc-tiledit/libs/gui/src/pixmapadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ QPixmap PixmapAdapter::sircImageToPixmap(const SircImage &sircImage) {
for (int x = 0; x < WIDTH_PIXELS; x++) {
for (int y = 0; y < HEIGHT_PIXELS; y++) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index)
const auto paletteColor = pixelData[x][y];
const auto paletteColor = pixelData[(x * WIDTH_PIXELS) + y];

Check warning on line 57 in sirc-tiledit/libs/gui/src/pixmapadapter.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/gui/src/pixmapadapter.cpp#L57

Added line #L57 was not covered by tests
assert(paletteColor < palette.size());
const auto sircColor = palette[paletteColor];

Expand Down
105 changes: 105 additions & 0 deletions sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "nanobench/nanobench.h"

#include <filesystem>
#include <fstream>

#include "testconfig.h"
#include <imageloader.hpp>
#include <mediancutquantizer.hpp>
#include <rgbaadapter.hpp>
#include <sircimage.hpp>

SircImage setupBenchmark(const std::filesystem::path &inputPath) {

Check warning on line 12 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L12

Added line #L12 was not covered by tests
const std::filesystem::path testRootPath(BENCHMARK_ROOT);

const auto inputPixelData =
ImageLoader::loadImageFromPng((testRootPath / inputPath).c_str());
return RgbaAdapter::rgbaToSircImage(inputPixelData);
}

int main() {

Check warning on line 20 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L20

Added line #L20 was not covered by tests
const auto pixelArtBackgroundImage = setupBenchmark(
std::filesystem::path("resources/pixel_art_background.png"));
const auto gradientImage =
setupBenchmark(std::filesystem::path("resources/gradient.png"));
const auto redfloweringGumImage =
setupBenchmark(std::filesystem::path("resources/red_flowering_gum.png"));
const auto quantizer = MedianCutQuantizer();
constexpr int epochs = 500;

Check warning on line 28 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L27-L28

Added lines #L27 - L28 were not covered by tests

std::ofstream pixelArtBackground2BppPyPerf("pixel_art_background-2bpp.json");
std::ofstream pixelArtBackground4BppPyPerf("pixel_art_background-4bpp.json");
std::ofstream gradient2BppPyPerf("gradient-2bpp.json");
std::ofstream gradient4BppPyPerf("gradient-4bpp.json");
std::ofstream redFloweringGum2BppPyPerf("red_flowering_gum-2bpp.json");
std::ofstream redFloweringGum4BppPyPerf("red_flowering_gum-4bpp.json");
std::ofstream redFloweringGum8BppPyPerf("red_flowering_gum-8bpp.json");

ankerl::nanobench::Bench()
.epochs(epochs)
.run("Quantizes a real test image correctly (pixel_art_background/2bpp)",

Check warning on line 40 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L39-L40

Added lines #L39 - L40 were not covered by tests
[&] {
ankerl::nanobench::doNotOptimizeAway(quantizer.quantize(
pixelArtBackgroundImage, PaletteReductionBpp::TwoBpp));
})

Check warning on line 44 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L44

Added line #L44 was not covered by tests
.render(ankerl::nanobench::templates::pyperf(),
pixelArtBackground2BppPyPerf);

ankerl::nanobench::Bench()
.epochs(epochs)
.run("Quantizes a real test image correctly (pixel_art_background/4bpp)",

Check warning on line 50 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L49-L50

Added lines #L49 - L50 were not covered by tests
[&] {
ankerl::nanobench::doNotOptimizeAway(quantizer.quantize(
pixelArtBackgroundImage, PaletteReductionBpp::FourBpp));
})

Check warning on line 54 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L54

Added line #L54 was not covered by tests
.render(ankerl::nanobench::templates::pyperf(),
pixelArtBackground4BppPyPerf);

ankerl::nanobench::Bench()
.epochs(epochs)
.run("Quantizes a real test image correctly (gradient/2bpp)",

Check warning on line 60 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L59-L60

Added lines #L59 - L60 were not covered by tests
[&] {
ankerl::nanobench::doNotOptimizeAway(quantizer.quantize(
gradientImage, PaletteReductionBpp::TwoBpp));
})

Check warning on line 64 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L64

Added line #L64 was not covered by tests
.render(ankerl::nanobench::templates::pyperf(), gradient2BppPyPerf);

ankerl::nanobench::Bench()
.epochs(epochs)
.run("Quantizes a real test image correctly (gradient/4bpp)",

Check warning on line 69 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L68-L69

Added lines #L68 - L69 were not covered by tests
[&] {
ankerl::nanobench::doNotOptimizeAway(quantizer.quantize(
gradientImage, PaletteReductionBpp::FourBpp));
})

Check warning on line 73 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L73

Added line #L73 was not covered by tests
.render(ankerl::nanobench::templates::pyperf(), gradient4BppPyPerf);

ankerl::nanobench::Bench()
.epochs(epochs)
.run("Quantizes a real test image correctly (red_flowering_gum/2bpp)",

Check warning on line 78 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L77-L78

Added lines #L77 - L78 were not covered by tests
[&] {
ankerl::nanobench::doNotOptimizeAway(quantizer.quantize(
redfloweringGumImage, PaletteReductionBpp::TwoBpp));
})

Check warning on line 82 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L82

Added line #L82 was not covered by tests
.render(ankerl::nanobench::templates::pyperf(),
redFloweringGum2BppPyPerf);

ankerl::nanobench::Bench()
.epochs(epochs)
.run("Quantizes a real test image correctly (red_flowering_gum/4bpp)",

Check warning on line 88 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L87-L88

Added lines #L87 - L88 were not covered by tests
[&] {
ankerl::nanobench::doNotOptimizeAway(quantizer.quantize(
redfloweringGumImage, PaletteReductionBpp::FourBpp));
})

Check warning on line 92 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L92

Added line #L92 was not covered by tests
.render(ankerl::nanobench::templates::pyperf(),
redFloweringGum4BppPyPerf);

ankerl::nanobench::Bench()
.epochs(epochs)
.run("Quantizes a real test image correctly (red_flowering_gum/8bpp)",

Check warning on line 98 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L97-L98

Added lines #L97 - L98 were not covered by tests
[&] {
ankerl::nanobench::doNotOptimizeAway(quantizer.quantize(
redfloweringGumImage, PaletteReductionBpp::None));
})

Check warning on line 102 in sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp

View check run for this annotation

Codecov / codecov/patch

sirc-tiledit/libs/shared/benchmarks/benchmarks.cpp#L102

Added line #L102 was not covered by tests
.render(ankerl::nanobench::templates::pyperf(),
redFloweringGum8BppPyPerf);
}
24 changes: 24 additions & 0 deletions sirc-tiledit/libs/shared/benchmarks/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
conf = configuration_data()
conf.set_quoted('BENCHMARK_ROOT', meson.source_root() / 'libs/shared/benchmarks')
configure_file(
output : 'testconfig.h',
configuration : conf
)

nanobench_lib = library(
'nanobench',
['./nanobench/nanobench.cpp'],
version : '4.3.11',
)


benchmark(
'AllBenchmarks',
executable(
'benchmarks',
['benchmarks.cpp'],
include_directories : shared_library_include_files,
link_with : [nanobench_lib, shared_lib],
),
)

3 changes: 3 additions & 0 deletions sirc-tiledit/libs/shared/benchmarks/nanobench/nanobench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define ANKERL_NANOBENCH_IMPLEMENT

#include "./nanobench.h"
Loading

0 comments on commit 8bc442c

Please sign in to comment.