[Python-Dev] finalization again

Guido van Rossum guido@python.org
Fri, 10 Mar 2000 08:46:43 -0500


> What if you timestamp instances when you create them?  Then when you
> have trash cycles with finalizers, you sort them and finalize in
> chronological order.  The nice thing here is that the user can have
> complete control over finalization order by controlling object
> creation order.
> 
> Some random thoughts:
> 
> - Finalization order of cyclic finalizable trash is completely
>   deterministic.
> 
> - Given sufficient resolution of your system clock, you should never
>   have two objects with the same timestamp.

Forget the clock -- just use a counter that is incremented on each
allocation.

> - You could reduce the memory footprint by only including a timestamp
>   for objects whose classes have __del__'s at instance creation time.
>   Sticking an __del__ into your class dynamically would have no effect
>   on objects that are already created (and I wouldn't poke you with a
>   pointy stick if even post-twiddle instances didn't get
>   timestamped).  Thus, such objects would never be finalized -- tough
>   luck.
> 
> - FIFO order /seems/ more natural to me than FILO, but then I rarely
>   create cyclic objects, and almost never use __del__, so this whole
>   argument has been somewhat academic to me :).

Ai, there's the rub.

Suppose I have a tree with parent and child links.  And suppose I have
a rule that children need to be finalized before their parents (maybe
they represent a Unix directory tree, where you must rm the files
before you can rmdir the directory).  This suggests that we should
choose LIFO: you must create the parents first (you have to create a
directory before you can create files in it).  However, now we add
operations to move nodes around in the tree.  Suddenly you can have a
child that is older than its parent! Conclusion: the creation time is
useless; the application logic and actual link relationships are
needed.

> - The rule seems easy enough to implement, describe, and understand.
> 
> I think I came up with a few more points on the drive home, but my
> post jam, post lightbulb endorphodrenalin rush is quickly subsiding,
> so I leave the rest until tomorrow.
> 
> its-simply-a-matter-of-time-ly y'rs,
> -Barry

Time flies like an arrow -- fruit flies like a banana.

--Guido van Rossum (home page: http://www.python.org/~guido/)