Skip to content

Improve caching of equivalent variables in analyser model.#1389

Open
hsorby wants to merge 9 commits into
cellml:mainfrom
hsorby:cache-equiv-vars
Open

Improve caching of equivalent variables in analyser model.#1389
hsorby wants to merge 9 commits into
cellml:mainfrom
hsorby:cache-equiv-vars

Conversation

@hsorby
Copy link
Copy Markdown
Contributor

@hsorby hsorby commented Apr 27, 2026

@hsorby hsorby requested review from agarny and nickerso April 27, 2026 22:57
Copy link
Copy Markdown
Contributor

@agarny agarny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before reviewing further, I am going to wait for PR #1390 (for issue #1379) to be reviewed and merged in since it affects the caching code.

Comment thread src/api/libcellml/analysermodel.h Outdated
Copy link
Copy Markdown
Contributor

@agarny agarny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good and definitely faster! Nice!

AnalyserModel::AnalyserModelImpl::buildEquivalentVariablesCache() should probably be called in AnalyserModel::areEquivalentVariables() (if the cache hasn't already been built), but having said that we know that the analyser is going to call AnalyserModel::areEquivalentVariables() many times, so I guess it's fine to call AnalyserModel::AnalyserModelImpl::buildEquivalentVariablesCache() upon retrieving the analyser model?

Comment thread src/analyser.cpp Outdated
Comment thread src/analysermodel.cpp
Comment thread src/analysermodel.cpp
Comment thread src/analysermodel.cpp
Comment thread src/analysermodel_p.h
Comment thread src/analysermodel.cpp Outdated
Comment thread src/analysermodel_p.h
Comment thread src/analysermodel_p.h Outdated
Comment thread src/analysermodel_p.h Outdated
Comment thread src/analysermodel.cpp Outdated
Comment thread src/analysermodel_p.h Outdated
Comment thread src/analysermodel_p.h Outdated
@hsorby hsorby requested a review from agarny May 21, 2026 06:57
Copy link
Copy Markdown
Contributor

@agarny agarny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks. "Just" some nitpicking.

* because an @ref AnalyserModel always refers to a static version of a
* @ref Model. However, this might break if a @ref Model is modified after it
* has been analysed.
* The function utilizes caching which is constructed during the model
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"utilises", not "utilizes".

* has been analysed.
* The function utilizes caching which is constructed during the model
* analysis phase (@ref Analyser::analyseModel). The cache may become
* out-of-date if the model is changed after the model has been analysed.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"out of date", not "out-of-date" since it is not followed by a noun.

Comment thread src/analysermodel_p.h
return (first == other.first) & (second == other.second);
if (it == mEquivalentVariableCache.end()) {
mEquivalentVariableCache[x] = x;
return x;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put an empty line before thereturn statement (to make things stand out more).

Comment thread src/analysermodel.cpp
}
}

void AnalyserModel::AnalyserModelImpl::buildEquivalentVariablesCache(const ComponentPtr &component)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put this method before the previous one (at line 51), to respect the order in which they are declared in src/analysermodel_p.h.

Comment thread src/analysermodel_p.h
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to put some actual code in a header file? I am talking about findRootAddress() and uniteEquivalentAddresses().

Comment thread src/analysermodel_p.h
struct VariableKeyPair
std::unordered_map<uintptr_t, uintptr_t> mEquivalentVariableCache;

uintptr_t findRootAddress(uintptr_t x)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would rename this method to findVariableAddress(). I am not sure what is meant by "root" here.

Comment thread src/analysermodel_p.h
}

hash ^= pair.second + 0x9e3779b9 + (hash << 6) + (hash >> 2);
void uniteEquivalentAddresses(uintptr_t x, uintptr_t y)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename this method to uniteEquivalentVariableAddresses() to be consistent with line 51.

Comment thread .actrc
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine that this file (as well as act-end-2-end-input.txt and act-payload.json) were used for profiling? If so, then they should be removed. If not, then they should somehow be modified so that they are neither specific to your environment nor to a specific version of libCellML.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants