Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify if statement with container #3047

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

JCGoran
Copy link
Contributor

@JCGoran JCGoran commented Aug 14, 2024

Trivial simplification in solve.cpp. Note that I'm fairly sure this also fixes a subtle bug: the strcmp was comparing cvode_v, which should've actually been called cvode_t_v.

Copy link

✔️ a039b04 -> Azure artifacts URL

Copy link

codecov bot commented Aug 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 67.26%. Comparing base (09d0490) to head (53fb177).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3047      +/-   ##
==========================================
- Coverage   67.28%   67.26%   -0.02%     
==========================================
  Files         571      571              
  Lines      104913   104877      -36     
==========================================
- Hits        70588    70546      -42     
- Misses      34325    34331       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bbpbuildbot

This comment has been minimized.

@JCGoran JCGoran marked this pull request as ready for review August 15, 2024 06:13
method = (Symbol*) 0;
lq->element.sym = (Symbol*) 0;
cvodemethod_ = 3;
const std::unordered_map<std::string, int> methods = {{"after_cvode", 1},
Copy link
Member

@ferdonline ferdonline Aug 15, 2024

Choose a reason for hiding this comment

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

With such a very low number of items maybe underered_map is bit overkill?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, though I will admit that now the code is IMHO less readable since the unordered_map API is much nicer than messing with pair in a vector.

Copy link
Collaborator

Choose a reason for hiding this comment

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

std::map is much more readable! See comment below.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps it's an aside, but in my experience using containers will increase the code significantly compared to current string compairisons. On every function invocation, the container will likely be rebuilt.

For instance, in this godbolt, I copied the vector impl: https://godbolt.org/z/1naM8oW1o
Compared to a string compare, it's nearly 6 times longer, ASM wise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess my question in that case is: is this part of the code performance-critical? If so, I'm fine with leaving the current implementation (still would like to fix that typo though).

As a(nother) side note: the unordered_map version is only ~2x longer than the current one (only w/GCC, w/clang the difference is more like 5-6x, though still shorter than vector or map): https://godbolt.org/z/4a8Peo3Wb

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe make it static const to avoid reconstructing the map? I don't think that ASM size really matters here - but I would also move the map very close to the enum definition to lessen the chance of divergence.

- fix a bug: `cvode_v` should have been `cvode_t_v`
- use an internal `enum class` to avoid the above happening again
@JCGoran JCGoran changed the title Simplify if statement with unordered_map Simplify if statement with container Aug 15, 2024
Copy link

✔️ 5f91500 -> Azure artifacts URL

@bbpbuildbot

This comment has been minimized.

Comment on lines 136 to 146
const std::vector<std::pair<std::string, CVodeMethod>> cvode_methods = {
{"after_cvode", CVodeMethod::after_cvode},
{"cvode_t", CVodeMethod::cvode_t},
{"cvode_t_v", CVodeMethod::cvode_t_v}};

const auto& method_match =
std::find_if(cvode_methods.begin(), cvode_methods.end(), [&method](const auto& item) {
return method && method->name == item.first;
});
if (method && method_match != cvode_methods.end()) {
cvodemethod_ = method_match->second;
Copy link
Collaborator

@matz-e matz-e Aug 15, 2024

Choose a reason for hiding this comment

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

So... this is basically like a map lookup?

Suggested change
const std::vector<std::pair<std::string, CVodeMethod>> cvode_methods = {
{"after_cvode", CVodeMethod::after_cvode},
{"cvode_t", CVodeMethod::cvode_t},
{"cvode_t_v", CVodeMethod::cvode_t_v}};
const auto& method_match =
std::find_if(cvode_methods.begin(), cvode_methods.end(), [&method](const auto& item) {
return method && method->name == item.first;
});
if (method && method_match != cvode_methods.end()) {
cvodemethod_ = method_match->second;
const std::map<std::string, CVodeMethod> cvode_methods = {
{"after_cvode", CVodeMethod::after_cvode},
{"cvode_t", CVodeMethod::cvode_t},
{"cvode_t_v", CVodeMethod::cvode_t_v}};
decltype(cvode_methods)::const_iterator m;
if (method && (m = cvode_methods.find(method)) != cvode_methods.end()) {
cvodemethod_ = m->second;

Copy link

sonarcloud bot commented Aug 15, 2024

Copy link

✔️ 53fb177 -> Azure artifacts URL

Comment on lines +139 to +141
cvodemethod_ = CVodeMethod::nomethod;
if (method && cvode_method_map.count(method->name)) {
cvodemethod_ = cvode_method_map.at(method->name);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
cvodemethod_ = CVodeMethod::nomethod;
if (method && cvode_method_map.count(method->name)) {
cvodemethod_ = cvode_method_map.at(method->name);
cvodemethod_ = deduce_cvode_method(method);
if (cvodemethod_ != CVodeMethod::none) {

You can implement deduce_cvode_method in two stages, first convert method to a char*. Then convert the char* to enum, returning CVodeMethod::none if the string isn't one of the supported names for the cvode methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants