• Aaryaman Sagar's avatar
    InlineFunctionRef for inline FunctionRef storage · a703ff28
    Aaryaman Sagar authored
    Summary:
    InlineFunctionRef is a semantically the same as folly::FunctionRef but has the
    additional benefit of being able to store the function it was instantiated
    with inline in a buffer of the given capacity.  If there is not enough in-situ
    capacity for the callable, this has the same semantics as FunctionRef.
    
    This helps give a perf boost in the case where the data gets separated from
    the point of invocation.  If, for example, at the point of invocation, the
    InlineFunctionRef object is not cached, a remote memory/cache read might be
    required to invoke the original callable.  Customizable inline storage helps
    tune storage so we can store a type-erased callable with better performance
    and locality.  A real-life example of this might be a folly::FunctionRef with
    a function pointer.  The folly::FunctionRef would point to the function
    pointer object in a remote location.  This causes a double-indirection at the
    point of invocation, and if that memory is dirty, or not cached, it would
    cause additional cache misses.  On the other hand with InlineFunctionRef,
    inline storage would store the value of the function pointer, avoiding the
    need to do a remote lookup to fetch the value of the function pointer.
    
    To prevent misuse, InlineFunctionRef disallows construction from an lvalue
    callable.  This is to prevent usage where a user relies on the callable's
    state after invocation through InlineFunctionRef.  This has the potential to
    copy the callable into inline storage when the callable is small, so we might
    not use the same function when invoking, but rather a copy of it.
    
    Also note that InlineFunctionRef will always invoke the const qualified
    version of the call operator for any callable that is passed.  Regardless of
    whether it has a non-const version.  This is done to enforce the logical
    constraint of function state being immutable.
    
    This class is always trivially-copyable (and therefore
    trivially-destructible), making it suitable for use in a union without
    requiring manual destruction.
    
    Reviewed By: yfeldblum, ot
    
    Differential Revision: D14029799
    
    fbshipit-source-id: 2cff3ce27d564f3d524095189f847c14911f9402
    a703ff28
FunctionRefTest.cpp 6.53 KB