New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't need to copy __builtins__ into data #159

Closed
sqlalchemy-bot opened this Issue Feb 10, 2011 · 2 comments

Comments

Projects
None yet
1 participant
@sqlalchemy-bot

sqlalchemy-bot commented Feb 10, 2011

Migrated issue, originally created by Michael Bayer (@zzzeek)

someone pointed this out on the list. The copy we do from builtins means templates without a lot of variables are going to be disproportionately slow. We can shift to a "check dict then builtins" approach, possibly using some newer Python dictionary features (i.e. missing):

diff -r 6779081b32cfb237062da8bc623a27d47301ce4c mako/runtime.py
--- a/mako/runtime.py	Wed Jan 19 10:33:00 2011 -0500
+++ b/mako/runtime.py	Thu Feb 10 17:20:58 2011 -0500
@@ -22,12 +22,7 @@
     def __init__(self, buffer, **data):
         self._buffer_stack = [buffer]
  
-        # original data, minus the builtins
-        self._orig = data
- 
-        # the context data which includes builtins
-        self._data = __builtin__.__dict__.copy()
-        self._data.update(data)
+        self._data = data
         self._kwargs = data.copy()
         self._with_template = None
         self._outputting_as_unicode = None
@@ -75,7 +70,10 @@
         return self._data.keys()
  
     def __getitem__(self, key):
-        return self._data[key]
+        if key in self._data:
+            return self._data[key]
+        else:
+            return __builtins__[key]
 
     def _push_writer(self):
         """push a capturing buffer onto this Context and return
@@ -107,7 +105,9 @@
     def get(self, key, default=None):
         """Return a value from this :class:`.Context`."""
  
-        return self._data.get(key, default)
+        return self._data.get(key, 
+                __builtins__.get(key, default)
+                )
  
     def write(self, string):
         """Write a string to this :class:`.Context` object's
@@ -124,7 +124,6 @@
         c = Context.__new__(Context)
         c._buffer_stack = self._buffer_stack
         c._data = self._data.copy()
-        c._orig = self._orig
         c._kwargs = self._kwargs
         c._with_template = self._with_template
         c._outputting_as_unicode = self._outputting_as_unicode
@@ -499,7 +498,7 @@
     (callable_, ctx) = _populate_self_namespace(
                                 context._clean_inheritance_tokens(), 
                                 template)
-    callable_(ctx, **_kwargs_for_include(callable_, context._orig, **kwargs))
+    callable_(ctx, **_kwargs_for_include(callable_, context._data, **kwargs))
  
 def _inherit_from(context, uri, calling_uri):
     """called by the _inherit method in template modules to set
diff -r 6779081b32cfb237062da8bc623a27d47301ce4c setup.cfg
--- a/setup.cfg	Wed Jan 19 10:33:00 2011 -0500
+++ b/setup.cfg	Thu Feb 10 17:20:58 2011 -0500
@@ -1,2 +1,5 @@
 [egg_info]
 tag_build = dev
+
+[nosetests]
+where = test
@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Feb 21, 2011

Michael Bayer (@zzzeek) wrote:

this is 6c06a32 and it does in fact enhance speed a good deal.

@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Feb 21, 2011

Changes by Michael Bayer (@zzzeek):

  • changed status to closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment