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

Darin Adler darin at apple.com
Thu Oct 29 12:56:32 PDT 2009


On Oct 27, 2009, at 10:55 AM, Jens Alfke wrote:

> Looking at how refcounting is implemented in WebCore, I was  
> surprised to find that there are a lot of functions/methods that  
> take PassRefPtr<>s as parameters instead of regular pointers to  
> those objects. I can't see any benefit to this, and it adds the  
> overhead of a ref() and deref() at every call-site.

Have you read the RefPtr document? It is at <http://webkit.org/coding/RefPtr.html 
 >.

Only functions that take ownership of the passed-in objects should  
take PassRefPtr. This makes it so we can optimally handle the case  
where that object was just created and lets us hand off the reference  
instead of having to do a round of reference count thrash. If the  
function is going to be storing the value in a RefPtr, then the  
PassRefPtr does not introduce any reference count thrash even if the  
passed in object was not just created.

It’s wrong for the HTMLNameCollection constructor to take a PassRefPtr  
for its document argument because it doesn’t take ownership of the  
document. So that one is definitely a mistake.

It is true that PassRefPtr arguments can be tricky as you mentioned  
and the RefPtr document also mentions.

     -- Darin



More information about the webkit-dev mailing list