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

Use generator instead of list expand or add method #1225

Merged
merged 3 commits into from
Dec 5, 2022

Conversation

jmishra01
Copy link
Contributor

We can use generator to create pretty string of Tree in place of list. This PR reduce memory usage.

Generator is memory efficient approach.
@erezsh
Copy link
Member

erezsh commented Dec 5, 2022

I like this change!

Review: You can turn a few more of the "yield"s to "yield from"s.

Also improve performance of "iter_subtrees_topdown"
Performance of "iter_subtrees_topdown" method reduce as size of tree increases. Using instance of list method improve the performance.
@jmishra01
Copy link
Contributor Author

@erezsh Done!

@erezsh
Copy link
Member

erezsh commented Dec 5, 2022

What I meant is that you don't need any for loop..

yield from (i for i in ('\t', '%s' % (self.children[0],), '\n'))

can be

yield from ('\t', '%s' % (self.children[0],), '\n')

etc.

Although this particular expression can be rewritten as a single string format.

Give it another try if you like. Otherwise no problem, I'll do the finishing touches myself.

@jmishra01
Copy link
Contributor Author

Thank you for review, I change few lines, use yield with f-string method.

@@ -149,13 +148,15 @@ def iter_subtrees_topdown(self):
Iterates over all the subtrees, return nodes in order like pretty() does.
"""
stack = [self]
stack_append = stack.append
Copy link
Member

Choose a reason for hiding this comment

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

I assume you added this for performance. Did you benchmark it on a realistic use-case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I'm working on one language module and use lark to create grammar.
After changing append and pop method calling approach, performance improve.

Copy link
Member

Choose a reason for hiding this comment

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

Did you try also changing Tree to a local variable (_Tree or such)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not such type of thing, but I create child class of it, and enhance some part for better performance.
Also working on multi-threading, with cythonise some part of code and logic.

Copy link
Member

Choose a reason for hiding this comment

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

I meant specifically inside this function, to skip the global lookup.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, you're aware of https://github.com/lark-parser/lark_cython?

Yes.
I recommend, we should start implement lark in C++ or Rust, so with WASM and Cython (or Pybind11), we can use optimise version of it in Python and JS world.

Copy link
Member

Choose a reason for hiding this comment

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

Having Lark implemented in Rust would be awesome. But I don't have the time to do it myself.

Anyway, there's still a lot of speed to be gained with lark-cython, it's still using a lot of Python objects when it doesn't have to.

Copy link
Contributor Author

@jmishra01 jmishra01 Dec 5, 2022

Choose a reason for hiding this comment

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

I'm interested to take both the tasks, if you allow.

Copy link
Member

Choose a reason for hiding this comment

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

You're very welcome to do so!

Just keep in mind I will only merge code of high quality, i.e. readable, tested, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I will take care of it.

@erezsh erezsh merged commit c3b2996 into lark-parser:master Dec 5, 2022
@erezsh
Copy link
Member

erezsh commented Dec 5, 2022

Thanks!

@jmishra01 jmishra01 deleted the memory-efficient branch December 6, 2022 05:43
@jmishra01 jmishra01 restored the memory-efficient branch December 6, 2022 05:44
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.

2 participants