Replace assert with assume for release build optimization (#1390)

This commit is contained in:
Sylvie
2024-04-02 11:09:31 -04:00
committed by GitHub
parent 1d39e5ed56
commit a234da42a6
26 changed files with 158 additions and 147 deletions

View File

@@ -3,7 +3,6 @@
#include "gfx/main.hpp"
#include <algorithm>
#include <assert.h>
#include <ctype.h>
#include <inttypes.h>
#include <ios>
@@ -17,6 +16,7 @@
#include "extern/getopt.hpp"
#include "file.hpp"
#include "helpers.hpp" // assume
#include "platform.hpp"
#include "version.hpp"
@@ -633,7 +633,7 @@ int main(int argc, char *argv[]) {
if (musl_optind != curArgc) {
// This happens if `--` is passed, process the remaining arg(s) as positional
assert(musl_optind < curArgc);
assume(musl_optind < curArgc);
for (int i = musl_optind; i < curArgc; ++i) {
registerInput(argv[i]);
}
@@ -845,7 +845,7 @@ int main(int argc, char *argv[]) {
void Palette::addColor(uint16_t color) {
for (size_t i = 0; true; ++i) {
assert(i < colors.size()); // The packing should guarantee this
assume(i < colors.size()); // The packing should guarantee this
if (colors[i] == color) { // The color is already present
break;
} else if (colors[i] == UINT16_MAX) { // Empty slot

View File

@@ -3,7 +3,6 @@
#include "gfx/pal_packing.hpp"
#include <algorithm>
#include <assert.h>
#include <deque>
#include <inttypes.h>
#include <optional>
@@ -107,7 +106,7 @@ private:
return it;
}
reference operator*() const {
assert((*_iter).has_value());
assume((*_iter).has_value());
return **_iter;
}
pointer operator->() const {
@@ -308,7 +307,7 @@ static void decant(
// Build up the "component"...
colors.clear();
members.clear();
assert(members.empty()); // Compiler optimization hint
assume(members.empty()); // Compiler optimization hint
do {
ProtoPalette const &protoPal = protoPalettes[attrs->protoPalIndex];
// If this is the first proto-pal, or if at least one color matches, add it

View File

@@ -3,7 +3,6 @@
#include "gfx/pal_sorting.hpp"
#include <algorithm>
#include <assert.h>
#include "helpers.hpp"
@@ -55,7 +54,7 @@ void grayscale(
// This method is only applicable if there are at most as many colors as colors per palette, so
// we should only have a single palette.
assert(palettes.size() == 1);
assume(palettes.size() == 1);
Palette &palette = palettes[0];
std::fill(RANGE(palette.colors), Rgba::transparent);

View File

@@ -3,7 +3,6 @@
#include "gfx/pal_spec.hpp"
#include <algorithm>
#include <assert.h>
#include <charconv>
#include <fstream>
#include <inttypes.h>
@@ -26,13 +25,13 @@ using namespace std::string_view_literals;
constexpr uint8_t nibble(char c) {
if (c >= 'a') {
assert(c <= 'f');
assume(c <= 'f');
return c - 'a' + 10;
} else if (c >= 'A') {
assert(c <= 'F');
assume(c <= 'F');
return c - 'A' + 10;
} else {
assert(c >= '0' && c <= '9');
assume(c >= '0' && c <= '9');
return c - '0';
}
}
@@ -59,8 +58,8 @@ void parseInlinePalSpec(char const * const rawArg) {
auto parseError = [&rawArg, &arg](size_type ofs, size_type len, char const *msg) {
(void)arg; // With NDEBUG, `arg` is otherwise not used
assert(ofs <= arg.length());
assert(len <= arg.length());
assume(ofs <= arg.length());
assume(len <= arg.length());
errorMessage(msg);
fprintf(
@@ -178,7 +177,7 @@ void parseInlinePalSpec(char const * const rawArg) {
*/
template<size_t n>
static bool readMagic(std::filebuf &file, char const *magic) {
assert(strlen(magic) == n);
assume(strlen(magic) == n);
char magicBuf[n];
return file.sgetn(magicBuf, n) == n && memcmp(magicBuf, magic, n);

View File

@@ -3,7 +3,6 @@
#include "gfx/process.hpp"
#include <algorithm>
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <optional>
@@ -48,7 +47,7 @@ public:
if (!slot.has_value()) {
slot.emplace(rgba);
} else if (*slot != rgba) {
assert(slot->cgbColor() != UINT16_MAX);
assume(slot->cgbColor() != UINT16_MAX);
return &*slot;
}
return nullptr;
@@ -270,7 +269,7 @@ public:
if (png_get_PLTE(png, info, &embeddedPal, &nbColors) != 0) {
if (png_get_tRNS(png, info, &transparencyPal, &nbTransparentEntries, nullptr)) {
assert(nbTransparentEntries <= nbColors);
assume(nbTransparentEntries <= nbColors);
}
options.verbosePrint(
@@ -326,16 +325,16 @@ public:
// Update `info` with the transformations
png_read_update_info(png, info);
// These shouldn't have changed
assert(png_get_image_width(png, info) == width);
assert(png_get_image_height(png, info) == height);
assume(png_get_image_width(png, info) == width);
assume(png_get_image_height(png, info) == height);
// These should have changed, however
assert(png_get_color_type(png, info) == PNG_COLOR_TYPE_RGBA);
assert(png_get_bit_depth(png, info) == 8);
assume(png_get_color_type(png, info) == PNG_COLOR_TYPE_RGBA);
assume(png_get_bit_depth(png, info) == 8);
// Now that metadata has been read, we can process the image data
size_t nbRowBytes = png_get_rowbytes(png, info);
assert(nbRowBytes != 0);
assume(nbRowBytes != 0);
DefaultInitVec<png_byte> row(nbRowBytes);
// Holds known-conflicting color pairs to avoid warning about them twice.
// We don't need to worry about transitivity, as ImagePalette slots are immutable once
@@ -394,7 +393,7 @@ public:
}
}
} else {
assert(interlaceType == PNG_INTERLACE_ADAM7);
assume(interlaceType == PNG_INTERLACE_ADAM7);
// For interlace to work properly, we must read the image `nbPasses` times
for (int pass = 0; pass < PNG_INTERLACE_ADAM7_PASSES; ++pass) {
@@ -549,7 +548,7 @@ static void generatePalSpec(Png const &png) {
// Fill in the palette spec
options.palSpec.emplace_back(); // A single palette, with `#00000000`s (transparent)
assert(options.palSpec.size() == 1);
assume(options.palSpec.size() == 1);
if (embPalSize > options.maxOpaqueColors()) { // Ignore extraneous colors if they are unused
embPalSize = options.maxOpaqueColors();
}
@@ -568,7 +567,7 @@ static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>>
// Run a "pagination" problem solver
// TODO: allow picking one of several solvers?
auto [mappings, nbPalettes] = packing::overloadAndRemove(protoPalettes);
assert(mappings.size() == protoPalettes.size());
assume(mappings.size() == protoPalettes.size());
if (options.verbosity >= Options::VERB_INTERM) {
fprintf(
@@ -647,7 +646,7 @@ static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>>
});
if (iter == palettes.end()) {
assert(!protoPal.empty());
assume(!protoPal.empty());
error("Failed to fit tile colors [%s] in specified palettes", listColors(protoPal));
bad = true;
}
@@ -725,7 +724,7 @@ public:
for (uint32_t x = 0; x < 8; ++x) {
row <<= 1;
uint8_t index = palette.indexOf(tile.pixel(x, y).cgbColor());
assert(index < palette.size()); // The color should be in the palette
assume(index < palette.size()); // The color should be in the palette
if (index & 1) {
row |= 1;
}
@@ -803,7 +802,7 @@ public:
}
// If we have both (i.e. we have symmetry), default to vflip only
assert(hasVFlip || hasVHFlip);
assume(hasVFlip || hasVHFlip);
return hasVFlip ? MatchType::VFLIP : MatchType::VHFLIP;
}
friend bool operator==(TileData const &lhs, TileData const &rhs) {
@@ -854,7 +853,7 @@ static void outputTileData(
break;
}
}
assert(remainingTiles == 0);
assume(remainingTiles == 0);
}
static void outputMaps(
@@ -877,7 +876,7 @@ static void outputMaps(
uint8_t bank = 0;
for (auto attr : attrmap) {
if (tileID == options.maxNbTiles[bank]) {
assert(bank == 0);
assume(bank == 0);
bank = 1;
tileID = 0;
}
@@ -976,7 +975,7 @@ static void outputTileData(UniqueTiles const &tiles) {
uint16_t tileID = 0;
for (auto iter = tiles.begin(), end = tiles.end() - options.trim; iter != end; ++iter) {
TileData const *tile = *iter;
assert(tile->tileID == tileID);
assume(tile->tileID == tileID);
++tileID;
output->sputn(reinterpret_cast<char const *>(tile->data().data()), options.bitDepth * 8);
}

View File

@@ -3,7 +3,6 @@
#include "gfx/proto_palette.hpp"
#include <algorithm>
#include <assert.h>
#include "helpers.hpp"
@@ -41,8 +40,8 @@ bool ProtoPalette::add(uint16_t color) {
ProtoPalette::ComparisonResult ProtoPalette::compare(ProtoPalette const &other) const {
// This works because the sets are sorted numerically
assert(std::is_sorted(RANGE(_colorIndices)));
assert(std::is_sorted(RANGE(other._colorIndices)));
assume(std::is_sorted(RANGE(_colorIndices)));
assume(std::is_sorted(RANGE(other._colorIndices)));
auto ours = _colorIndices.begin(), theirs = other._colorIndices.begin();
bool weBigger = true, theyBigger = true;

View File

@@ -4,7 +4,6 @@
#include <algorithm>
#include <array>
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <optional>
@@ -14,6 +13,7 @@
#include "defaultinitalloc.hpp"
#include "file.hpp"
#include "helpers.hpp" // assume
#include "itertools.hpp"
#include "gfx/main.hpp"
@@ -42,7 +42,7 @@ static DefaultInitVec<uint8_t> readInto(std::string const &path) {
// Arbitrary, but if you got a better idea...
size_t newSize = oldSize != data.capacity() ? data.capacity() : oldSize * 2;
assert(oldSize != newSize);
assume(oldSize != newSize);
data.resize(newSize);
}
@@ -343,9 +343,9 @@ void reverse() {
tileID =
(*tilemap)[index] - options.baseTileIDs[bank] + bank * options.maxNbTiles[0];
}
assert(tileID < nbTileInstances); // Should have been checked earlier
assume(tileID < nbTileInstances); // Should have been checked earlier
size_t palID = palmap ? (*palmap)[index] : attribute & 0b111;
assert(palID < palettes.size()); // Should be ensured on data read
assume(palID < palettes.size()); // Should be ensured on data read
// We do not have data for tiles trimmed with `-x`, so assume they are "blank"
static std::array<uint8_t, 16> const trimmedTile{

View File

@@ -3,10 +3,11 @@
#include "gfx/rgba.hpp"
#include <algorithm>
#include <assert.h>
#include <math.h>
#include <stdint.h>
#include "helpers.hpp" // assume
#include "gfx/main.hpp" // options
/*
@@ -37,7 +38,7 @@ uint16_t Rgba::cgbColor() const {
if (isTransparent()) {
return transparent;
}
assert(isOpaque());
assume(isOpaque());
uint8_t r = red, g = green, b = blue;
if (options.useColorCurve) {
@@ -56,7 +57,7 @@ uint16_t Rgba::cgbColor() const {
}
uint8_t Rgba::grayIndex() const {
assert(isGray());
assume(isGray());
// Convert from [0; 256[ to [0; maxOpaqueColors[
return static_cast<uint16_t>(255 - red) * options.maxOpaqueColors() / 256;
}