Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

threadlocal: avoiding a dynamic cast in opt builds #11900

Merged
merged 2 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/envoy/runtime/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ envoy_cc_library(
external_deps = ["abseil_optional"],
deps = [
"//include/envoy/stats:stats_interface",
"//include/envoy/thread_local:thread_local_interface",
"//source/common/common:assert_lib",
"//source/common/singleton:threadsafe_singleton",
"@envoy_api//envoy/type/v3:pkg_cc_proto",
Expand Down
3 changes: 2 additions & 1 deletion include/envoy/runtime/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "envoy/common/pure.h"
#include "envoy/stats/store.h"
#include "envoy/thread_local/thread_local.h"
#include "envoy/type/v3/percent.pb.h"

#include "common/common/assert.h"
Expand Down Expand Up @@ -70,7 +71,7 @@ using RandomGeneratorPtr = std::unique_ptr<RandomGenerator>;
/**
* A snapshot of runtime data.
*/
class Snapshot {
class Snapshot : public ThreadLocal::ThreadLocalObject {
public:
virtual ~Snapshot() = default;

Expand Down
10 changes: 7 additions & 3 deletions include/envoy/thread_local/thread_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ class Slot {

/**
* This is a helper on top of get() that casts the object stored in the slot to the specified
* type. Since the slot only stores pointers to the base interface, dynamic_cast provides some
* level of protection via RTTI.
* type. Since the slot only stores pointers to the base interface, the static_cast operates
* in production for performance, and the dynamic_cast validates correctness in tests and debug
* builds.
*/
template <class T> T& getTyped() { return *std::dynamic_pointer_cast<T>(get()); }
template <class T> T& getTyped() {
ASSERT(std::dynamic_pointer_cast<T>(get()) != nullptr);
return *static_cast<T*>(get().get());
}

/**
* Run a callback on all registered threads.
Expand Down
4 changes: 1 addition & 3 deletions source/common/runtime/runtime_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ struct RuntimeStats {
/**
* Implementation of Snapshot whose source is the vector of layers passed to the constructor.
*/
class SnapshotImpl : public Snapshot,
public ThreadLocal::ThreadLocalObject,
Logger::Loggable<Logger::Id::runtime> {
class SnapshotImpl : public Snapshot, Logger::Loggable<Logger::Id::runtime> {
public:
SnapshotImpl(RandomGenerator& generator, RuntimeStats& stats,
std::vector<OverrideLayerConstPtr>&& layers);
Expand Down