Commit 06bb6a93 authored by Robert Bradshaw's avatar Robert Bradshaw

Major speedups to @cython.compile by caching parsing information.

parent b51af22b
......@@ -7,6 +7,19 @@ from distutils.extension import Extension
from Cython import Utils
from Cython.Compiler.Main import Context, CompilationOptions, default_options
def cached_function(f):
cache_name = '__%s_cache' % f.__name__
def wrapper(*args):
cache = getattr(f, cache_name, None)
if cache is None:
cache = {}
setattr(f, cache_name, cache)
if args in cache:
return cache[args]
res = cache[args] = f(*args)
return res
return wrapper
def cached_method(f):
cache_name = '__%s_cache' % f.__name__
def wrapper(self, *args):
......
......@@ -14,7 +14,7 @@ from Cython.Compiler.Main import Context, CompilationOptions, default_options
from Cython.Compiler.ParseTreeTransforms import CythonTransform, SkipDeclarations, AnalyseDeclarationsTransform
from Cython.Compiler.TreeFragment import parse_from_strings
from Cython.Build.Dependencies import strip_string_literals, cythonize
from Cython.Build.Dependencies import strip_string_literals, cythonize, cached_function
from Cython.Compiler import Pipeline
import cython as cython_module
......@@ -38,6 +38,7 @@ class AllSymbols(CythonTransform, SkipDeclarations):
def visit_NameNode(self, node):
self.names.add(node.name)
@cached_function
def unbound_symbols(code, context=None):
code = to_unicode(code)
if context is None:
......
......@@ -65,9 +65,10 @@ class Context(object):
import Builtin, CythonScope
self.modules = {"__builtin__" : Builtin.builtin_scope}
cyscope = CythonScope.create_cython_scope(
self, create_testscope=create_testscope)
self.modules["cython"] = self.cython_scope = cyscope
if self.cython_scope is None:
Context.cython_scope = CythonScope.create_cython_scope(
self, create_testscope=create_testscope)
self.modules["cython"] = self.cython_scope
self.include_directories = include_directories
self.future_directives = set()
self.compiler_directives = compiler_directives
......
Markdown is supported
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