I'm surprised nobody has mentioned this:  there are no "unboxed" types
in CPython - in effect, every object user code creates is allocated
from the heap. Even, e.g., integers and floats. So even non-contrived
code can create garbage at a ferocious rate. For example, think about
this simple function, which naively computes the Euclidean distance
between two n-vectors:

```python
    def dist(xs, ys):
        from math import sqrt
        if len(xs) != len(ys):
            raise ValueError("inputs must have same length")
        return sqrt(sum((x - y)**2 for x, y in zip(xs, ys)))
```

In general, `len(xs)` and `len(ys)` each create a new integer object,
which both become trash the instant `!=` completes.  Say the common
length is `n`.

`zip` then creates `n` 2-tuple objects, each of which lives only long
enough to be unpacked into `x` and `y`.  The the result of `x-y` lives
only long enough to be squared, and the result of that lives only long
enough to be added into `sum()`'s internal accumulator.  Finally, the
grand total lives only long enough to extract its square root.

With "immediate" reclamation of garbage via refcounting, memory use is
trival regardless of how large `n` is, as CPython reuses the same heap
space over & over & over, ASAP.  The space for each 2-tuple is
reclaimed before `x-y` is computed, the space for that is reclaimed
when the square completes, and the space for the square is reclaimed
right after `sum()` folds it in.  It's memory-efficient and
cache-friendly "in the small".

Of course that's _assuming__, e.g., that `(x-y).__pow__(2)` doesn't
save away its arguments somewhere that outlives the method call, but
the compiler has no way to know whether it does.  The only thing it
can assume about the element types is that they support the methods
the code invokes.  It fact, the compiler has no idea whether the
result type of `x-y` is even the same as the type of `x` or of `y`.
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/NU4T5TFPDVYDCR5ADY6KKJ6USWVFD3TZ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to