5 ms·
Does anyone know why Python refcounts everything -- even small integers, True, False, None...? Why not avoid it?
by throwaway613834 9y ago
Does anyone know why Python refcounts everything -- even small integers, True, False, None...? Why not avoid it?
- chrisseaton 9y agoI don't know the internals of Python, but maybe checking if you need to refcount something is basically takes as long as actually just going ahead and doing it all the time anyway.
- throwaway613834 9y agoYeah you might be right, it's hard to say. Generally I view writing as more expensive than reading so I don't know.
- gvx 9y agoNote that it's not just write vs read, it's write vs read + branch.
- throwaway613834 9y agoRight, if it was just 1 read vs. 1 write then I would know!
- pythonaut_16 9y agoThat's also the same thought behind the Python practice of "it's better to ask forgiveness than permission". If you're going to have a conditional that will evaluate to True most of the time, sometimes (in Python) it's considered better to just go ahead and try the logic that the conditional is guarding, and then catch the exception and handle it if it happens.
- jepler 9y agoSure: CPython doesn't know statically that anything is "not an object", so your proposal adds an extra test to every incref and every decref. This is perceived as probably worse for performance. In 2002, a hacked CPython interpreter (source code long since lost, sorry!) saw a 5% performance decrease on pystone but a 14% performance increase on an integer-heavy workload. https://mail.python.org/pipermail/python-dev/2002-August/027685.html https://mail.python.org/pipermail/python-dev/2002-August/027...
- throwaway613834 9y agoInteresting, thanks for the link! Generally I've viewed writing as more expensive than reading, so I'd be curious if that behavior is still true on modern hardware.
- Waterluvian 9y agoI'll probably eat my own words, but generally I believe CPython is meant to be easy to reason about, easy to read the code for and understand the implementation, and not to try to be meaningfully performant. I would bet the decision to refcount everything is to keep it simple.
- throwaway613834 9y agoPossibly, though it seems they do try to optimize things (they have an entire peephole optimizer).