[Bioperl-l] Bio::Root::RootI::DESTROY -> Bio::Root::Root::_cleanup_methods

Tim Bunce Tim.Bunce@pobox.com
Mon, 30 Sep 2002 17:31:13 +0100


On Mon, Sep 30, 2002 at 04:34:15PM +0100, Ewan Birney wrote:
> On Mon, 30 Sep 2002, Tim Bunce wrote:
> 
> > A couple of quick questions after another skim through the Bio::Root code...
> >
> > Are cleanup methods used? If so, what for (typically)?
> 
> usually IO, Jason knows more.

If the usage doesn't need the object to exist any more, then it would
be cheaper to use a mechanism that just stores blessed refs in the object.
Then when the object is destroyed perl will reduce the ref counts on all
the data items stored in the object. Any that are blessed refs will trigger
a DESTROY.

Here's a module the demonstrates the principle:

  http://search.cpan.org/author/TILLY/ReleaseAction/ReleaseAction.pm
  http://search.cpan.org/src/TILLY/ReleaseAction/ReleaseAction.pm

I'd suggest something like a add_post_destroy_hook method:

	$obj->add_post_destroy_hook( sub { ... } )
or maybe
	$obj->add_post_destroy_hook( my_hook_name => sub { ... } )

where if the ref supplied isn't blessed then it's wrapped and
blessed in the manner of ReleaseAction.pm above.

(A delete_post_destroy_hook could be given just the name of the hook.)

Using this approach you'll get ~5% speedup in the next_seq test
because you'll no longer need a DESTROY method. It'll all be done
at full C-speed inside perl.

The code ref supplied can be a closure that 'contains' the data it
needs to do the clean up - such as the path to the temp file to delete.
(Take care not to refer to the object or you'll get a ref loop and
a memory 'leak'.)

> > Why do some classes, like Bio::Seq, define an empty DESTROY method
> > (and thereby prevent super class DESTROY or cleanup methods being called)?
> 
> Probably Idiocy.

:-/

Tim.