Improve performance by a lot (although quite a dirty patch)#50
Conversation
Author
|
Another benchmark (with https://github.com/treeform/benchy): import benchy
import markdown
let data = "# Hello world"
timeIt "test":
let a = markdown(data)
keep(a)With the patch: Without it: 14x time improvement for a markdown hello-world (from 0.35ms to 0.025ms)! |
Author
|
By the way, I checked and all tests pass just fine. |
ghost
commented
Mar 5, 2021
|
|
||
| from markdownpkg/entities import htmlEntityToUtf8 | ||
|
|
||
| var precompiledExp {.threadvar.}: Table[string, re.Regex] |
Author
There was a problem hiding this comment.
The {.threadvar.} annotation makes this caching thread-safe (each different thread will have to compile regular expressions by itself, but I think that this is fine)
Author
|
Related: #48 |
Owner
|
Awesome. I like the solution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Right now nim-markdown recompiles all regular expressions on EACH call to any method/proc, so that if some proc is called 100 times,
remodule (which uses pcre) has to recompile the same regular expression 100 times - see documentation of https://nim-lang.org/docs/re.html#re%2CstringIn the long run it would be better to actually define variables with precompiled expressions like:
but this patch will work for now.
For a simple MD document (taken from Casa) - https://gist.github.com/Yardanico/3ed52233c29b6328bedce30a9f332c3a
I got a ~8x time improvement - from 27.5ms to 3ms:
This is already much better, but I think that we can make it even faster :)