Hi Victor,

There are plenty of reasons for a new, cleaner C-API.
However, performance isn't really one of them.

The C-API is not much of an obstacle to improving the performance of
CPython, at the moment.

There are implementation details that leak into the API, but they are
only an issue when we want to change those details.
At which point, we should consider focused changes rather than the vague, sweeping changes suggested in PEP 620.


On 21/09/2020 7:35 pm, Victor Stinner wrote:
Hi,

I need to help to attempt to optimize my experimental CPython fork
which uses tagged pointers.

When I proposed my PEP 620 "Hide implementation details from the C
API", I was asked about a proof that the PEP unlocks real optimization
possibilities. So I wrote an implementation of tagged pointers:
https://github.com/vstinner/cpython/pull/6

The main benefit is the memory usage. For example, list(range(200))
uses 1656 bytes instead of 7262 (4x less memory).

Sadly, my current simple implementation is 1.1x slower than the
reference. I suspect that adding a condition to Py_INCREF() and
Py_DECREF() explains a large part of this overhead.

My implementation uses tagged pointers for:

* integers in the range: [-5; 256]
* None, True and False singletons

It would be nice to use tagged pointers for a wide range of integer
numbers, but I wrote a simple implementation: _Py_TAGPTR_UNBOX() has
to return a borrowed reference. This function should return a strong
reference to support a larger range.

More information in the document:
https://github.com/vstinner/cpython/blob/tagged_ptr/TAGGED_POINTERS.rst

A few suggestions:

Make the tagged value something like:
typedef struct {
   intptr_t bits;
} PyTaggedValue;

which will prevent erroneous casts to and from PyObject *.

Add new INCREF/DECREF inline functions that take tagged values.

Never return a borrowed reference (you should should know that ;)

Why are you tagging None, True and False? They don't take up any space.

Abstract out the tagging scheme. You will want to change it.

Cheers,
Mark.
_______________________________________________
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/5L34BUFCP2BJXA6GITLXFJ6CCIIPQZTS/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to