[pypy-svn] extradoc extradoc: use listings instead of pygmentize

cfbolz commits-noreply at bitbucket.org
Fri Mar 25 23:57:02 CET 2011


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: extradoc
Changeset: r3406:5ac85459c63b
Date: 2011-03-25 22:58 +0100
http://bitbucket.org/pypy/extradoc/changeset/5ac85459c63b/

Log:	use listings instead of pygmentize

diff --git a/talk/icooolps2011/code/trace2.tex b/talk/icooolps2011/code/trace2.tex
--- a/talk/icooolps2011/code/trace2.tex
+++ b/talk/icooolps2011/code/trace2.tex
@@ -1,17 +1,17 @@
-\begin{Verbatim}[commandchars=\\\{\}]
+\begin{lstlisting}[escapechar=|,basicstyle=\ttfamily]]
 # inst.getattr("a")
 map1 = inst.map
 guard(map1 == 0xb74af4a8)
-{\color{gray}index1 = Map.getindex(map1, "a")}
-{\color{gray}guard(index1 != -1)}
+|{\color{gray}index1 = Map.getindex(map1, "a")}|
+|{\color{gray}guard(index1 != -1)}|
 storage1 = inst.storage
 result1 = storage1[index1]
 
 # inst.getattr("b")
-{\color{gray}map2 = inst.map}
-{\color{gray}guard(map2 == 0xb74af4a8)}
-{\color{gray}index2 = Map.getindex(map2, "b")}
-{\color{gray}guard(index2 == -1)}
+|{\color{gray}map2 = inst.map}|
+|{\color{gray}guard(map2 == 0xb74af4a8)}|
+|{\color{gray}index2 = Map.getindex(map2, "b")}|
+|{\color{gray}guard(index2 == -1)}|
 cls1 = inst.cls
 methods1 = cls.methods
 result2 = dict.get(methods1, "b")
@@ -19,10 +19,10 @@
 v2 = result1 + result2
 
 # inst.getattr("c")
-{\color{gray}map3 = inst.map}
-{\color{gray}guard(map3 == 0xb74af4a8)}
-{\color{gray}index3 = Map.getindex(map3, "c")}
-{\color{gray}guard(index3 == -1)}
+|{\color{gray}map3 = inst.map}|
+|{\color{gray}guard(map3 == 0xb74af4a8)}|
+|{\color{gray}index3 = Map.getindex(map3, "c")}|
+|{\color{gray}guard(index3 == -1)}|
 cls1 = inst.cls
 methods2 = cls.methods
 result3 = dict.get(methods2, "c")
@@ -30,4 +30,4 @@
 
 v4 = v2 + result3
 return(v4)
-\end{Verbatim}
+\end{lstlisting}

diff --git a/talk/icooolps2011/code/trace5.tex b/talk/icooolps2011/code/trace5.tex
--- a/talk/icooolps2011/code/trace5.tex
+++ b/talk/icooolps2011/code/trace5.tex
@@ -1,4 +1,4 @@
-\begin{Verbatim}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
 # inst.getattr("a")
 map1 = inst.map
 guard(map1 == 0xb74af4a8)
@@ -15,4 +15,4 @@
 # inst.getattr("c")
 v4 = v2 + 17
 return(v4)
-\end{Verbatim}
+\end{lstlisting}

diff --git a/talk/icooolps2011/code/interpreter-slow.tex b/talk/icooolps2011/code/interpreter-slow.tex
--- a/talk/icooolps2011/code/interpreter-slow.tex
+++ b/talk/icooolps2011/code/interpreter-slow.tex
@@ -1,39 +1,39 @@
-\begin{Verbatim}[commandchars=\\\{\}]
-\PY{k}{class} \PY{n+nc}{Class}\PY{p}{(}\PY{n+nb}{object}\PY{p}{)}\PY{p}{:}
-    \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{name}\PY{p}{)}\PY{p}{:}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{name} \PY{o}{=} \PY{n}{name}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{methods} \PY{o}{=} \PY{p}{\PYZob{}}\PY{p}{\PYZcb{}}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
+class Class(object):
+    def __init__(self, name):
+        self.name = name
+        self.methods = {}
 
