faster-cpython / ideas

1.67k stars 49 forks source link

Timely deallocation #605

Closed KubaO closed 12 months ago

KubaO commented 1 year ago

Per the Perceus precise reference counting paper:

I haven't checked, but I imagine that Python currently derefs locals when the scope is exited, and not at the point of last reference in the scope.

Is timely deallocation an idea that was raised before - and if not, would that be something worth doing? I presume a proof-of-concept with some reasonable benchmark indicators of improvement would be in order first.

brandtbucher commented 1 year ago

One tricky thing is that people generally expect code like this to work:

>>> def f():
...     spam = 42
...     print(locals()["spam"])
...     print(eval("spam"))
...     breakpoint()
... 
>>> f()
42
42
--Return--
> <stdin>(5)f()->None
(Pdb) spam
42

Implicitly deleting spam too early causes code like this to fail.

serjflint commented 1 year ago

Hello! I'm not an expert, I just want to add some ideas. Can't locals() increase counters to the visible objects and by itself use some hidden from the user mechanism to keep itself until the end of the scope?

gvanrossum commented 12 months ago

This clearly looks like it would affect the behavior of many user programs by changing the order in which finalizers run. I don't think that we can do this without a PEP, and I honestly think such a PEP would not be acceptable, so I think I'll close this issue.

For a new language it would make sense though. Mojo has this baked into the language currently.