Add a fast path to folly::ThreadLocal
authorDave Watson <davejwatson@fb.com>
Wed, 22 Nov 2017 16:16:04 +0000 (08:16 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 22 Nov 2017 16:25:44 +0000 (08:25 -0800)
commitdc4be288a0cd5b88beb4f8c1b5d74d98791d43c9
tree9fccb0a4e293d373d5cb024f86a54ef6143d9710
parent8bfce3ed35f7597ce59278a845e9be6d75609a41
Add a fast path to folly::ThreadLocal

Summary:
Currently folly::ThreadLocal[Ptr] is pretty heavy-weight for a get():

1) call instance(), take a static init guard, branch
2) call getThreadEntry, check if thread_local is not null, branch
3) check if id < threadEntry->capacity, branch
4) Finally, return threadEntry->elements[id]

If we have real thread_locals, we can do better by caching the capacity directly,
combining all three checks:

1) checkif id < threadLocalCapacityCheck, branch.  If not, do slow path.
2) return threadEntry->elements[id].  Threadentry is never null if capacity > 0, and
    instance() setup work is called during the first getThreadEntry call when threadlocalcapacity == 0.

Reviewed By: yfeldblum

Differential Revision: D6379878

fbshipit-source-id: 4fc7564bbb2f319d65875124026aef28d910ef06
folly/ThreadLocal.h
folly/detail/ThreadLocalDetail.h
folly/test/ThreadLocalBenchmark.cpp