-
Notifications
You must be signed in to change notification settings - Fork 122
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
Performance improvement #284
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Just checking this "High Performance" port, I noticed a few things:
inlineTags
( https://github.com/syrusakbary/pyjade/blob/master/pyjade/compiler.py#L18 )selfClosing
( https://github.com/syrusakbary/pyjade/blob/master/pyjade/compiler.py#L40 )autocloseCode
( https://github.com/syrusakbary/pyjade/blob/master/pyjade/compiler.py#L51 )should be sets and not lists.
We seem to only do lookups. This costs O(n) and a set would make it O(1).
Replacing
[...]
by{...}
would make the change. I haven't checked, but if they're constants, then, we should replace[...]
byfrozenset([...])
instead.As
autocloseCode
can mutate, it should be aset
and the register method would become:In https://github.com/syrusakbary/pyjade/blob/master/pyjade/compiler.py#L199
I believe
textOnly = tag.textOnly or not bool(len(tag.block.nodes))
could be replaced bytextOnly = tag.textOnly or not tag.block.nodes
. The truthy value of a container is alreadyFalse
if it's empty,True
otherwise. But maybe I've missed something.About format_path ( https://github.com/syrusakbary/pyjade/blob/master/pyjade/compiler.py#L273 ):
Checking if a character is in a string (especially one that is likely to be at the end of the string) may be slightly costly. Well, it's not that costly, but for extension, we have a function made for that job:
os.path.splitext
.I've created the following snippet. I encourage you to run it on your own system.
It's a 25-30% performance gain.
In https://github.com/syrusakbary/pyjade/blob/master/pyjade/compiler.py#L296
TYPE_CODE
is a constant and should be declared outside of the function (to avoid recreating it for every function call).Another place about set/list:
https://github.com/syrusakbary/pyjade/blob/master/pyjade/compiler.py#L307
What does it mean?
set
will always be faster than alist
to lookup. Especially if there are non-matching candidates.set
always costs the same.set
is more costly to build than alist
.set
becomes cheaper again.Here again, we can expect up to 17% performance improvement (if there are many misses).
That's pretty much it, in terms of what pops to the eyes, in
compiler.py
.Keep up the good work!
The text was updated successfully, but these errors were encountered: