kennygrant / sanitize

Package sanitize provides functions for sanitizing text in golang strings.
BSD 3-Clause "New" or "Revised" License
334 stars 73 forks source link

improving regex speed 2x faster #9

Closed eduncan911 closed 9 years ago

eduncan911 commented 9 years ago

Instantiating regex is expensive. Should always keep it global for speed. This PR improves performance over 2x times.

Original speed:

BenchmarkPath              10000        106790 ns/op
BenchmarkName              20000         62479 ns/op
BenchmarkHTMLAllowed        2000       1149116 ns/op

Improvements in this pull request:

BenchmarkPath              30000         51607 ns/op
BenchmarkName             100000         23429 ns/op
BenchmarkHTMLAllowed        3000        501696 ns/op

Benchmarked on Go1.4.1

eduncan911 commented 9 years ago

since u have merged the other one (for docs), I can resubmit this PR with the conflicts resolved if u like?

eduncan911 commented 9 years ago

there ya go. pulled in the doc updates and resolved the conflicts.

ready to merge

kennygrant commented 9 years ago

Other pull request was merged because it is trivial, I'd like some time to think about this change.

eduncan911 commented 9 years ago

no problem. all i did was move them out of local scope and into the package scope. i also added benchmark tests for the affected methods.

all existing tests passed, indicating that no logic was changed or broken. i also verified there was a test for each method affected by this change.

MustCompile is an extra safety measure ensuring the regex is valid before the app runs.

kennygrant commented 9 years ago

Thanks for this. I've merged your changes with minor tweaks - I prefer to keep the pkg globals inline above the functions which use them, so that it's clear where they are used, and I wanted the global names a bit clearer.