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

Refactor parser #346

Merged
merged 7 commits into from
Sep 29, 2023
Merged

Refactor parser #346

merged 7 commits into from
Sep 29, 2023

Conversation

Adda0
Copy link
Collaborator

@Adda0 Adda0 commented Sep 26, 2023

This PR improves the performance of parser. More optimizations are to come, as well as a possible rewrite of the formula parser.

@Adda0 Adda0 requested review from tfiedor and jurajsic September 26, 2023 07:32
@codecov
Copy link

codecov bot commented Sep 26, 2023

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (069554c) 72.94% compared to head (ef48e39) 72.95%.
Report is 1 commits behind head on devel.

Additional details and impacted files
@@           Coverage Diff           @@
##            devel     #346   +/-   ##
=======================================
  Coverage   72.94%   72.95%           
=======================================
  Files          33       33           
  Lines        4144     4148    +4     
  Branches      955      955           
=======================================
+ Hits         3023     3026    +3     
- Misses        740      741    +1     
  Partials      381      381           
Files Coverage Δ
include/mata/parser/inter-aut.hh 97.29% <100.00%> (ø)
src/parser.cc 81.61% <100.00%> (ø)
src/inter-aut.cc 68.36% <86.48%> (+0.07%) ⬆️

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

Comment on lines 231 to +232
for (int j = static_cast<int>(opstack.size())-1; j >= 0; --j) {
size_t j_size_t{ static_cast<size_t>(j) };
assert(!opstack[j_size_t].is_operand());
if (opstack[j_size_t].is_leftpar())
auto formula_node_opstack_it{ opstack.begin() + j };
Copy link
Member

@jurajsic jurajsic Sep 26, 2023

Choose a reason for hiding this comment

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

Isn't this just iterating from end to begin? So we could have
for (auto formula_node_opstack_it = opstack.rbegin(); formula_node_opstack_it != opstack.rend(); ++formula_node_opstack_it)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think you are right. It should be possible to refactor this as you say. I will investigate.

Copy link
Collaborator Author

@Adda0 Adda0 Sep 27, 2023

Choose a reason for hiding this comment

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

The int arithmetic is there so you can do erase() in the vector. It will not work with a reverse iterator. I would keep it as it is, or rework the logic to not have to erase inside the loop.

Copy link
Member

Choose a reason for hiding this comment

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

I think it could be done somehow with erase(), because it returns iterator, but a forward one.
But moving erasing outside the loop would probably be the best.

FormulaNode node{};
std::vector<FormulaGraph> children{};

FormulaGraph() = default;
FormulaGraph(const FormulaNode& n) : node(n), children() {}
FormulaGraph(FormulaNode&& n) : node(std::move(n)), children() {}
explicit FormulaGraph(const FormulaNode& n) : node(n), children() { children.reserve(2); }
Copy link
Collaborator

Choose a reason for hiding this comment

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

just wandering, can you put the two alredy to the the line 129 with the declaration?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, vector has no such constructor, unfortunately. It can be done with a lambda or a named function inside the parentheses in the initialization list. Something like

FormulaGraph() : children([](){ std::vector<FormulaNode> children; children.reserve(100); return children;}()) {}

if (token.second) { // is quoted?
result.push_back(std::move(token));
continue;
}

const std::string_view token_string = token.first;
size_t last_operator = 0;
const std::string_view token_string{ token.first };
Copy link
Collaborator

Choose a reason for hiding this comment

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

why this everywhere actually?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I mena why to initialize things with the curly brackets {bla} instead of = bla?

Copy link
Member

Choose a reason for hiding this comment

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

I think it is some C++ idiom, that is advised to use. I myself don't like it.

Copy link
Member

Choose a reason for hiding this comment

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

It is to prevent narrowing of the types (https://stackoverflow.com/a/27755143), but naturally, C++ community has to make sure it is ugly as pug's arse.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Exactly as you say. It is a good idea, only horribly implemented as far as visualization goes. But it helps indeed and warns you about potential implicit conversion bugs. In modern C++, it should always be preferred over assignment operator.

@kilohsakul
Copy link
Collaborator

anyway, great!

if (token.second) { // is quoted?
result.push_back(std::move(token));
continue;
}

const std::string_view token_string = token.first;
size_t last_operator = 0;
const std::string_view token_string{ token.first };
Copy link
Collaborator

Choose a reason for hiding this comment

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

I mena why to initialize things with the curly brackets {bla} instead of = bla?

@kilohsakul
Copy link
Collaborator

Weird thing, if you look at the checks - the performance tests, then you can see that complement got faster.
It should not, right?
Also the comparison of times for operations do not seem to correspond to differences in runtimes. Maybe it would deserve little investigation eventually (not within this pull request).

include/mata/parser/inter-aut.hh Show resolved Hide resolved
if (token.second) { // is quoted?
result.push_back(std::move(token));
continue;
}

const std::string_view token_string = token.first;
size_t last_operator = 0;
const std::string_view token_string{ token.first };
Copy link
Member

Choose a reason for hiding this comment

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

I think it is some C++ idiom, that is advised to use. I myself don't like it.

if (token.second) { // is quoted?
result.push_back(std::move(token));
continue;
}

const std::string_view token_string = token.first;
size_t last_operator = 0;
const std::string_view token_string{ token.first };
Copy link
Member

Choose a reason for hiding this comment

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

It is to prevent narrowing of the types (https://stackoverflow.com/a/27755143), but naturally, C++ community has to make sure it is ugly as pug's arse.

@tfiedor
Copy link
Member

tfiedor commented Sep 29, 2023

The checks are passing, we decided to move with this PR. Please @Adda0 check the rest of the conversations post mortem. Oaw oaw, thanks.

@tfiedor tfiedor merged commit bb85433 into devel Sep 29, 2023
20 checks passed
@tfiedor tfiedor deleted the refactor_parser branch September 29, 2023 19:04
@Adda0
Copy link
Collaborator Author

Adda0 commented Oct 2, 2023

Weird thing, if you look at the checks - the performance tests, then you can see that complement got faster.
It should not, right?
Also the comparison of times for operations do not seem to correspond to differences in runtimes. Maybe it would deserve little investigation eventually (not within this pull request).

Complement got slower, not faster. I think this is only a deviation which stems from us running the performance tests only once. The runtimes are not precise, and it runs faster one time and slower another time. Nothing except for parsing and mintermization can change in this PR.

@Adda0
Copy link
Collaborator Author

Adda0 commented Oct 2, 2023

The changes from the discussions were addressed and will be part of a future parser optimization PR.

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.

4 participants