Commit 0134ddae authored by leszeks's avatar leszeks Committed by Commit bot

[interpreter] Use hashmap for ConstantArrayBuilder's constant map

Uses the base hashmap to store the ConstantArrayBuilder's constant map,
which slightly improves the performance of ConstantArrayBuilder::Insert.

Includes a small overload of the hashmap LookupOrInsert method, which
allows passing in a value creation function instead of just default
initialising new values.

On Octane's codeload, this gives (on my machine) a 0.27% improvement,
which doesn't sound like a lot but I guess every little helps.

Review-Url: https://codereview.chromium.org/2336553002
Cr-Commit-Position: refs/heads/master@{#39883}
parent 5352e7a5
......@@ -52,6 +52,13 @@ class TemplateHashMapImpl {
Entry* LookupOrInsert(const Key& key, uint32_t hash,
AllocationPolicy allocator = AllocationPolicy());
// If an entry with matching key is found, returns that entry.
// If no matching entry is found, a new entry is inserted with
// corresponding key, key hash, and value created by func.
template <typename Func>
Entry* LookupOrInsert(const Key& key, uint32_t hash, const Func& value_func,
AllocationPolicy allocator = AllocationPolicy());
Entry* InsertNew(const Key& key, uint32_t hash,
AllocationPolicy allocator = AllocationPolicy());
......@@ -126,13 +133,23 @@ template <typename Key, typename Value, typename MatchFun,
typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry*
TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::LookupOrInsert(
const Key& key, uint32_t hash, AllocationPolicy allocator) {
return LookupOrInsert(key, hash, []() { return Value(); }, allocator);
}
template <typename Key, typename Value, typename MatchFun,
class AllocationPolicy>
template <typename Func>
typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry*
TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::LookupOrInsert(
const Key& key, uint32_t hash, const Func& value_func,
AllocationPolicy allocator) {
// Find a matching entry.
Entry* entry = Probe(key, hash);
if (entry->exists()) {
return entry;
}
return FillEmptyEntry(entry, key, Value(), hash, allocator);
return FillEmptyEntry(entry, key, value_func(), hash, allocator);
}
template <typename Key, typename Value, typename MatchFun,
......
......@@ -4,6 +4,7 @@
#include "src/interpreter/constant-array-builder.h"
#include <functional>
#include <set>
#include "src/isolate.h"
......@@ -72,9 +73,10 @@ STATIC_CONST_MEMBER_DEFINITION const size_t
ConstantArrayBuilder::ConstantArrayBuilder(Zone* zone,
Handle<Object> the_hole_value)
: constants_map_(zone),
: constants_map_(std::equal_to<Address>(), 16, ZoneAllocationPolicy(zone)),
smi_map_(zone),
smi_pairs_(zone),
zone_(zone),
the_hole_value_(the_hole_value) {
idx_slice_[0] =
new (zone) ConstantArraySlice(zone, 0, k8BitCapacity, OperandSize::kByte);
......@@ -153,16 +155,11 @@ Handle<FixedArray> ConstantArrayBuilder::ToFixedArray(Isolate* isolate) {
}
size_t ConstantArrayBuilder::Insert(Handle<Object> object) {
auto entry = constants_map_.find(object.address());
return (entry == constants_map_.end()) ? AllocateEntry(object)
: entry->second;
}
ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateEntry(
Handle<Object> object) {
index_t index = AllocateIndex(object);
constants_map_[object.address()] = index;
return index;
return constants_map_
.LookupOrInsert(object.address(), ObjectHash(object.address()),
[&]() { return AllocateIndex(object); },
ZoneAllocationPolicy(zone_))
->value;
}
ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateIndex(
......
......@@ -70,7 +70,6 @@ class ConstantArrayBuilder final BASE_EMBEDDED {
private:
typedef uint32_t index_t;
index_t AllocateEntry(Handle<Object> object);
index_t AllocateIndex(Handle<Object> object);
index_t AllocateReservedEntry(Smi* value);
......@@ -108,9 +107,12 @@ class ConstantArrayBuilder final BASE_EMBEDDED {
Handle<Object> the_hole_value() const { return the_hole_value_; }
ConstantArraySlice* idx_slice_[3];
ZoneMap<Address, index_t> constants_map_;
base::TemplateHashMapImpl<Address, index_t, std::equal_to<Address>,
ZoneAllocationPolicy>
constants_map_;
ZoneMap<Smi*, index_t> smi_map_;
ZoneVector<std::pair<Smi*, index_t>> smi_pairs_;
Zone* zone_;
Handle<Object> the_hole_value_;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment