Allow choosing between compile-time or runtime generation of large attack tables. #79
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.
I'm trying to use shakmaty in a WebAssembly project that will run in a browser. Unfortunately the static attack tables (particularly
shakmaty::bootstrap::ATTACKS
, which is 700KB on its own) are increasing my binary size by an order of magnitude (~80KB -> ~800KB). To address this, I would like a Cargo feature that controls whether the tables are generated at compile time and embedded in the binary, or are generated at runtime.With this PR, when the
runtime-lut
feature is enabled,ATTACKS
andRAYS
are boxed (Box::<[u64, _]>
), and use thelazy_static
macro to defer initialisation to the first use at runtime.The code is not really the prettiest, but I believe the
maybe_const_fn
macro is the only way to avoid writing out the table generation functions twice, once for array and once for Box. It would have been possible to simply callBox::new(init_magics())
, which semantically works, but causes the whole LUT to be pushed onto the stack while it's generated, which triggered a stack overflow for me building against thewasm32-unknown-unknown
target.Possible concerns:
lazy_static
when feature is enabled.ATTACKS
andRAYS
variables now have different types depending on a feature flag. It would be possible to make them both&'static [u64; _]
(usingBox::leak
for the box version), but since they are internal-only and Box derefs to an array anyway this didn't seem necessary.KNIGHT_ATTACKS
etc.) since they are much smaller. If the consistency is desirable though this approach could be extended to all static tables.