-    \PY{k}{def} \PY{n+nf}{instantiate}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{)}\PY{p}{:}
-        \PY{k}{return} \PY{n}{Instance}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{)}
+    def instantiate(self):
+        return Instance(self)
 
-    \PY{k}{def} \PY{n+nf}{find\PYZus{}method}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{name}\PY{p}{)}\PY{p}{:}
-        \PY{n}{result} \PY{o}{=} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{methods}\PY{o}{.}\PY{n}{get}\PY{p}{(}\PY{n}{name}\PY{p}{)}
-        \PY{k}{if} \PY{n}{result} \PY{o+ow}{is} \PY{o+ow}{not} \PY{n+nb+bp}{None}\PY{p}{:}
-            \PY{k}{return} \PY{n}{result}
-        \PY{k}{raise} \PY{n+ne}{AttributeError}\PY{p}{(}\PY{n}{name}\PY{p}{)}
+    def find_method(self, name):
+        result = self.methods.get(name)
+        if result is not None:
+            return result
+        raise AttributeError(name)
 
-    \PY{k}{def} \PY{n+nf}{change\PYZus{}method}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{name}\PY{p}{,} \PY{n}{value}\PY{p}{)}\PY{p}{:}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{methods}\PY{p}{[}\PY{n}{name}\PY{p}{]} \PY{o}{=} \PY{n}{value}
+    def change_method(self, name, value):
+        self.methods[name] = value
 
 
-\PY{k}{class} \PY{n+nc}{Instance}\PY{p}{(}\PY{n+nb}{object}\PY{p}{)}\PY{p}{:}
-    \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{cls}\PY{p}{)}\PY{p}{:}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{cls} \PY{o}{=} \PY{n}{cls}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{attributes} \PY{o}{=} \PY{p}{\PYZob{}}\PY{p}{\PYZcb{}}
+class Instance(object):
+    def __init__(self, cls):
+        self.cls = cls
+        self.attributes = {}
 
-    \PY{k}{def} \PY{n+nf}{getfield}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{name}\PY{p}{)}\PY{p}{:}
-        \PY{n}{result} \PY{o}{=} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{attributes}\PY{o}{.}\PY{n}{get}\PY{p}{(}\PY{n}{name}\PY{p}{)}
-        \PY{k}{if} \PY{n}{result} \PY{o+ow}{is} \PY{o+ow}{not} \PY{n+nb+bp}{None}\PY{p}{:}
-            \PY{k}{return} \PY{n}{result}
-        \PY{k}{raise} \PY{n+ne}{AttributeError}\PY{p}{(}\PY{n}{name}\PY{p}{)}
+    def getfield(self, name):
+        result = self.attributes.get(name)
+        if result is not None:
+            return result
+        raise AttributeError(name)
 
-    \PY{k}{def} \PY{n+nf}{write\PYZus{}attribute}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{name}\PY{p}{,} \PY{n}{value}\PY{p}{)}\PY{p}{:}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{attributes}\PY{p}{[}\PY{n}{name}\PY{p}{]} \PY{o}{=} \PY{n}{value}
+    def write_attribute(self, name, value):
+        self.attributes[name] = value
 
-    \PY{k}{def} \PY{n+nf}{getattr}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{name}\PY{p}{)}\PY{p}{:}
-        \PY{k}{try}\PY{p}{:}
-            \PY{k}{return} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{getfield}\PY{p}{(}\PY{n}{name}\PY{p}{)}
-        \PY{k}{except} \PY{n+ne}{AttributeError}\PY{p}{:}
-            \PY{k}{return} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{cls}\PY{o}{.}\PY{n}{find\PYZus{}method}\PY{p}{(}\PY{n}{name}\PY{p}{)}
-\end{Verbatim}
+    def getattr(self, name):
+        try:
+            return self.getfield(name)
+        except AttributeError:
+            return self.cls.find_method(name)
+\end{lstlisting}

diff --git a/talk/icooolps2011/code/version.tex b/talk/icooolps2011/code/version.tex
--- a/talk/icooolps2011/code/version.tex
+++ b/talk/icooolps2011/code/version.tex
@@ -1,26 +1,26 @@
-\begin{Verbatim}[commandchars=\\\{\}]
-\PY{k}{class} \PY{n+nc}{VersionTag}\PY{p}{(}\PY{n+nb}{object}\PY{p}{)}\PY{p}{:}
-    \PY{k}{pass}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
+class VersionTag(object):
+    pass
 
-\PY{k}{class} \PY{n+nc}{Class}\PY{p}{(}\PY{n+nb}{object}\PY{p}{)}\PY{p}{:}
-    \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{name}\PY{p}{)}\PY{p}{:}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{name} \PY{o}{=} \PY{n}{name}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{methods} \PY{o}{=} \PY{p}{\PYZob{}}\PY{p}{\PYZcb{}}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{version} \PY{o}{=} \PY{n}{VersionTag}\PY{p}{(}\PY{p}{)}
+class Class(object):
+    def __init__(self, name):
+        self.name = name
+        self.methods = {}
+        self.version = VersionTag()
 
-    \PY{k}{def} \PY{n+nf}{find\PYZus{}method}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{name}\PY{p}{)}\PY{p}{:}
-        \PY{n+nb+bp}{self} \PY{o}{=} \PY{n}{hint}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{promote}\PY{o}{=}\PY{n+nb+bp}{True}\PY{p}{)}
-        \PY{n}{version} \PY{o}{=} \PY{n}{hint}\PY{p}{(}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{version}\PY{p}{,} \PY{n}{promote}\PY{o}{=}\PY{n+nb+bp}{True}\PY{p}{)}
-        \PY{n}{result} \PY{o}{=} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{\PYZus{}find\PYZus{}method}\PY{p}{(}\PY{n}{name}\PY{p}{,} \PY{n}{version}\PY{p}{)}
-        \PY{k}{if} \PY{n}{result} \PY{o+ow}{is} \PY{o+ow}{not} \PY{n+nb+bp}{None}\PY{p}{:}
-            \PY{k}{return} \PY{n}{result}
-        \PY{k}{raise} \PY{n+ne}{AttributeError}\PY{p}{(}\PY{n}{name}\PY{p}{)}
+    def find_method(self, name):
+        self = hint(self, promote=True)
+        version = hint(self.version, promote=True)
+        result = self._find_method(name, version)
+        if result is not None:
+            return result
+        raise AttributeError(name)
 
-    \PY{n+nd}{@purefunction}
-    \PY{k}{def} \PY{n+nf}{\PYZus{}find\PYZus{}method}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{name}\PY{p}{,} \PY{n}{version}\PY{p}{)}\PY{p}{:}
-        \PY{k}{return} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{methods}\PY{o}{.}\PY{n}{get}\PY{p}{(}\PY{n}{name}\PY{p}{)}
+    @purefunction
+    def _find_method(self, name, version):
+        return self.methods.get(name)
 
-    \PY{k}{def} \PY{n+nf}{change\PYZus{}method}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{name}\PY{p}{,} \PY{n}{value}\PY{p}{)}\PY{p}{:}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{methods}\PY{p}{[}\PY{n}{name}\PY{p}{]} \PY{o}{=} \PY{n}{value}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{version} \PY{o}{=} \PY{n}{VersionTag}\PY{p}{(}\PY{p}{)}
-\end{Verbatim}
+    def change_method(self, name, value):
+        self.methods[name] = value
+        self.version = VersionTag()
+\end{lstlisting}

diff --git a/talk/icooolps2011/paper.tex b/talk/icooolps2011/paper.tex
--- a/talk/icooolps2011/paper.tex
+++ b/talk/icooolps2011/paper.tex
@@ -8,9 +8,25 @@
 \usepackage{xspace}
 \usepackage[scaled=0.8]{beramono}
 \usepackage[utf8]{inputenc}
+\usepackage{setspace}
+\usepackage{listings}
 
 \input{code/style.tex}
 
+\lstset{
+  basicstyle=\ttfamily\footnotesize,
+  language=Python,
+  keywordstyle=\bfseries,
+  stringstyle=\color{blue},
+  commentstyle=\color{gray}\textit,
+  fancyvrb=true,
+  showstringspaces=false,
+  %keywords={def,while,if,elif,return,class,get,set,new,guard_class}
+  numberstyle = \tiny,
+  numbersep = -20pt,
+}
+
+
 \newboolean{showcomments}
 \setboolean{showcomments}{true}
 \ifthenelse{\boolean{showcomments}}
