electron/shell/renderer/api/context_bridge/object_cache.cc
Samuel Attard 09870d97b5
perf: optimize data structures in context_bridge::ObjectCache (#27639)
* Use std::forward_list instead of base::LinkedList for better perf,
more consistent memory management.  Better than std::list because we
don't need the double-linked-list behavior of std::list
* Use std::unordered_map instead of std::map for the v8 hash table
2021-02-08 12:30:25 -08:00

57 lines
1.5 KiB
C++

// Copyright (c) 2020 Slack Technologies, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/renderer/api/context_bridge/object_cache.h"
#include <utility>
#include "shell/common/api/object_life_monitor.h"
namespace electron {
namespace api {
namespace context_bridge {
ObjectCache::ObjectCache() {}
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();
proxy_map_[hash].push_front(std::make_pair(from, proxy_value));
}
}
v8::MaybeLocal<v8::Value> ObjectCache::GetCachedProxiedObject(
v8::Local<v8::Value> from) const {
if (!from->IsObject() || from->IsNullOrUndefined())
return v8::MaybeLocal<v8::Value>();
auto obj = v8::Local<v8::Object>::Cast(from);
int hash = obj->GetIdentityHash();
auto iter = proxy_map_.find(hash);
if (iter == proxy_map_.end())
return v8::MaybeLocal<v8::Value>();
auto& list = iter->second;
for (const auto& pair : list) {
auto from_cmp = pair.first;
if (from_cmp == from) {
if (pair.second.IsEmpty())
return v8::MaybeLocal<v8::Value>();
return pair.second;
}
}
return v8::MaybeLocal<v8::Value>();
}
} // namespace context_bridge
} // namespace api
} // namespace electron