JavaScript: account for the fact that our JavaScript bindings use a 32-bit address space#1367
JavaScript: account for the fact that our JavaScript bindings use a 32-bit address space#1367agarny wants to merge 3 commits intocellml:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Updates libCellML’s cached variable-equivalence logic to avoid cache-key collisions in 32-bit address spaces (notably the JavaScript/WASM bindings), addressing incorrect cache hits on very large models (Fixes #1366).
Changes:
- Replaces the Cantor-pairing-based cache key with a
(uintptr_t, uintptr_t)key in anunordered_mapforAnalyserModel::areEquivalentVariables(). - Switches generator initialisation logic to use the
AnalyserModelcachedareEquivalentVariables()API. - Adds (currently disabled/commented-out) large-model regression tests across C++, Python, and JavaScript bindings.
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
src/analysermodel.cpp |
Uses a pointer-pair key for cached equivalence results to avoid 32-bit overflow collisions. |
src/analysermodel_p.h |
Introduces VariableKeyPair + custom hash and moves cache storage to unordered_map. |
src/generator.cpp |
Routes equivalence checks through mAnalyserModel->areEquivalentVariables() (cached). |
tests/generator/generator.cpp |
Adds a large-model regression test, but it is commented out. |
tests/bindings/python/test_generator.py |
Adds a large-model regression test, but it is disabled via a triple-quoted block. |
tests/bindings/javascript/generator.test.js |
Adds a large-model regression test, but it is commented out; also introduces unused imports. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Note that the test is disabled since it performs repeated parse/analyse/generate loops, which take several minutes to complete. So, it should be enabled only when needed, and not as part of our regular test suite.
... rather than the generic areEquivalentVariables() method which doesn't include caching.
Indeed, in our WebAssembly module, the key was 32-bit (while 64-bit in C++/Python) which caused collisions and incorrect cache hits. So, we replaced the Cantor-pairing key and std::map-based cache with a typed VariableKeyPair and std::unordered_map. This makes the cached-equivalence lookup more explicit, portable, and efficient.
|
|
||
| bool operator==(const VariableKeyPair &other) const | ||
| { | ||
| #ifdef CODE_COVERAGE_ENABLED |
There was a problem hiding this comment.
is this a new thing that we haven't done before? or doing something we've done before in a different way?
There was a problem hiding this comment.
CODE_COVERAGE_ENABLED is indeed a new thing. It is needed for our code coverage test (in case the name didn't give it away! :)). We have a line that normally reads:
return first == other.first && second == other.second;second == other.second is some kind of guard, but if I recall correctly code coverage tells us that it is is never false and that's because if first == other.first is false then second == other.second doesn't get evaluated. So, I use CODE_COVERAGE_ENABLED for the case the code is use during code coverage and, in this case, rather than executing:
return first == other.first && second == other.second;we execute:
const auto firstEqual = static_cast<uintptr_t>(first == other.first);
const auto secondEqual = static_cast<uintptr_t>(second == other.second);
return firstEqual * secondEqual != 0;which is the same (albeit less efficient) and has 100% coverage.
nickerso
left a comment
There was a problem hiding this comment.
I think this looks ok, just trying to understand the introduction and use of the new code coverage define.
Fixes #1366.