@@ -296,9 +312,9 @@
 dictionaries.
 Let's assume we trace through code that sums three attributes, such as:
 
-\begin{Verbatim}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
 inst.getattr("a") + inst.getattr("b") + inst.getattr("c")
-\end{Verbatim}
+\end{lstlisting}
 
 \begin{figure}
 \input{code/trace1.tex}
@@ -360,18 +376,19 @@
 However, the optimizer can statically know the value of a variable even if it
 is not a constant in the original source code. For example, consider the
 following fragment of RPython code:
-\begin{Verbatim}[commandchars=\\\{\}]
-\PY{k}{if} \PY{n}{x} \PY{o}{==} \PY{l+m+mi}{4}\PY{p}{:}
-    \PY{n}{y} \PY{o}{=} \PY{n}{y} \PY{o}{+} \PY{n}{x}
-\end{Verbatim}
+
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
+if x == 4:
+    y = y + x
+\end{lstlisting}
 
 If the fragment is traced with \texttt{x} being \texttt{4}, the following trace is
 produced:
 %
-\begin{Verbatim}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
 guard(x == 4)
 y = y + x
-\end{Verbatim}
+\end{lstlisting}
 
 In the trace above, the value of \texttt{x} is statically known thanks to the
 guard. Remember that a guard is a runtime check. The above trace will run to
@@ -392,31 +409,32 @@
 a lot of computation depending on the value of that variable.
 
 Let's make this more concrete. If we trace a call to the following function:
-\begin{Verbatim}[commandchars=\\\{\}]
-\PY{k}{def} \PY{n+nf}{f1}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{n}{y}\PY{p}{)}\PY{p}{:}
-    \PY{n}{z} \PY{o}{=} \PY{n}{x} \PY{o}{*} \PY{l+m+mi}{2} \PY{o}{+} \PY{l+m+mi}{1}
-    \PY{k}{return} \PY{n}{z} \PY{o}{+} \PY{n}{y}
-\end{Verbatim}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
+def f2(x, y):
+    x = hint(x, promote=True)
+    z = x * 2 + 1
+    return z + y
+\end{lstlisting}
 
 We get a trace that looks like this:
 
-\begin{Verbatim}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
 v1 = x * 2
 z = v1 + 1
 v2 = z + y
 return(v2)
-\end{Verbatim}
+\end{lstlisting}
 
 Observe how the first two operations could be constant-folded if the value of
 \texttt{x} were known. Let's assume that the value of \texttt{x} can vary, but does so
 rarely, i.e. only takes a few different values at runtime. If this is the
 case, we can add a hint to promote \texttt{x}, like this:
-\begin{Verbatim}[commandchars=\\\{\}]
-\PY{k}{def} \PY{n+nf}{f2}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{n}{y}\PY{p}{)}\PY{p}{:}
-    \PY{n}{x} \PY{o}{=} \PY{n}{hint}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{n}{promote}\PY{o}{=}\PY{n+nb+bp}{True}\PY{p}{)}
-    \PY{n}{z} \PY{o}{=} \PY{n}{x} \PY{o}{*} \PY{l+m+mi}{2} \PY{o}{+} \PY{l+m+mi}{1}
-    \PY{k}{return} \PY{n}{z} \PY{o}{+} \PY{n}{y}
-\end{Verbatim}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
+def f2(x, y):
+    x = hint(x, promote=True)
+    z = x * 2 + 1
+    return z + y
+\end{lstlisting}
 
 The meaning of this hint is that the tracer should pretend that \texttt{x} is a
 constant
@@ -426,13 +444,13 @@
 the arguments \texttt{4} and \texttt{8}. The trace will be the same, except for one
 operation at the beginning:
 
-\begin{Verbatim}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
 guard(x == 4)
 v1 = x * 2
 z = v1 + 1
 v2 = z + y
 return(v2)
-\end{Verbatim}
+\end{lstlisting}
 
 The promotion is turned into a \texttt{guard} operation in the trace. The guard
 captures the value of \texttt{x} as it was at runtime. From the point of view of the
