EXPERIMENT: expression templates #15
Open
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.
This is a proof-of-concept for expression templates. If we go this route, it'd be a major redesign but should not require any changes to user code (API reverse compatibility).
a & bwill return a typeBBExpr<...>that implements lazy evaluation.(a & b).popcnt()ora = b & ~ccan be done without allocating anyBitSettemporary objects. Furthermore, an optimizing compiler will be able to inline the operations and holistically optimize the whole statement. Debug builds will be slow because inlining is turned off.BitSettoBBExprandBBExprMutable. This will allow these methods to be used on expressions, e.g.,(a & b).popcnt(). New containers, such as statically-sized bit vectors that live on the stack, can simply inherit fromBBExprand implementbbexpr_num_blocks()andbbexpr_get_block()to be blessed with all these methods.BBExprcan be iterated over:for (int i : bbvec)orfor (int i : (a & ~b)). Consequently, BBScan can be deprecated. Possibly BBObject can be deprecated as well.src/bitscan/tests/test_bbexpr.cpp. It's notable that the fastest version is (IMHO) also the most readable.auto. Example:auto a = b & c; b.clear();would be a problem becauseawould hold a reference tobandcbutBitSet a = b & c; b.clear();would be okay. (I'm assumingBitSethaving a hypotheticalclear()method that releases memory.) All C++ linear algebra libraries have the same problem because they all use expression templates.