Hybrid implementation for PyLongObject (performance)

The idea is mixing PyLongObject with Python 2’s PyIntObject implementation.

For example, on a 64-bit platform, if (an integer >=-9223372036854775808 and <=9223372036854775807), PyLongObject uses a native C type signed long to represent it.

People mostly use + - * operations, maybe using native int is faster, even including the cost of overflow check.

If operation will overflow or other operations like **(power operator), we can transform native int to current form, and run in current code path.

Regards

Wasn’t this asked on python-ideas?

This was posted to Python-Ideas:
https://mail.python.org/archives/list/python-ideas@python.org/thread/L6BMMETVQYFGDIG7VIFICDSYVUXQAS52/

It didn’t appear due to audit on that day, so I sent to here again.

Currently, PyLongObject uses an array of “digits” which are 30-bit numbers (*). If this array has length 1, then arithmetic can already be done using machine words. The only complication is the sign, since that’s stored separately. But the sign is pretty trivial, that won’t be a performance issue.

So for numbers less than 30 bits long, there is essentially no gain possible. Your proposal would mostly help for numbers between 30 bits and 63 bits long. I doubt that there are many applications with numbers specifically in this range, therefore I don’t think that your proposal is worth it.

(*) I’m assuming a 64-bit platform. Adjust the numbers appropriately for 32-bit.