@@ -441,11 +459,11 @@
 assume that \texttt{x} is equal to \texttt{4}, meaning that the optimizer will turn this
 trace into:
 
-\begin{Verbatim}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
 guard(x == 4)
 v2 = 9 + y
 return(v2)
-\end{Verbatim}
+\end{lstlisting}
 
 Notice how the first two arithmetic operations were constant folded. The hope is
 that the guard is executed quicker than the multiplication and the addition that
@@ -457,11 +475,11 @@
 capture a different value of \texttt{x}. If it is e.g. \texttt{2}, then the optimized
 trace looks like this:
 
-\begin{Verbatim}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
 guard(x == 2)
 v2 = 5 + y
 return(v2)
-\end{Verbatim}
+\end{lstlisting}
 
 This new trace will be attached to the guard instruction of the first trace. If
 \texttt{x} takes on even more values, a new trace will eventually be made for all of them,
@@ -493,29 +511,29 @@
 
 As an example, take the following class:
 
-\begin{Verbatim}[commandchars=\\\{\}]
-\PY{k}{class} \PY{n+nc}{A}\PY{p}{(}\PY{n+nb}{object}\PY{p}{)}\PY{p}{:}
-    \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{x}\PY{p}{,} \PY{n}{y}\PY{p}{)}\PY{p}{:}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{x} \PY{o}{=} \PY{n}{x}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{y} \PY{o}{=} \PY{n}{y}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
+class A(object):
+    def __init__(self, x, y):
+        self.x = x
+        self.y = y
 
-    \PY{k}{def} \PY{n+nf}{f}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{val}\PY{p}{)}\PY{p}{:}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{y} \PY{o}{=} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{compute}\PY{p}{(}\PY{p}{)} \PY{o}{+} \PY{n}{val}
+    def f(self, val):
+        self.y = self.compute() + val
 
-    \PY{k}{def} \PY{n+nf}{compute}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{)}\PY{p}{:}
-        \PY{k}{return} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{x} \PY{o}{*} \PY{l+m+mi}{2} \PY{o}{+} \PY{l+m+mi}{1}
-\end{Verbatim}
+    def compute(self):
+        return self.x * 2 + 1
+\end{lstlisting}
 
 Tracing the call \texttt{a.f(10)} of some instance of \texttt{A} yields the following
 trace (note how the call to \texttt{compute} is inlined):
 %
-\begin{Verbatim}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
 x = a.x
 v1 = x * 2
 v2 = v1 + 1
 v3 = v2 + val
 a.y = v3
-\end{Verbatim}
+\end{lstlisting}
 
 In this case, adding a promote of \texttt{self} in the \texttt{f} method to get rid of the
 computation of the first few operations does not help. Even if \texttt{a} is a
@@ -527,29 +545,29 @@
 is a pure function. To communicate this, there is a \texttt{purefunction} decorator.
 If the code in \texttt{compute} should be constant-folded away, we would change the
 class as follows:
-\begin{Verbatim}[commandchars=\\\{\}]
-\PY{k}{class} \PY{n+nc}{A}\PY{p}{(}\PY{n+nb}{object}\PY{p}{)}\PY{p}{:}
-    \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{x}\PY{p}{,} \PY{n}{y}\PY{p}{)}\PY{p}{:}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{x} \PY{o}{=} \PY{n}{x}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{y} \PY{o}{=} \PY{n}{y}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
+class A(object):
+    def __init__(self, x, y):
+        self.x = x
+        self.y = y
 
-    \PY{k}{def} \PY{n+nf}{f}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{val}\PY{p}{)}\PY{p}{:}
-        \PY{n+nb+bp}{self} \PY{o}{=} \PY{n}{hint}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{promote}\PY{o}{=}\PY{n+nb+bp}{True}\PY{p}{)}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{y} \PY{o}{=} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{compute}\PY{p}{(}\PY{p}{)} \PY{o}{+} \PY{n}{val}
+    def f(self, val):
+        self = hint(self, promote=True)
+        self.y = self.compute() + val
 
-    \PY{n+nd}{@purefunction}
-    \PY{k}{def} \PY{n+nf}{compute}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{)}\PY{p}{:}
-        \PY{k}{return} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{x} \PY{o}{*} \PY{l+m+mi}{2} \PY{o}{+} \PY{l+m+mi}{1}
-\end{Verbatim}
+    @purefunction
+    def compute(self):
+        return self.x * 2 + 1
+\end{lstlisting}
 
 Now the trace will look like this:
 %
-\begin{Verbatim}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
 guard(a == 0xb73984a8)
 v1 = compute(a)
 v2 = v1 + val
 a.y = v2
-\end{Verbatim}
+\end{lstlisting}
 
 Here, \texttt{0xb73984a8} is the address of the instance of \texttt{A} that was used
 during tracing. The call to \texttt{compute} is not inlined, so that the optimizer
@@ -558,11 +576,11 @@
 is a constant reference, the call will be removed by the optimizer. The final
 trace looks like this:
 %
-\begin{Verbatim}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
 guard(a == 0xb73984a8)
 v2 = 9 + val
 a.y = v2
-\end{Verbatim}
+\end{lstlisting}
 
 (assuming that the \texttt{x} field's value is \texttt{4}).
 

diff --git a/talk/icooolps2011/Makefile b/talk/icooolps2011/Makefile
--- a/talk/icooolps2011/Makefile
+++ b/talk/icooolps2011/Makefile
@@ -1,5 +1,5 @@
 
-jit-hints.pdf: paper.tex paper.bib code/interpreter-slow.tex code/map.tex code/version.tex
+jit-hints.pdf: paper.tex paper.bib
 	pdflatex paper
 	bibtex paper
 	pdflatex paper

diff --git a/talk/icooolps2011/code/map.tex b/talk/icooolps2011/code/map.tex
--- a/talk/icooolps2011/code/map.tex
+++ b/talk/icooolps2011/code/map.tex
@@ -1,49 +1,49 @@
-\begin{Verbatim}[commandchars=\\\{\}]
-\PY{k}{class} \PY{n+nc}{Map}\PY{p}{(}\PY{n+nb}{object}\PY{p}{)}\PY{p}{:}
-    \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{)}\PY{p}{:}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{indexes} \PY{o}{=} \PY{p}{\PYZob{}}\PY{p}{\PYZcb{}}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{other\PYZus{}maps} \PY{o}{=} \PY{p}{\PYZob{}}\PY{p}{\PYZcb{}}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
+class Map(object):
+    def __init__(self):
+        self.indexes = {}
+        self.other_maps = {}
 
-    \PY{n+nd}{@purefunction}
-    \PY{k}{def} \PY{n+nf}{getindex}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{name}\PY{p}{)}\PY{p}{:}
-        \PY{k}{return} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{indexes}\PY{o}{.}\PY{n}{get}\PY{p}{(}\PY{n}{name}\PY{p}{,} \PY{o}{-}\PY{l+m+mi}{1}\PY{p}{)}
+    @purefunction
+    def getindex(self, name):
+        return self.indexes.get(name, -1)
 
-    \PY{n+nd}{@purefunction}
-    \PY{k}{def} \PY{n+nf}{add\PYZus{}attribute}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{name}\PY{p}{)}\PY{p}{:}
-        \PY{k}{if} \PY{n}{name} \PY{o+ow}{not} \PY{o+ow}{in} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{other\PYZus{}maps}\PY{p}{:}
-            \PY{n}{newmap} \PY{o}{=} \PY{n}{Map}\PY{p}{(}\PY{p}{)}
-            \PY{n}{newmap}\PY{o}{.}\PY{n}{indexes}\PY{o}{.}\PY{n}{update}\PY{p}{(}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{indexes}\PY{p}{)}
-            \PY{n}{newmap}\PY{o}{.}\PY{n}{indexes}\PY{p}{[}\PY{n}{name}\PY{p}{]} \PY{o}{=} \PY{n+nb}{len}\PY{p}{(}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{indexes}\PY{p}{)}
-            \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{other\PYZus{}maps}\PY{p}{[}\PY{n}{name}\PY{p}{]} \PY{o}{=} \PY{n}{newmap}
-        \PY{k}{return} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{other\PYZus{}maps}\PY{p}{[}\PY{n}{name}\PY{p}{]}
+    @purefunction
+    def add_attribute(self, name):
+        if name not in self.other_maps:
+            newmap = Map()
+            newmap.indexes.update(self.indexes)
+            newmap.indexes[name] = len(self.indexes)
+            self.other_maps[name] = newmap
+        return self.other_maps[name]
 
