Skip to content

Commit 176651a

Browse files
committed
Merge pull request #248 from gsnedders/phase_memo
Apply memoization to getPhases; this provides a decent perf gain; r=nobody!
2 parents 911cf45 + bf3e733 commit 176651a

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

html5lib/html5parser.py

+1
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

+12
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)