-
-
Notifications
You must be signed in to change notification settings - Fork 602
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
Improve the performance for parsing props, classes and style #341
Comments
I compared the speed of MacBook M1
Jetson Nano
This is about a 6x performance improvement. For a simpler string like "disabled" the improvement is almost 10x. |
I compared the current style parsing Lines 96 to 98 in 95364b7
Lines 211 to 213 in 95364b7
implementation with a trivial one def parse_trivial(text: str) -> dict:
result = {}
for word in text.split(';'):
word = word.strip()
if word:
key, value = word.split(':', 1)
result[key.strip()] = value.strip()
return result and one with a compiled regex: STYLE_PATTERN = re.compile(r'\s*(.*?)\s*:\s*(.*?)\s*(?:;|$)')
def parse_regex(text: str) -> dict:
return dict(re.findall(STYLE_PATTERN, text)) But the "trivial" implementation is fastest on a MacBook M1
and on a Jetson Nano:
This is for 10,000 iterations with the string "color: red; background-color: green ; width:12em;height:34.5em; transform: translate(120.0px, 50%); box-shadow: 0 0 0.5em #1976d2". But results are similar for a simple "color: red". I hoped to save some time with the much shorter regex. But apparently we should replace the current implementation with |
Ok, I replaced both functions with faster implementations. For classes we don't need to parse anything, but simply split a string into words. Branch parsing-performance is ready for review and merge. |
Reviewed and merged. Wonderful! |
While profiling the UI updates in a production environment we noticed that the
.props()
method can be a bottleneck in certain situations (see #338). Currently we use theshlex
module. But this might be overpowered.In #338 (comment) I presented an alternative solution:
Output:
We should compare both implementations and switch to the regex solution if it's faster. Maybe we can also find regular expressions for parsing classes and style definitions.
The text was updated successfully, but these errors were encountered: