interpreter vs. compiled

Paul Boddie paul at boddie.org.uk
Sat Aug 2 15:31:16 EDT 2008


On 2 Aug, 08:33, castironpi <castiro... at gmail.com> wrote:
> On Aug 1, 5:24 am, Paul Boddie <p... at boddie.org.uk> wrote:
>
> >   a + b # in Python
> >
> > ...is not sufficiently represented by...
> >
> >   ldr r1, a
> >   ldr r2, b
> >   add r3, r1, r2
> >
> > ...in some assembly language (and the resulting machine code), mostly
> > because the semantics of Python addition are more complicated.
>
> No, it is not sufficiently represented.  Python runs checks before and
> after, to check for overflows.

It does more than this...

>    test safeinteger a
>    test safeinteger b
>    ldr r1, a
>    ldr r2, b
>    add r3, r1, r2
>    test not overflow
>
> However, no implementation of Python can do better, given Python's
> specification.

In fact, as was probably mentioned before, it does something more like
this:

      get method __add__ on a (if possible) or jump to (1)
      populate an invocation frame with a and b
      call the method
      test the result against the special NotImplemented value
      if result is not NotImplemented then jump to (3)
  (1) get method __radd__ on b (if possible) or jump to (2)
      populate an invocation frame with b and a
      call the method
      test the result against the special NotImplemented value
      if result is not NotImplemented then jump to (3)
  (2) raise a TypeError exception
  (3) provide the result to whatever gets evaluated next

The instructions that you list, which is based on the really simple
case which I mentioned, happens in the method that gets called (eg.
__add__), and then only for integers. Note that seemingly trivial
things like getting the methods can be quite a few instructions in any
low-level code.

[...]

> Another factor, a and b are known to be and are always integers, in a
> given C context.
>
>   int a, b;
>   ...
>   a + b
>
> The C compilation process outputs:
>
>    ldr r1, a
>    ldr r2, b
>    add r3, r1, r2

Right. That's why some people want to have type declarations in
Python, and others want to be able to generate specialised code at run-
time for such cases.

> and you are correct.  However, for:
>
>   string a, b;
>   a + b
>
> performs a concatenation which is not that simple.  The point is, C
> compilation runs, and you actually have -ldr, ldr, add- lying around
> in a file somewhere, which can run as three consecutive instructions
> on a processor.  It's already in the interpreter in Python, and you
> have the -test, test, ldr, ldr, add, test- sequence somewhere in
> Python.exe, specifically wherever the object code for ceval.c is
> going.

Right. The way to think about this is that due to the mechanics of
working out what kind of operations should be performed (should it be
an integer addition, a string concatentation, something else?),
there's a lot of code executed which is really "dancing around" the
actual work, and then for short bursts of instructions, the work
actually gets done. It's like having to jet around the world, sampling
drinks in different locations, rather than just lining the different
drinks up at home.

Paul



More information about the Python-list mailing list