bugdown: Flatten get_web_link_regex().

We use early-exit to flatten the code.

I also tweaked the comments a bit based on some recent
profile findings.  (e.g. reading the file isn't actually
a big bottleneck, it's more the regex itself)
This commit is contained in:
Steve Howell 2019-01-22 18:35:41 +00:00 committed by Tim Abbott
parent 852756aeb3
commit eea711a805
1 changed files with 48 additions and 46 deletions

View File

@ -92,10 +92,12 @@ LINK_REGEX = None # type: Pattern
def get_web_link_regex() -> str: def get_web_link_regex() -> str:
# We create this one time, but not at startup. So the # We create this one time, but not at startup. So the
# first message rendered in any process will have some # first message rendered in any process will have some
# extra costs. # extra costs. It's roughly 75ms to run this code, so
# caching the value in LINK_REGEX is super important here.
global LINK_REGEX global LINK_REGEX
if LINK_REGEX is None: if LINK_REGEX is not None:
# NOTE: this is a very expensive step, it reads a file of tlds! return LINK_REGEX
tlds = '|'.join(list_of_tlds()) tlds = '|'.join(list_of_tlds())
# A link starts at a word boundary, and ends at space, punctuation, or end-of-input. # A link starts at a word boundary, and ends at space, punctuation, or end-of-input.