Add base64 fallback encoding for resource embedding - #33
Merged
Conversation
When #embed is unavailable (pre-C23/C++26 compilers), the '\xNN' character literal fallback expands generated sources to ~6x the resource size, making large resources very slow to compile. - Add CMRC_BASE64 option: encode resources as base64 (~1.33x expansion), decoded once at static-initialization time; #embed still takes precedence. - Add cmrc::detail::b64_decode() in cmrc.hpp, guarded by CMRC_CMRC_HPP_BASE64 so it is compiled only in resource TUs that use it. - Split encoded strings into chunked literals to stay under MSVC per-literal and post-concatenation caps (C2026) on older toolsets. - Add CMRC_DISABLE_EMBED diagnostic option to force the fallback path on #embed-capable compilers for testing the generators. - Add flower_b64 test that builds the fallback path end-to-end and verifies decoded content matches the original file byte-for-byte. - Document the #embed / literal / base64 tradeoffs in the README.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
1.
CMRC_BASE64flag (CMakeRC.cmake)option(CMRC_BASE64 ... OFF), forwarded via-Dto the generation script. When ON, resources are encoded as standard base64 in the generated TU — but only as the fallback;#embedstill takes precedence on compilers that support it.file(READ ... HEX)usingmath(EXPR "0x...")+ a lookup table. It processes the hex in slices of 9000 bytes to avoid CMake's O(n²) variable-expansion trap (verified: the naive per-3-byte loop took >5 min for 2.6 MB; the sliced version runs in <1 s).b64_lens/b64_partspair, so arbitrarily large resources work on pre-2022 MSVC (per-literal ~16 K / 64 K concatenation caps). MSVC compliance was the reason a single big string literal is impossible — chunking sidesteps it entirely.CMRC_DISABLE_EMBED(diagnostic/testing, OFF by default) forces the fallback path so the generators can be exercised on#embed-capable compilers.2. Conditional decoder (
include/cmrc/cmrc.hpp)detail::b64_decode(chunks, lens, count)decodes the chunked string; guarded by#define CMRC_CMRC_HPP_BASE64, which the generated TU only defines when the fallback is actually taken. On a#embed-capable compiler the header isn't even included — zero dead code when base64 isn't used. The generated TU stores the decodedstd::stringin a namespace-scope static so the begin/endconst char* constsymbol pattern is unchanged.3. README
#embedauto-detection, then the literal vs base64 fallback, with explicit small-vs-large file tradeoffs (compiled from the measured numbers below).4. Tests
tests/flower_b64/— a nested consumer project that buildsflower.jpgtwice (literal + base64), runs both executables (byte-for-byte content check), measures first-resolve time, and asserts the generated-TU size comparison.tests/CMakeLists.txtregisters it asflower_b64.Verified measurements (clang 21,
flower.jpg2,595,120 bytes)All 13 tests pass (clean reconfigure + build). Existing behavior is unchanged with default flags (the default intermediate output is byte-identical to before). One caveat worth noting: the
flower_b64test takes ~1 min because compiling the 36 MB literal TU is itself the pain point the base64 flag solves.