Skip to content

Commit bf3e733

Browse files
committed
Apply memoization to getPhases; this provides a decent perf gain
1 parent 911cf45 commit bf3e733

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

html5lib/html5parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ def parseRCDataRawtext(self, token, contentType):
418418
self.phase = self.phases["text"]
419419

420420

421+
@utils.memoize
421422
def getPhases(debug):
422423
def log(function):
423424
"""Logger that records which phase processes each token"""

html5lib/utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,15 @@ def moduleFactory(baseModule, *args, **kwargs):
109109
return mod
110110

111111
return moduleFactory
112+
113+
114+
def memoize(func):
115+
cache = {}
116+
117+
def wrapped(*args, **kwargs):
118+
key = (tuple(args), tuple(kwargs.items()))
119+
if key not in cache:
120+
cache[key] = func(*args, **kwargs)
121+
return cache[key]
122+
123+
return wrapped

0 commit comments

Comments
 (0)