Skip to content

Commit 8805480

Browse files
committed
TypeGraph: Make Primitive types singletons
Should save some memory and improve performance by not having loads of copies of primitive types.
1 parent 0330ef8 commit 8805480

File tree

5 files changed

+85
-2
lines changed

5 files changed

+85
-2
lines changed

oi/type_graph/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_library(type_graph
1010
RemoveIgnored.cpp
1111
RemoveTopLevelPointer.cpp
1212
TopoSorter.cpp
13+
TypeGraph.cpp
1314
TypeIdentifier.cpp
1415
Types.cpp
1516
)

oi/type_graph/DrgnParser.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,6 @@ Array* DrgnParser::enumerateArray(struct drgn_type* type) {
450450
return make_type<Array>(type, t, len);
451451
}
452452

453-
// TODO deduplication of primitive types (also remember they're not only created
454-
// here)
455453
Primitive* DrgnParser::enumeratePrimitive(struct drgn_type* type) {
456454
Primitive::Kind kind;
457455
switch (drgn_type_kind(type)) {

oi/type_graph/Printer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
namespace type_graph {
2121

2222
Printer::Printer(std::ostream& out, size_t numTypes) : out_(out) {
23+
if (numTypes == 0) {
24+
baseIndent_ = 0;
25+
return;
26+
}
27+
2328
// Enough space for "[XYZ] ", where XYZ is the largest node number:
2429
baseIndent_ = static_cast<int>(log10(static_cast<double>(numTypes)) + 1) + 3;
2530
}

oi/type_graph/TypeGraph.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "TypeGraph.h"
17+
18+
namespace type_graph {
19+
20+
template <>
21+
Primitive* TypeGraph::make_type<Primitive>(Primitive::Kind kind) {
22+
switch (kind) {
23+
case Primitive::Kind::Int8:
24+
static Primitive pInt8{kind};
25+
return &pInt8;
26+
case Primitive::Kind::Int16:
27+
static Primitive pInt16{kind};
28+
return &pInt16;
29+
case Primitive::Kind::Int32:
30+
static Primitive pInt32{kind};
31+
return &pInt32;
32+
case Primitive::Kind::Int64:
33+
static Primitive pInt64{kind};
34+
return &pInt64;
35+
case Primitive::Kind::UInt8:
36+
static Primitive pUInt8{kind};
37+
return &pUInt8;
38+
case Primitive::Kind::UInt16:
39+
static Primitive pUInt16{kind};
40+
return &pUInt16;
41+
case Primitive::Kind::UInt32:
42+
static Primitive pUInt32{kind};
43+
return &pUInt32;
44+
case Primitive::Kind::UInt64:
45+
static Primitive pUInt64{kind};
46+
return &pUInt64;
47+
case Primitive::Kind::Float32:
48+
static Primitive pFloat32{kind};
49+
return &pFloat32;
50+
case Primitive::Kind::Float64:
51+
static Primitive pFloat64{kind};
52+
return &pFloat64;
53+
case Primitive::Kind::Float80:
54+
static Primitive pFloat80{kind};
55+
return &pFloat80;
56+
case Primitive::Kind::Float128:
57+
static Primitive pFloat128{kind};
58+
return &pFloat128;
59+
case Primitive::Kind::Bool:
60+
static Primitive pBool{kind};
61+
return &pBool;
62+
case Primitive::Kind::UIntPtr:
63+
static Primitive pUIntPtr{kind};
64+
return &pUIntPtr;
65+
case Primitive::Kind::Void:
66+
static Primitive pVoid{kind};
67+
return &pVoid;
68+
}
69+
}
70+
71+
} // namespace type_graph

oi/type_graph/TypeGraph.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,16 @@ class TypeGraph {
4242
rootTypes_.push_back(type);
4343
}
4444

45+
// Override of the generic make_type function that returns singleton Primitive
46+
// objects
47+
template <typename T>
48+
Primitive* make_type(Primitive::Kind kind);
49+
4550
template <typename T, typename... Args>
4651
T* make_type(Args&&... args) {
52+
static_assert(!std::is_same<T, Primitive>::value,
53+
"Primitive singleton override should be used");
54+
4755
auto type_unique_ptr = std::make_unique<T>(std::forward<Args>(args)...);
4856
auto type_raw_ptr = type_unique_ptr.get();
4957
types_.push_back(std::move(type_unique_ptr));

0 commit comments

Comments
 (0)