Use a custom generic tagged union Either instead of std::variant for efficiency (#1476)

* Implement custom generic tagged union `Either`

This should be more efficient than `std::variant`, while still
keeping runtime safety as it `assert`s when `get`ting values.

* Use `Either` for RPN expressions

* Use `Either` for file stack node data

* Use `Either` for `File` buffer

* Use `Either` for `STRFMT` args

* Use `Either` for RGBLINK symbol values

* Support an equivalent of `std::monostate` for `Either`

* Use `Either` for lexer tokens

* Use `Either` for symbol values

* Use `Either` for lexer mmap/buffer state
This commit is contained in:
Sylvie
2024-08-20 15:19:11 -04:00
committed by GitHub
parent 7d98b9a900
commit 57c3d74b9e
17 changed files with 303 additions and 174 deletions

View File

@@ -5,7 +5,6 @@
#include <deque>
#include <inttypes.h>
#include <stdint.h>
#include <variant>
#include <vector>
#include "helpers.hpp" // assume, clz, ctz
@@ -230,8 +229,8 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fil
);
isError = true;
value = 1;
} else if (auto *label = std::get_if<Label>(&symbol->data); label) {
value = label->section->bank;
} else if (symbol->data.holds<Label>()) {
value = symbol->data.get<Label>().section->bank;
} else {
error(
patch.src,
@@ -390,11 +389,11 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fil
fileSymbols[value].name.c_str()
);
isError = true;
} else if (auto *label = std::get_if<Label>(&symbol->data); label) {
value = label->section->org + label->offset;
} else if (symbol->data.holds<Label>()) {
Label const &label = symbol->data.get<Label>();
value = label.section->org + label.offset;
} else {
assume(std::holds_alternative<int32_t>(symbol->data));
value = std::get<int32_t>(symbol->data);
value = symbol->data.get<int32_t>();
}
}
break;