Skip to content

Commit

Permalink
Reduce the size of some enums and structs
Browse files Browse the repository at this point in the history
By removing padding, essentially. While trying to keep semantically-relevant
fields together.
This should bump performance slightly, simply from moving less data around
(especially in the lexer, which creates a bunch of contexts all the time)
  • Loading branch information
ISSOtm authored and Rangi42 committed Aug 27, 2024
1 parent 3557ecc commit 35164f5
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 41 deletions.
7 changes: 4 additions & 3 deletions include/asm/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <stdint.h>
#include <string>

enum FormatState {
enum FormatState : unsigned char {
FORMAT_SIGN, // expects '+' or ' ' (optional)
FORMAT_PREFIX, // expects '#' (optional)
FORMAT_ALIGN, // expects '-' (optional)
Expand All @@ -18,17 +18,18 @@ enum FormatState {
};

class FormatSpec {
FormatState state;
int sign;
bool prefix;
bool alignLeft;
bool padZero;
size_t width;
bool hasFrac;
size_t fracWidth;
size_t width;
int type;
bool valid;

FormatState state;

public:
bool isEmpty() const { return !state; }
bool isValid() const { return valid || state == FORMAT_DONE; }
Expand Down
18 changes: 9 additions & 9 deletions include/asm/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static_assert(LEXER_BUF_SIZE > 1, "Lexer buffer size is too small");
// This caps the size of buffer reads, and according to POSIX, passing more than SSIZE_MAX is UB
static_assert(LEXER_BUF_SIZE <= SSIZE_MAX, "Lexer buffer size is too large");

enum LexerMode {
enum LexerMode : unsigned char {
LEXER_NORMAL,
LEXER_RAW,
LEXER_SKIP_TO_ELIF,
Expand Down Expand Up @@ -80,23 +80,23 @@ struct IfStackEntry {
struct LexerState {
std::string path;

LexerMode mode;
std::deque<Expansion> expansions; // Front is the innermost current expansion
size_t macroArgScanDistance; // Max distance already scanned for macro args
bool disableMacroArgs;
bool disableInterpolation;
bool expandStrings;

bool atLineStart;
uint32_t lineNo;
uint32_t colNo;
int lastToken;

std::deque<IfStackEntry> ifStack;
LexerMode mode;

bool capturing; // Whether the text being lexed should be captured
size_t captureSize; // Amount of text captured
std::shared_ptr<std::vector<char>> captureBuf; // Buffer to send the captured text to if set

bool disableMacroArgs;
bool disableInterpolation;
size_t macroArgScanDistance; // Max distance already scanned for macro args
bool expandStrings;
std::deque<Expansion> expansions; // Front is the innermost current expansion
std::deque<IfStackEntry> ifStack;

Either<ViewedContent, BufferedContent> content;

Expand Down
2 changes: 1 addition & 1 deletion include/asm/output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
struct Expression;
struct FileStackNode;

enum StateFeature {
enum StateFeature : unsigned char {
STATE_EQU,
STATE_VAR,
STATE_EQUS,
Expand Down
2 changes: 1 addition & 1 deletion include/asm/rpn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ struct Expression {
std::string // Why the expression is not known, if it isn't
>
data = 0;
bool isSymbol = false; // Whether the expression represents a symbol suitable for const diffing
std::vector<uint8_t> rpn{}; // Bytes serializing the RPN expression
uint32_t rpnPatchSize = 0; // Size the expression will take in the object file
bool isSymbol = false; // Whether the expression represents a symbol suitable for const diffing

Expression() = default;
Expression(Expression &&) = default;
Expand Down
6 changes: 3 additions & 3 deletions include/asm/symbol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "asm/lexer.hpp"
#include "asm/section.hpp"

enum SymbolType {
enum SymbolType : unsigned char {
SYM_LABEL,
SYM_EQU,
SYM_VAR,
Expand All @@ -29,12 +29,12 @@ bool sym_IsPC(Symbol const *sym); // For the inline `getSection` method

struct Symbol {
std::string name;
std::shared_ptr<FileStackNode> src; // Where the symbol was defined
uint32_t fileLine; // Line where the symbol was defined
SymbolType type;
bool isExported; // Whether the symbol is to be exported
bool isBuiltin; // Whether the symbol is a built-in
Section *section;
std::shared_ptr<FileStackNode> src; // Where the symbol was defined
uint32_t fileLine; // Line where the symbol was defined

std::variant<
int32_t, // If isNumeric()
Expand Down
2 changes: 1 addition & 1 deletion include/asm/warning.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

extern unsigned int nbErrors, maxErrors;

enum WarningState { WARNING_DEFAULT, WARNING_DISABLED, WARNING_ENABLED, WARNING_ERROR };
enum WarningState : unsigned char { WARNING_DEFAULT, WARNING_DISABLED, WARNING_ENABLED, WARNING_ERROR };

enum WarningID {
WARNING_ASSERT, // Assertions
Expand Down
20 changes: 10 additions & 10 deletions include/gfx/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,39 @@
#include "gfx/rgba.hpp"

struct Options {
std::string input{}; // positional arg

bool useColorCurve = false; // -C
bool allowDedup = false; // -u
bool allowMirroringX = false; // -X, -m
bool allowMirroringY = false; // -Y, -m
bool columnMajor = false; // -Z
uint8_t verbosity = 0; // -v

std::string attrmap{}; // -a, -A
std::array<uint8_t, 2> baseTileIDs{0, 0}; // -b
enum {
enum : unsigned char {
NO_SPEC,
EXPLICIT,
EMBEDDED,
} palSpecType = NO_SPEC; // -c
} palSpecType = NO_SPEC; // -c
uint8_t bitDepth = 2; // -d
std::string attrmap{}; // -a, -A
std::array<uint8_t, 2> baseTileIDs{0, 0}; // -b
std::vector<std::array<std::optional<Rgba>, 4>> palSpec{};
uint8_t bitDepth = 2; // -d
struct {
uint16_t left;
uint16_t top;
uint16_t width;
uint16_t height;
} inputSlice{0, 0, 0, 0}; // -L (margins in clockwise order, like CSS)
std::array<uint16_t, 2> maxNbTiles{UINT16_MAX, 0}; // -N
uint16_t nbPalettes = 8; // -n
std::string output{}; // -o
std::string palettes{}; // -p, -P
std::string palmap{}; // -q, -Q
uint16_t reversedWidth = 0; // -r, in tiles
uint8_t nbColorsPerPal = 0; // -s; 0 means "auto" = 1 << bitDepth;
std::string tilemap{}; // -t, -T
uint64_t trim = 0; // -x

std::string input{}; // positional arg
uint16_t nbPalettes = 8; // -n
uint16_t reversedWidth = 0; // -r, in tiles
uint8_t nbColorsPerPal = 0; // -s; 0 means "auto" = 1 << bitDepth;

static constexpr uint8_t VERB_NONE = 0; // Normal, no extra output
static constexpr uint8_t VERB_CFG = 1; // Print configuration after parsing options
Expand Down
4 changes: 2 additions & 2 deletions include/link/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ extern bool disablePadding;
} while (0)

struct FileStackNode {
FileStackNodeType type;
Either<
std::vector<uint32_t>, // NODE_REPT
std::string // NODE_FILE, NODE_MACRO
>
data;
FileStackNodeType type;

FileStackNode *parent;
// Line at which the parent context was exited; meaningless for the root level
uint32_t lineNo;
FileStackNode *parent;

// REPT iteration counts since last named node, in reverse depth order
std::vector<uint32_t> &iters() { return data.get<std::vector<uint32_t>>(); }
Expand Down
2 changes: 1 addition & 1 deletion include/link/symbol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ struct Label {
struct Symbol {
// Info contained in the object files
std::string name;
ExportLevel type;
char const *objFileName;
FileStackNode const *src;
ExportLevel type;
int32_t lineNo;
Either<
int32_t, // Constants just have a numeric value
Expand Down
14 changes: 7 additions & 7 deletions include/linkdefs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#define RGBDS_OBJECT_VERSION_STRING "RGB9"
#define RGBDS_OBJECT_REV 11U

enum AssertionType { ASSERT_WARN, ASSERT_ERROR, ASSERT_FATAL };
enum AssertionType : unsigned char { ASSERT_WARN, ASSERT_ERROR, ASSERT_FATAL };

enum RPNCommand {
enum RPNCommand : unsigned char {
RPN_ADD = 0x00,
RPN_SUB = 0x01,
RPN_MUL = 0x02,
Expand Down Expand Up @@ -62,7 +62,7 @@ enum RPNCommand {
RPN_SYM = 0x81
};

enum SectionType {
enum SectionType : unsigned char {
SECTTYPE_WRAM0,
SECTTYPE_VRAM,
SECTTYPE_ROMX,
Expand All @@ -77,7 +77,7 @@ enum SectionType {
SECTTYPE_INVALID
};

enum FileStackNodeType {
enum FileStackNodeType : unsigned char {
NODE_REPT,
NODE_FILE,
NODE_MACRO,
Expand Down Expand Up @@ -119,13 +119,13 @@ static inline uint32_t nbbanks(SectionType type) {
return sectionTypeInfo[type].lastBank - sectionTypeInfo[type].firstBank + 1;
}

enum SectionModifier { SECTION_NORMAL, SECTION_UNION, SECTION_FRAGMENT };
enum SectionModifier : unsigned char { SECTION_NORMAL, SECTION_UNION, SECTION_FRAGMENT };

extern char const * const sectionModNames[];

enum ExportLevel { SYMTYPE_LOCAL, SYMTYPE_IMPORT, SYMTYPE_EXPORT };
enum ExportLevel : unsigned char { SYMTYPE_LOCAL, SYMTYPE_IMPORT, SYMTYPE_EXPORT };

enum PatchType {
enum PatchType : unsigned char {
PATCHTYPE_BYTE,
PATCHTYPE_WORD,
PATCHTYPE_LONG,
Expand Down
4 changes: 2 additions & 2 deletions src/link/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,10 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
// Since SDCC does not provide line info, everything will be reported as coming from the
// object file. It's better than nothing.
nodes[fileID].push_back({
.type = NODE_FILE,
.data = Either<std::vector<uint32_t>, std::string>(fileName),
.parent = nullptr,
.type = NODE_FILE,
.lineNo = 0,
.parent = nullptr,
});

std::vector<Symbol> &fileSymbols = symbolLists.emplace_front();
Expand Down
1 change: 0 additions & 1 deletion src/link/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

#include "error.hpp"
#include "extern/utf8decoder.hpp"
Expand Down

0 comments on commit 35164f5

Please sign in to comment.