Skip to content

Commit

Permalink
threadlocal: avoiding a dynamic cast in opt builds (envoyproxy#11900)
Browse files Browse the repository at this point in the history
requested follow-up PR from envoyproxy#11796 as it's more consistent with our style guide.

Risk Level: low
Testing: n/a
Docs Changes: n/a
Release Notes: n/a

Signed-off-by: Alyssa Wilk <alyssar@chromium.org>
  • Loading branch information
alyssawilk authored Jul 7, 2020
1 parent ef74d8f commit 363b104
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
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
5 changes: 2 additions & 3 deletions 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,10 +71,8 @@ using RandomGeneratorPtr = std::unique_ptr<RandomGenerator>;
/**
* A snapshot of runtime data.
*/
class Snapshot {
class Snapshot : public ThreadLocal::ThreadLocalObject {
public:
virtual ~Snapshot() = default;

struct Entry {
std::string raw_string_value_;
absl::optional<uint64_t> uint_value_;
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

0 comments on commit 363b104

Please sign in to comment.