-\PY{n}{EMPTY\PYZus{}MAP} \PY{o}{=} \PY{n}{Map}\PY{p}{(}\PY{p}{)}
+EMPTY_MAP = Map()
 
-\PY{k}{class} \PY{n+nc}{Instance}\PY{p}{(}\PY{n+nb}{object}\PY{p}{)}\PY{p}{:}
-    \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{cls}\PY{p}{)}\PY{p}{:}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{cls} \PY{o}{=} \PY{n}{cls}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{map} \PY{o}{=} \PY{n}{EMPTY\PYZus{}MAP}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{storage} \PY{o}{=} \PY{p}{[}\PY{p}{]}
+class Instance(object):
+    def __init__(self, cls):
+        self.cls = cls
+        self.map = EMPTY_MAP
+        self.storage = []
 
-    \PY{k}{def} \PY{n+nf}{getfield}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{name}\PY{p}{)}\PY{p}{:}
-        \PY{n+nb}{map} \PY{o}{=} \PY{n}{hint}\PY{p}{(}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{map}\PY{p}{,} \PY{n}{promote}\PY{o}{=}\PY{n+nb+bp}{True}\PY{p}{)}
-        \PY{n}{index} \PY{o}{=} \PY{n+nb}{map}\PY{o}{.}\PY{n}{getindex}\PY{p}{(}\PY{n}{name}\PY{p}{)}
-        \PY{k}{if} \PY{n}{index} \PY{o}{!=} \PY{o}{-}\PY{l+m+mi}{1}\PY{p}{:}
-            \PY{k}{return} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{storage}\PY{p}{[}\PY{n}{index}\PY{p}{]}
-        \PY{k}{raise} \PY{n+ne}{AttributeError}\PY{p}{(}\PY{n}{name}\PY{p}{)}
+    def getfield(self, name):
+        map = hint(self.map, promote=True)
+        index = map.getindex(name)
+        if index != -1:
+            return self.storage[index]
+        raise AttributeError(name)
 
-    \PY{k}{def} \PY{n+nf}{write\PYZus{}attribute}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{name}\PY{p}{,} \PY{n}{value}\PY{p}{)}\PY{p}{:}
-        \PY{n+nb}{map} \PY{o}{=} \PY{n}{hint}\PY{p}{(}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{map}\PY{p}{,} \PY{n}{promote}\PY{o}{=}\PY{n+nb+bp}{True}\PY{p}{)}
-        \PY{n}{index} \PY{o}{=} \PY{n+nb}{map}\PY{o}{.}\PY{n}{getindex}\PY{p}{(}\PY{n}{name}\PY{p}{)}
-        \PY{k}{if} \PY{n}{index} \PY{o}{!=} \PY{o}{-}\PY{l+m+mi}{1}\PY{p}{:}
-            \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{storage}\PY{p}{[}\PY{n}{index}\PY{p}{]} \PY{o}{=} \PY{n}{value}
-            \PY{k}{return}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{map} \PY{o}{=} \PY{n+nb}{map}\PY{o}{.}\PY{n}{add\PYZus{}attribute}\PY{p}{(}\PY{n}{name}\PY{p}{)}
-        \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{storage}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n}{value}\PY{p}{)}
+    def write_attribute(self, name, value):
+        map = hint(self.map, promote=True)
+        index = map.getindex(name)
+        if index != -1:
+            self.storage[index] = value
+            return
+        self.map = map.add_attribute(name)
+        self.storage.append(value)
 
-    \PY{k}{def} \PY{n+nf}{getattr}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{name}\PY{p}{)}\PY{p}{:}
-        \PY{k}{try}\PY{p}{:}
-            \PY{k}{return} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{getfield}\PY{p}{(}\PY{n}{name}\PY{p}{)}
-        \PY{k}{except} \PY{n+ne}{AttributeError}\PY{p}{:}
-            \PY{k}{return} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{cls}\PY{o}{.}\PY{n}{find\PYZus{}method}\PY{p}{(}\PY{n}{name}\PY{p}{)}
-\end{Verbatim}
+    def getattr(self, name):
+        try:
+            return self.getfield(name)
+        except AttributeError:
+            return self.cls.find_method(name)
+\end{lstlisting}

