Commit 51ce1df3 authored by Paul McCarthy's avatar Paul McCarthy
Browse files

Turns out that pyparsing is *really* slow. So I've memoized the parse

function.
parent 8b17840c
......@@ -51,11 +51,14 @@ about the program.
from __future__ import print_function
import sys
import logging
import pyparsing as pp
import fsl.utils.memoize as memoize
log = logging.getLogger(__name__)
......@@ -160,6 +163,7 @@ def getDeclarations(code):
return decs
@memoize.memoizeMD5
def parseGLSL(source):
"""Parses the given GLSL source, and returns:
- The attribute declarations.
......
#!/usr/bin/env python
#
# memoize.py - Memoization decorators.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
"""This module provides a handful of decorators which may be used to memoize
a function:
.. autosummary::
:nosignatures:
memoizeMD5
"""
import hashlib
def memoizeMD5(func):
"""Memoize the given function. Whenever the function is called, an
md5 digest of its arguments is calculated - if the digest has been
previously cached, the previous value calculated by the function is
returned.
"""
cache = {}
def wrapper(*args, **kwargs):
args = list(args) + list(kwargs.values())
hashobj = hashlib.md5()
for arg in args:
hashobj.update(str(arg))
digest = hashobj.hexdigest()
cached = cache.get(digest)
if cached is not None:
return cached
result = func(*args, **kwargs)
cache[digest] = result
return result
return wrapper
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment