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

perf: optimize data structures in context_bridge::ObjectCache #27664

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 3 additions & 19 deletions shell/renderer/api/context_bridge/object_cache.cc
Expand Up @@ -14,31 +14,16 @@ namespace api {

namespace context_bridge {

ObjectCachePairNode::ObjectCachePairNode(ObjectCachePair&& pair) {
this->pair = std::move(pair);
}

ObjectCachePairNode::~ObjectCachePairNode() = default;

ObjectCache::ObjectCache() {}
ObjectCache::~ObjectCache() {
for (const auto& pair : proxy_map_) {
while (!pair.second.empty()) {
ObjectCachePairNode* node = pair.second.head()->value();
node->RemoveFromList();
delete node;
}
}
}
ObjectCache::~ObjectCache() = default;

void ObjectCache::CacheProxiedObject(v8::Local<v8::Value> from,
v8::Local<v8::Value> proxy_value) {
if (from->IsObject() && !from->IsNullOrUndefined()) {
auto obj = v8::Local<v8::Object>::Cast(from);
int hash = obj->GetIdentityHash();

auto* node = new ObjectCachePairNode(std::make_pair(from, proxy_value));
proxy_map_[hash].Append(node);
proxy_map_[hash].push_front(std::make_pair(from, proxy_value));
}
}

Expand All @@ -54,8 +39,7 @@ v8::MaybeLocal<v8::Value> ObjectCache::GetCachedProxiedObject(
return v8::MaybeLocal<v8::Value>();

auto& list = iter->second;
for (auto* node = list.head(); node != list.end(); node = node->next()) {
auto& pair = node->value()->pair;
for (const auto& pair : list) {
auto from_cmp = pair.first;
if (from_cmp == from) {
if (pair.second.IsEmpty())
Expand Down
11 changes: 2 additions & 9 deletions shell/renderer/api/context_bridge/object_cache.h
Expand Up @@ -5,7 +5,7 @@
#ifndef SHELL_RENDERER_API_CONTEXT_BRIDGE_OBJECT_CACHE_H_
#define SHELL_RENDERER_API_CONTEXT_BRIDGE_OBJECT_CACHE_H_

#include <map>
#include <unordered_map>
#include <utility>

#include "base/containers/linked_list.h"
Expand All @@ -22,13 +22,6 @@ namespace context_bridge {

using ObjectCachePair = std::pair<v8::Local<v8::Value>, v8::Local<v8::Value>>;

struct ObjectCachePairNode : public base::LinkNode<ObjectCachePairNode> {
explicit ObjectCachePairNode(ObjectCachePair&& pair);
~ObjectCachePairNode();

ObjectCachePair pair;
};

class ObjectCache final {
public:
ObjectCache();
Expand All @@ -41,7 +34,7 @@ class ObjectCache final {

private:
// object_identity ==> [from_value, proxy_value]
std::map<int, base::LinkedList<ObjectCachePairNode>> proxy_map_;
std::unordered_map<int, std::forward_list<ObjectCachePair>> proxy_map_;
};

} // namespace context_bridge
Expand Down