8000 gh-124889: Remove redundant artificial rules in PEG parser by efimov-mikhail · Pull Request #124893 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-124889: Remove redundant artificial rules in PEG parser #124893

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

Merged

Conversation

efimov-mikhail
Copy link
Contributor
@efimov-mikhail efimov-mikhail commented Oct 2, 2024

Cache in C PEG-generator reworked:
we save artificial rules in cache by Node string representation as a key instead of Node object itself. As a result total count of artificial rules in parsers.c is lowered from 283 to 170. More natural number ordering is used for the names of artificial rules.

@ghost
Copy link
ghost commented Oct 2, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link
bedevere-app bot commented Oct 2, 2024

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Cache in C PEG-generator reworked:
we save artificial rules in cache by Node string representation as a key instead of Node object itself.
As a result total count of artificial rules in parsers.c is lowered from 283 to 170.
More natural number ordering is used for the names of artificial rules.
self.cache[node] = self.generate_call(node.alts[0].items[0])
return self.generate_call(node.alts[0].items[0])

node_str = f"{node}"
Copy link
Member

Choose a reason for hiding this comment

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

It would be great if we can abstract this away in a function. Maybe

def generate_artificial_rule(node, prefix, new_rule_fn):
    node_str = f"{node}"
    key = f"{prefix}_{node_str}"
    if key in self.cache:
             name = self.cache[key]
     else:
             name = new_rule_fn()
             self.cache[key] = name
       return name

@pablogsal
Copy link
Member
pablogsal commented Oct 2, 2024

Excellent find @efimov-mikhail 👌

In general LGTM, I just have a small suggestion and we can land it.

I am skipping news since this is not a user visible change.

Copy link
Member
@pablogsal pablogsal left a comment

Choose a reason for hiding this comment

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

LGTM! Great find

Left a small nit. Tell me what you think

…ython#124893)

Auxiliary method CCallMakerVisitor._generate_artificial_rule_call is added.
Its purpose is abstracting work with artificial rules cache.
@efimov-mikhail efimov-mikhail force-pushed the peg_parser_remove_redundant_functions branch from 731ea7f to 0a8f197 Compare October 3, 2024 10:20
@efimov-mikhail
Copy link
Contributor Author

What do you think about this refactoring?

I've moved visit_Rhs method to emphasize similarity of visit_* methods with artificial rules.
Moreover, not only generating artificial rule and saving to cache is moved to aux function,
but creation of FunctionCall object, which creations are also very similar.

Name "_generate_artificial_rule_call" of method seems to to be suitable, since we already have "generate_call" method in this class.

Copy link
Member
@lysnikolaou lysnikolaou left a comment

Choose a reason for hiding this comment

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

Thanks for this @efimov-mikhail! It's a very nice improvement. I've left a couple of more minor comments about the code.

python#124893)

Explicit using of "is_repeat1" kwarg is added to visit_Repeat0 and visit_Repeat1 methods.
Its slightly improve code readabitily.
Copy link
Member
@lysnikolaou lysnikolaou left a comment

Choose a reason for hiding this comment

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

LGTM!

@pablogsal pablogsal merged commit 1f9025a into python:main Oct 3, 2024
43 checks passed
@pablogsal
Copy link
Member

And that's it! Fantastic work @efimov-mikhail! Thanks a lot for the catch and the PR. Looking forward for more contributions 🙂

@efimov-mikhail
Copy link
Contributor Author
efimov-mikhail commented Oct 3, 2024

Great thanks! Collaboration was perfect!
I have some thoughts about future improvements in code generation procedure.
It would be great to make more contributions.

@efimov-mikhail efimov-mikhail deleted the peg_parser_remove_redundant_functions branch October 3, 2024 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0