[webkit-dev] Why are PassRefPtr<>s used as function parameters?

Darin Adler darin at apple.com
Thu Oct 29 13:09:46 PDT 2009

On Oct 29, 2009, at 12:57 PM, Drew Wilson wrote:

> HTMLCollection() keeps a reference to the object, so you can't  
> safely pass in just a raw pointer.

Good point, but strictly speaking that’s not true.

It’s always safe to pass a raw pointer. The PassRefPtr type for the  
argument is solely about optimization, not safety or correctness.

I see what you mean, though. HTMLCollection’s constructor does take a  
PassRefPtr so it would be OK to have HTMLNamedCollection be consistent  
with this. Although there’s no realistic chance the document will be a  
just-created object, so it’s not a great practical optimization.

> Since HTMLCollection keeps it around, you'd have to do the ref anyway.

Unfortunately, the get() inside the HTMLNameCollection’s constructor  
that is needed so we can call nameCollectionInfo with the same  
document pointer means that we do an extra round of reference count  
churn. And there’s no obvious way to rewrite it to eliminate that. So  
the PassRefPtr type on HTMLNameCollection’s constructor’s document  
argument currently does no good and a bit of harm.

     -- Darin

More information about the webkit-dev mailing list