Skip to content

Commit b9b1ea6

Browse files
sstricklcommit-bot@chromium.org
authored andcommitted
[vm] Refactor hash maps with T* keys where T <: Object.
Rename PointerKeyValueTrait<T>, which is used to create sets of pointers to T instances where T <: Object and T has appropriate Hash and Equals instance methods, to the more specific name PointerSetKeyValueTrait. Create a PointerSet<T> alias for using this trait with DirectChainedHashMap and use that alias in other code as a shorthand. Remove PointerSetKeyValueTrait<const char> as a superclass of CStringKeyValueTrait, as the only reuse from the former are the two methods KeyOf and ValueOf which just return their argument, and having this relationship is odd since const char is not a subtype of Object. TEST=Renaming/refactoring, so existing tests. Change-Id: I0274b16cb9fcb3939a28fb109fb8626c1ac8c0e9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/215761 Commit-Queue: Tess Strickland <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
1 parent 0aeaa80 commit b9b1ea6

File tree

6 files changed

+33
-37
lines changed

6 files changed

+33
-37
lines changed

runtime/vm/compiler/backend/range_analysis.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,6 @@ class Scheduler {
642642
}
643643

644644
private:
645-
typedef DirectChainedHashMap<PointerKeyValueTrait<Instruction> > Map;
646-
647645
Instruction* EmitRecursively(Instruction* instruction, Instruction* sink) {
648646
// Schedule all unscheduled inputs and unwrap all constrained inputs.
649647
for (intptr_t i = 0; i < instruction->InputCount(); i++) {
@@ -723,7 +721,7 @@ class Scheduler {
723721
}
724722

725723
FlowGraph* flow_graph_;
726-
Map map_;
724+
PointerSet<Instruction> map_;
727725
const ZoneGrowableArray<BlockEntryInstr*>& loop_headers_;
728726
GrowableArray<BlockEntryInstr*> pre_headers_;
729727
GrowableArray<Instruction*> emitted_;

runtime/vm/compiler/backend/redundancy_elimination.cc

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ class CSEInstructionMap : public ValueObject {
4646
}
4747

4848
private:
49-
typedef DirectChainedHashMap<PointerKeyValueTrait<Instruction> > Map;
50-
51-
Map map_;
49+
PointerSet<Instruction> map_;
5250
};
5351

5452
// Place describes an abstract location (e.g. field) that IR can load
@@ -683,7 +681,7 @@ class PhiPlaceMoves : public ZoneAllocated {
683681
class AliasedSet : public ZoneAllocated {
684682
public:
685683
AliasedSet(Zone* zone,
686-
DirectChainedHashMap<PointerKeyValueTrait<Place> >* places_map,
684+
PointerSet<Place>* places_map,
687685
ZoneGrowableArray<Place*>* places,
688686
PhiPlaceMoves* phi_moves)
689687
: zone_(zone),
@@ -1179,7 +1177,7 @@ class AliasedSet : public ZoneAllocated {
11791177

11801178
Zone* zone_;
11811179

1182-
DirectChainedHashMap<PointerKeyValueTrait<Place> >* places_map_;
1180+
PointerSet<Place>* places_map_;
11831181

11841182
const ZoneGrowableArray<Place*>& places_;
11851183

@@ -1188,7 +1186,7 @@ class AliasedSet : public ZoneAllocated {
11881186
// A list of all seen aliases and a map that allows looking up canonical
11891187
// alias object.
11901188
GrowableArray<const Place*> aliases_;
1191-
DirectChainedHashMap<PointerKeyValueTrait<const Place> > aliases_map_;
1189+
PointerSet<const Place> aliases_map_;
11921190

11931191
SmallSet<Place::ElementSize> typed_data_access_sizes_;
11941192

@@ -1244,9 +1242,8 @@ static bool IsPhiDependentPlace(Place* place) {
12441242
// corresponding to phi input are numbered and record outgoing phi moves
12451243
// for each block which establish correspondence between phi dependent place
12461244
// and phi input's place that is flowing in.
1247-
static PhiPlaceMoves* ComputePhiMoves(
1248-
DirectChainedHashMap<PointerKeyValueTrait<Place> >* map,
1249-
ZoneGrowableArray<Place*>* places) {
1245+
static PhiPlaceMoves* ComputePhiMoves(PointerSet<Place>* map,
1246+
ZoneGrowableArray<Place*>* places) {
12501247
Thread* thread = Thread::Current();
12511248
Zone* zone = thread->zone();
12521249
PhiPlaceMoves* phi_moves = new (zone) PhiPlaceMoves();
@@ -1300,10 +1297,9 @@ DART_FORCE_INLINE static intptr_t GetPlaceId(const Instruction* instr) {
13001297

13011298
enum CSEMode { kOptimizeLoads, kOptimizeStores };
13021299

1303-
static AliasedSet* NumberPlaces(
1304-
FlowGraph* graph,
1305-
DirectChainedHashMap<PointerKeyValueTrait<Place> >* map,
1306-
CSEMode mode) {
1300+
static AliasedSet* NumberPlaces(FlowGraph* graph,
1301+
PointerSet<Place>* map,
1302+
CSEMode mode) {
13071303
// Loads representing different expression ids will be collected and
13081304
// used to build per offset kill sets.
13091305
Zone* zone = graph->zone();
@@ -1735,7 +1731,7 @@ class LoadOptimizer : public ValueObject {
17351731
return false;
17361732
}
17371733

1738-
DirectChainedHashMap<PointerKeyValueTrait<Place> > map;
1734+
PointerSet<Place> map;
17391735
AliasedSet* aliased_set = NumberPlaces(graph, &map, kOptimizeLoads);
17401736
if ((aliased_set != NULL) && !aliased_set->IsEmpty()) {
17411737
// If any loads were forwarded return true from Optimize to run load
@@ -2774,7 +2770,7 @@ class LoadOptimizer : public ValueObject {
27742770
}
27752771

27762772
FlowGraph* graph_;
2777-
DirectChainedHashMap<PointerKeyValueTrait<Place> >* map_;
2773+
PointerSet<Place>* map_;
27782774

27792775
// Mapping between field offsets in words and expression ids of loads from
27802776
// that offset.
@@ -2866,7 +2862,7 @@ class StoreOptimizer : public LivenessAnalysis {
28662862
public:
28672863
StoreOptimizer(FlowGraph* graph,
28682864
AliasedSet* aliased_set,
2869-
DirectChainedHashMap<PointerKeyValueTrait<Place> >* map)
2865+
PointerSet<Place>* map)
28702866
: LivenessAnalysis(aliased_set->max_place_id(), graph->postorder()),
28712867
graph_(graph),
28722868
map_(map),
@@ -2887,7 +2883,7 @@ class StoreOptimizer : public LivenessAnalysis {
28872883
return;
28882884
}
28892885

2890-
DirectChainedHashMap<PointerKeyValueTrait<Place> > map;
2886+
PointerSet<Place> map;
28912887
AliasedSet* aliased_set = NumberPlaces(graph, &map, kOptimizeStores);
28922888
if ((aliased_set != NULL) && !aliased_set->IsEmpty()) {
28932889
StoreOptimizer store_optimizer(graph, aliased_set, &map);
@@ -3048,7 +3044,7 @@ class StoreOptimizer : public LivenessAnalysis {
30483044
}
30493045

30503046
FlowGraph* graph_;
3051-
DirectChainedHashMap<PointerKeyValueTrait<Place> >* map_;
3047+
PointerSet<Place>* map_;
30523048

30533049
// Mapping between field offsets in words and expression ids of loads from
30543050
// that offset.

runtime/vm/compiler/backend/slot.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class SlotCache : public ZoneAllocated {
4343
: zone_(thread->zone()), fields_(thread->zone()) {}
4444

4545
Zone* const zone_;
46-
DirectChainedHashMap<PointerKeyValueTrait<const Slot> > fields_;
46+
PointerSet<const Slot> fields_;
4747
};
4848

4949
#define NATIVE_SLOT_NAME(C, F) Kind::k##C##_##F

runtime/vm/hash_map.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -359,21 +359,21 @@ class ZoneDirectChainedHashMap
359359
};
360360

361361
template <typename T>
362-
class PointerKeyValueTrait {
362+
class PointerSetKeyValueTrait {
363363
public:
364364
typedef T* Value;
365365
typedef T* Key;
366366
typedef T* Pair;
367367

368368
static Key KeyOf(Pair kv) { return kv; }
369-
370369
static Value ValueOf(Pair kv) { return kv; }
371-
372370
static inline uword Hash(Key key) { return key->Hash(); }
373-
374371
static inline bool IsKeyEqual(Pair kv, Key key) { return kv->Equals(*key); }
375372
};
376373

374+
template <typename T>
375+
using PointerSet = DirectChainedHashMap<PointerSetKeyValueTrait<T>>;
376+
377377
template <typename T>
378378
class NumbersKeyValueTrait {
379379
public:
@@ -408,12 +408,14 @@ class RawPointerKeyValueTrait {
408408
static bool IsKeyEqual(Pair kv, Key key) { return kv.key == key; }
409409
};
410410

411-
class CStringSetKeyValueTrait : public PointerKeyValueTrait<const char> {
411+
class CStringSetKeyValueTrait {
412412
public:
413-
using Key = PointerKeyValueTrait<const char>::Key;
414-
using Value = PointerKeyValueTrait<const char>::Value;
415-
using Pair = PointerKeyValueTrait<const char>::Pair;
413+
using Key = const char*;
414+
using Value = const char*;
415+
using Pair = const char*;
416416

417+
static Key KeyOf(Pair kv) { return kv; }
418+
static Value ValueOf(Pair kv) { return kv; }
417419
static uword Hash(Key key) {
418420
ASSERT(key != nullptr);
419421
return Utils::StringHash(key, strlen(key));

runtime/vm/hash_map_test.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TestValue {
1919
};
2020

2121
TEST_CASE(DirectChainedHashMap) {
22-
DirectChainedHashMap<PointerKeyValueTrait<TestValue> > map;
22+
DirectChainedHashMap<PointerSetKeyValueTrait<TestValue>> map;
2323
EXPECT(map.IsEmpty());
2424
TestValue v1(0);
2525
TestValue v2(1);
@@ -33,14 +33,14 @@ TEST_CASE(DirectChainedHashMap) {
3333
EXPECT(map.Remove(&v1));
3434
EXPECT(map.Lookup(&v1) == NULL);
3535
map.Insert(&v1);
36-
DirectChainedHashMap<PointerKeyValueTrait<TestValue> > map2(map);
36+
DirectChainedHashMap<PointerSetKeyValueTrait<TestValue>> map2(map);
3737
EXPECT(map2.LookupValue(&v1) == &v1);
3838
EXPECT(map2.LookupValue(&v2) == &v2);
3939
EXPECT(map2.LookupValue(&v3) == &v1);
4040
}
4141

4242
TEST_CASE(DirectChainedHashMapInsertRemove) {
43-
DirectChainedHashMap<PointerKeyValueTrait<TestValue> > map;
43+
DirectChainedHashMap<PointerSetKeyValueTrait<TestValue>> map;
4444
EXPECT(map.IsEmpty());
4545
TestValue v1(1);
4646
TestValue v2(3); // Note: v1, v2, v3 should have the same hash.
@@ -97,7 +97,7 @@ TEST_CASE(DirectChainedHashMapInsertRemove) {
9797
}
9898

9999
TEST_CASE(MallocDirectChainedHashMap) {
100-
MallocDirectChainedHashMap<PointerKeyValueTrait<TestValue> > map;
100+
MallocDirectChainedHashMap<PointerSetKeyValueTrait<TestValue>> map;
101101
EXPECT(map.IsEmpty());
102102
TestValue v1(0);
103103
TestValue v2(1);
@@ -108,7 +108,7 @@ TEST_CASE(MallocDirectChainedHashMap) {
108108
EXPECT(map.LookupValue(&v1) == &v1);
109109
EXPECT(map.LookupValue(&v2) == &v2);
110110
EXPECT(map.LookupValue(&v3) == &v1);
111-
MallocDirectChainedHashMap<PointerKeyValueTrait<TestValue> > map2(map);
111+
MallocDirectChainedHashMap<PointerSetKeyValueTrait<TestValue>> map2(map);
112112
EXPECT(map2.LookupValue(&v1) == &v1);
113113
EXPECT(map2.LookupValue(&v2) == &v2);
114114
EXPECT(map2.LookupValue(&v3) == &v1);
@@ -117,7 +117,7 @@ TEST_CASE(MallocDirectChainedHashMap) {
117117
TEST_CASE(ZoneDirectChainedHashMap) {
118118
auto zone = thread->zone();
119119
auto const map = new (zone)
120-
ZoneDirectChainedHashMap<PointerKeyValueTrait<TestValue>>(zone);
120+
ZoneDirectChainedHashMap<PointerSetKeyValueTrait<TestValue>>(zone);
121121
EXPECT(map->IsEmpty());
122122
TestValue v1(0);
123123
TestValue v2(1);

runtime/vm/program_visitor.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ void ProgramVisitor::NormalizeAndDedupCompressedStackMaps(Thread* thread) {
646646
class NormalizeAndDedupCompressedStackMapsVisitor
647647
: public CodeVisitor,
648648
public Dedupper<CompressedStackMaps,
649-
PointerKeyValueTrait<const CompressedStackMaps>> {
649+
PointerSetKeyValueTrait<const CompressedStackMaps>> {
650650
public:
651651
NormalizeAndDedupCompressedStackMapsVisitor(Zone* zone,
652652
IsolateGroup* isolate_group)

0 commit comments

Comments
 (0)