icyleaf / markd

Yet another markdown parser, Compliant to CommonMark specification, written in Crystal.
MIT License
109 stars 31 forks source link

Store often-used regex in constant to improve performance #8

Closed straight-shoota closed 7 years ago

straight-shoota commented 7 years ago

Fixup for performance hit introduced by #5 as reported by @asterite

The regexes used by Decoder.decode and Encoder.encode are now cached as constants.

asterite commented 7 years ago

There are also many other Regex.new that should be cached. One in decode_entities_string, yet another one in Encoder.encode. Caching those improves performance again (just caching the first one reduces the time from 0.97s to 0.7s in one benchmark I have). But I don't have time to send a PR for this.

straight-shoota commented 7 years ago

Yes. I've already been working on a few improvements. There is still a lot to do :)

asterite commented 7 years ago

Another one: the HUGE Regex for entities can be simplified to this:

Regex.new("&(?:(\\w+;?)|(#[xX][\\da-fA-F]+;?|#\\d+;?))")

Then you need to check in both legacy and entity mappings. But it's still faster than processing that super big regex.

icyleaf commented 7 years ago

I also have no much time on crystal. My baby was born recently, only limit time to coding 🤣

asterite commented 7 years ago

@icyleaf Congratulations! Please spend less time with Crystal and more with your baby :-)