diff --git a/talk/icooolps2011/code/trace4.tex b/talk/icooolps2011/code/trace4.tex
--- a/talk/icooolps2011/code/trace4.tex
+++ b/talk/icooolps2011/code/trace4.tex
@@ -1,37 +1,37 @@
-\begin{Verbatim}[commandchars=\\\{\}]
+\begin{lstlisting}[escapechar=|,mathescape,basicstyle=\ttfamily]
 # inst.getattr("a")
 map1 = inst.map
 guard(map1 == 0xb74af4a8)
-{\color{gray}index1 = Map.getindex(map1, "a")}
-{\color{gray}guard(index1 != -1)}
+|{\color{gray}index1 = Map.getindex(map1, "a")}|
+|{\color{gray}guard(index1 != -1)}|
 storage1 = inst.storage
 result1 = storage1[index1]
 
 # inst.getattr("b")
-{\color{gray}map2 = inst.map}
-{\color{gray}guard(map2 == 0xb74af4a8)}
-{\color{gray}index2 = Map.getindex(map2, "b")}
-{\color{gray}guard(index2 == -1)}
+|{\color{gray}map2 = inst.map}|
+|{\color{gray}guard(map2 == 0xb74af4a8)}|
+|{\color{gray}index2 = Map.getindex(map2, "b")}|
+|{\color{gray}guard(index2 == -1)}|
 cls1 = inst.cls
 guard(cls1 == 0xb7aaaaf8)
 version1 = cls1.version
 guard(version1 == 0xb7bbbb18)
-{\color{gray}result2 = Class._find_method(cls, "b", version1)}
-{\color{gray}guard(result2 is not None)}
+|{\color{gray}result2 = Class.\_find\_method(cls, "b", version1)}|
+|{\color{gray}guard(result2 is not None)}|
 v2 = result1 + result2
 
 # inst.getattr("c")
-{\color{gray}map3 = inst.map}
-{\color{gray}guard(map3 == 0xb74af4a8)}
-{\color{gray}index3 = Map.getindex(map3, "c")}
-{\color{gray}guard(index3 == -1)}
-{\color{gray}cls2 = inst.cls}
-{\color{gray}guard(cls2 == 0xb7aaaaf8)}
-{\color{gray}version2 = cls2.version}
-{\color{gray}guard(version2 == 0xb7bbbb18)}
-{\color{gray}result3 = Class._find_method(cls, "c", version2)}
-{\color{gray}guard(result3 is not None)}
+|{\color{gray}map3 = inst.map}|
+|{\color{gray}guard(map3 == 0xb74af4a8)}|
+|{\color{gray}index3 = Map.getindex(map3, "c")}|
+|{\color{gray}guard(index3 == -1)}|
+|{\color{gray}cls2 = inst.cls}|
+|{\color{gray}guard(cls2 == 0xb7aaaaf8)}|
+|{\color{gray}version2 = cls2.version}|
+|{\color{gray}guard(version2 == 0xb7bbbb18)}|
+|{\color{gray}result3 = Class.\_find\_method(cls, "c", version2)}|
+|{\color{gray}guard(result3 is not None)}|
 
 v4 = v2 + result3
 return(v4)
-\end{Verbatim}
+\end{lstlisting}

diff --git a/talk/icooolps2011/code/trace1.tex b/talk/icooolps2011/code/trace1.tex
--- a/talk/icooolps2011/code/trace1.tex
+++ b/talk/icooolps2011/code/trace1.tex
@@ -1,4 +1,4 @@
-\begin{Verbatim}
+\begin{lstlisting}[mathescape,basicstyle=\ttfamily]
 # inst.getattr("a")
 attributes1 = inst.attributes
 result1 = dict.get(attributes1, "a")
@@ -25,4 +25,4 @@
 
 v4 = v2 + result3
 return(v4)
-\end{Verbatim}
+\end{lstlisting}


More information about the Pypy-commit mailing list