Last active
December 7, 2019 16:28
-
-
Save bkietz/4f4d612e15224e6f3359323324da16bf to your computer and use it in GitHub Desktop.
Lazily initialized data member: c++11, thread safe, const correct.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
class Int { | |
public: | |
explicit Int(int i) : i_(i) { | |
static_assert(sizeof(lazy_) == sizeof(void*), | |
"lazy members take up only one pointer's worth of space"); | |
} | |
// naive string constructing accessor | |
std::string Str() const { return std::to_string(i_); } | |
// lazily accessor | |
const std::string& LazyStr() const { return *lazy_; } | |
private: | |
int i_; | |
struct L : lazy<std::string, L> { | |
std::string get() const { return Member(&Int::lazy_).Str(); } | |
} lazy_; | |
}; | |
constexpr int kIntValue = 841123325; | |
static void Baseline(benchmark::State& state) { | |
const Int i(kIntValue); | |
for (auto _ : state) { | |
auto created_string = i.Str(); | |
benchmark::DoNotOptimize(created_string); | |
} | |
} | |
BENCHMARK(Baseline); | |
static void Lazy(benchmark::State& state) { | |
const Int i(kIntValue); | |
for (auto _ : state) { | |
const auto& lazy_string_ref = i.LazyStr(); | |
benchmark::DoNotOptimize(lazy_string_ref); | |
} | |
} | |
BENCHMARK(Lazy); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <atomic> | |
#include <cassert> | |
#include <cstdint> | |
template <typename Constructed, typename This> | |
class lazy { | |
public: | |
const Constructed& operator*() const { return *get_lazy(); } | |
const Constructed* operator->() const { return get_lazy(); } | |
protected: | |
lazy() : ptr_(nullptr) {} | |
template <typename Of> | |
const Of& Member(This Of::*member) const { | |
auto offset = reinterpret_cast<intptr_t>(&(reinterpret_cast<Of*>(0)->*member)); | |
return *reinterpret_cast<const Of*>(reinterpret_cast<intptr_t>(cast()) - offset); | |
} | |
private: | |
const Constructed* get_lazy() const { | |
auto ptr = ptr_.load(); | |
if (ptr != nullptr) return ptr; | |
ptr = new Constructed(cast()->get()); | |
Constructed* expected = nullptr; | |
if (ptr_.compare_exchange_strong(expected, ptr)) { | |
return ptr; | |
} | |
delete ptr; | |
assert(expected != nullptr); | |
return expected; | |
} | |
const This* cast() const { return static_cast<const This*>(this); } | |
mutable std::atomic<Constructed*> ptr_; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Boost Software License - Version 1.0 - August 17th, 2003 | |
Permission is hereby granted, free of charge, to any person or organization | |
obtaining a copy of the software and accompanying documentation covered by | |
this license (the "Software") to use, reproduce, display, distribute, | |
execute, and transmit the Software, and to prepare derivative works of the | |
Software, and to permit third-parties to whom the Software is furnished to | |
do so, all subject to the following: | |
The copyright notices in the Software and this entire statement, including | |
the above license grant, this restriction and the following disclaimer, | |
must be included in all copies of the Software, in whole or in part, and | |
all derivative works of the Software, unless such copies or derivative | |
works are solely in the form of machine-executable object code generated by | |
a source language processor. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT | |
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | |
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
DEALINGS IN THE SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
43 times faster according to QuickBench:
http://quick-bench.com/vEwWVw59IZE6JPYN1yPMoj5mfJk