Skip to content

Commit

Permalink
Small FlatMap optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
chenBright committed Aug 3, 2024
1 parent 64ce760 commit 3ac6e31
Show file tree
Hide file tree
Showing 16 changed files with 590 additions and 422 deletions.
1 change: 0 additions & 1 deletion src/brpc/builtin/hotspots_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ static DisplayType StringToDisplayType(const std::string& val) {
static std::once_flag flag;
std::call_once(flag, []() {
display_type_map = new butil::CaseIgnoredFlatMap<DisplayType>;
display_type_map->init(10);
(*display_type_map)["dot"] = DisplayType::kDot;
#if defined(OS_LINUX)
(*display_type_map)["flame"] = DisplayType::kFlameGraph;
Expand Down
2 changes: 0 additions & 2 deletions src/brpc/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ friend void policy::ProcessThriftRequest(InputMessageBase*);
UserFieldsMap* request_user_fields() {
if (!_request_user_fields) {
_request_user_fields = new UserFieldsMap;
_request_user_fields->init(29);
}
return _request_user_fields;
}
Expand All @@ -270,7 +269,6 @@ friend void policy::ProcessThriftRequest(InputMessageBase*);
UserFieldsMap* response_user_fields() {
if (!_response_user_fields) {
_response_user_fields = new UserFieldsMap;
_response_user_fields->init(29);
}
return _response_user_fields;
}
Expand Down
3 changes: 1 addition & 2 deletions src/brpc/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ class Extension {

private:
friend class butil::GetLeakySingleton<Extension<T> >;
Extension();
~Extension();
Extension() = default;
butil::CaseIgnoredFlatMap<T*> _instance_map;
butil::Mutex _map_mutex;
};
Expand Down
9 changes: 0 additions & 9 deletions src/brpc/extension_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ Extension<T>* Extension<T>::instance() {
return butil::get_leaky_singleton<Extension<T> >();
}

template <typename T>
Extension<T>::Extension() {
_instance_map.init(29);
}

template <typename T>
Extension<T>::~Extension() {
}

template <typename T>
int Extension<T>::Register(const std::string& name, T* instance) {
if (NULL == instance) {
Expand Down
20 changes: 10 additions & 10 deletions src/brpc/kvmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ class KVMap {
// Get value of a key(case-sensitive)
// Return pointer to the value, NULL on not found.
const std::string* Get(const char* key) const { return _entries.seek(key); }
const std::string* Get(const std::string& key) const { return _entries.seek(key); }
const std::string* Get(const std::string& key) const {
return _entries.seek(key);
}

// Set value of a key
void Set(const std::string& key, const std::string& value) { GetOrAdd(key) = value; }
void Set(const std::string& key, const char* value) { GetOrAdd(key) = value; }
void Set(const std::string& key, const std::string& value) {
_entries[key] = value;
}
void Set(const std::string& key, const char* value) { _entries[key] = value; }
// Convert other types to string as well
template <typename T>
void Set(const std::string& key, const T& value) { GetOrAdd(key) = std::to_string(value); }
void Set(const std::string& key, const T& value) {
_entries[key] = std::to_string(value);
}

// Remove a key
void Remove(const char* key) { _entries.erase(key); }
Expand All @@ -60,12 +66,6 @@ class KVMap {
size_t Count() const { return _entries.size(); }

private:
std::string& GetOrAdd(const std::string& key) {
if (!_entries.initialized()) {
_entries.init(29);
}
return _entries[key];
}

Map _entries;
};
Expand Down
6 changes: 0 additions & 6 deletions src/brpc/uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ static void ParseQueries(URI::QueryMap& query_map, const std::string &query) {
}
for (QuerySplitter sp(query.c_str()); sp; ++sp) {
if (!sp.key().empty()) {
if (!query_map.initialized()) {
query_map.init(URI::QUERY_MAP_INITIAL_BUCKET);
}
std::string key(sp.key().data(), sp.key().size());
std::string value(sp.value().data(), sp.value().size());
query_map[key] = value;
Expand Down Expand Up @@ -347,9 +344,6 @@ void URI::PrintWithoutHost(std::ostream& os) const {
}

void URI::InitializeQueryMap() const {
if (!_query_map.initialized()) {
CHECK_EQ(0, _query_map.init(QUERY_MAP_INITIAL_BUCKET));
}
ParseQueries(_query_map, _query);
_query_was_modified = false;
_initialized_query_map = true;
Expand Down
1 change: 0 additions & 1 deletion src/brpc/uri.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ namespace brpc {
// interpretable as extension
class URI {
public:
static const size_t QUERY_MAP_INITIAL_BUCKET = 16;
typedef butil::FlatMap<std::string, std::string> QueryMap;
typedef QueryMap::const_iterator QueryIterator;

Expand Down
2 changes: 0 additions & 2 deletions src/bthread/butex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ int butex_wake_all(void* arg, bool nosignal) {
return nwakeup;
}
butil::FlatMap<bthread_tag_t, TaskGroup*> nwakeups;
nwakeups.init(FLAGS_task_group_ntags);
// We will exchange with first waiter in the end.
ButexBthreadWaiter* next = static_cast<ButexBthreadWaiter*>(
bthread_waiters.head()->value());
Expand Down Expand Up @@ -436,7 +435,6 @@ int butex_wake_except(void* arg, bthread_t excluded_bthread) {
return nwakeup;
}
butil::FlatMap<bthread_tag_t, TaskGroup*> nwakeups;
nwakeups.init(FLAGS_task_group_ntags);
do {
// pop reversely
ButexBthreadWaiter* w = static_cast<ButexBthreadWaiter*>(bthread_waiters.tail()->value());
Expand Down
Loading

0 comments on commit 3ac6e31

Please sign in to comment.