[Python-Dev] Caching of builtins and globals in action

Samuele Pedroni pedronis@bluewin.ch
Fri, 13 Jun 2003 17:25:37 +0200


At 15:23 12.06.2003 +0200, Samuele Pedroni wrote:
>I have played a bit with the notion of caching builtin and global values 
>(to be precise the address of their dictionary entry) without changing 
>language semantics:
>
>I have an experimental modification to the interpreteter such that I get 
>these numbers: [bl_ is the baseline CVS code, ex_ is the modified interpreter]:

while obtaining a speedup when globals&built-ins are involved, as I said 
the modification preserve the current semantics, here are transcripts 
showing its working

Python 2.3b1+ (#100, Jun 12 2003, 22:25:31)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> def g():
...   y = 1
...   print y
...
 >>> g()
1
 >>> # g doesn't need a cache
...
 >>> def f():
...  print x
...  print x
...
 >>> x=2
 >>> f()
alloc cache
cache in code
caching
2
using cache
2
release my cache ref
 >>> # the cache is allocated on demand, stored away in code, and populated 
and used
...
 >>> f()
fast load cache
using cache
2
using cache
2
release my cache ref
 >>> # cache is retrieved from code and put to use
...
 >>> del x
 >>> x=3
 >>> f()
alloc cache
cache in code
caching
3
using cache
3
release my cache ref
 >>> # major change, cache discarded and rebuilt
...
 >>> def h():
...  print range
...
 >>> h()
alloc cache
cache in code
caching
<built-in function range>
release my cache ref
 >>> h()
fast load cache
using cache
<built-in function range>
release my cache ref
 >>> range = "range"
 >>> h()
alloc cache
cache in code
caching
range
release my cache ref
 >>> del range
 >>> h()
alloc cache
cache in code
caching
<built-in function range>
release my cache ref
 >>> # shadowing/unshadowing of built-ins work

Python 2.3b1+ (#102, Jun 12 2003, 22:35:51)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> c = compile("def f(): print x\nf()","<>","exec")
 >>> exec c in {'x': 1}
alloc cache
cache in code
caching
1
release my cache ref
 >>> exec c in {'x': 2}
alloc cache
cache in code
caching
2
release my cache ref