Commit ee31dcdd authored by Lucian Grijincu's avatar Lucian Grijincu Committed by Jordan DeLong

folly: speed up fastpath of ThreadLocal::get()

Summary:
A smaller function makes it more likely it will be inlined
(it wasn't before, is now).

Test Plan: n/a

Reviewed By: tudorb@fb.com

FB internal diff: D759996
parent 05fd9909
......@@ -79,11 +79,12 @@ class ThreadLocal {
T* get() const {
T* ptr = tlp_.get();
if (UNLIKELY(ptr == nullptr)) {
ptr = new T();
tlp_.reset(ptr);
if (LIKELY(ptr != nullptr)) {
return ptr;
}
return ptr;
// separated new item creation out to speed up the fast path.
return makeTlp();
}
T* operator->() const {
......@@ -112,6 +113,12 @@ class ThreadLocal {
ThreadLocal(const ThreadLocal&) = delete;
ThreadLocal& operator=(const ThreadLocal&) = delete;
T* makeTlp() const {
T* ptr = new T();
tlp_.reset(ptr);
return ptr;
}
mutable ThreadLocalPtr<T,Tag> tlp_;
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment