[webkit-dev] Should we ever use std::function instead of WTF::Function?

Maciej Stachowiak mjs at apple.com
Tue Jun 13 10:08:02 PDT 2017


In case it turns out not to be possible to reduce the number of concepts (besides eliminating std::function), maybe it would help to change the names and behaviors of these classes to match better. Function, SharedFunction and ScopedFunction would have a much more obvious relationship to each other than Function, SharedTask and ScopedLambda. 

(I'm not sure if the direct assignment from a lambda is an incidental difference or one that's required by the different ownership semantics.)

 - Maciej

> On Jun 13, 2017, at 9:34 AM, Filip Pizlo <fpizlo at apple.com> wrote:
> 
> We should have a better story here.  Right now the story is too complicated.  We have:
> 
> - ScopedLambda or ScopedLambdaRef if you have a stack-allocated function that outlives its user
> - SharedTask if you have a heap-allocated function that you want to share and ref-count
> - WTF::Function if you have a heap-allocated function that you want to transfer ownership (move yes, copy no)
> - std::function if you have a heap-alloated function that you want to pass by copy
> 
> Only std::function and WTF::Function do the magic that lets you say:
> 
> std::function f = <lambda>
> 
> Also, std::function has the benefit that it does copying.  None of the others do that.
> 
> ScopedLambda serves a specific purpose: it avoids allocation.  Probably we want to keep that one even if we merge the others.
> 
> IMO SharedTask has the nicest semantics.  I don’t ever want the activation of the function to be copied.  In my experience I always want sharing if more than one reference to the function exists.  I think that what we really want in most places is a WTF::Function that has sharing semantics like SharedTask.  That would let us get rid of std::function and SharedTask.
> 
> In the current status quo, it’s not always correct to convert std::function to the others because:
> 
> - Unlike ScopedLambda and SharedTask, std::function has the magic constructor that allows you to just assign a lambda to it.
> - Unlike ScopedLambda, std::function is safe if the use is not scoped.
> - Unlike WTF::Function, std::function can be copied.
> 
> -Filip
> 
> 
>> On Jun 13, 2017, at 9:24 AM, Darin Adler <darin at apple.com> wrote:
>> 
>> I’ve noticed many patches switching us from std::function to WTF::Function recently, to fix problems with copying and thread safety.
>> 
>> Does std::function have any advantages over WTF::Function? Should we ever prefer std::function, or should we use WTF::Function everywhere in WebKit where we would otherwise use std::function?
>> 
>> — Darin
>> _______________________________________________
>> webkit-dev mailing list
>> webkit-dev at lists.webkit.org
>> https://lists.webkit.org/mailman/listinfo/webkit-dev
> 
> _______________________________________________
> webkit-dev mailing list
> webkit-dev at lists.webkit.org
> https://lists.webkit.org/mailman/listinfo/webkit-dev



More information about the webkit-dev mailing list