Skip to content

Commit

Permalink
factor out string table
Browse files Browse the repository at this point in the history
  • Loading branch information
vilterp committed Sep 16, 2021
1 parent 05c3261 commit 23ab632
Showing 1 changed file with 62 additions and 24 deletions.
86 changes: 62 additions & 24 deletions src/gc-heap-snapshot.cpp
Expand Up @@ -41,33 +41,65 @@ struct Edge {
size_t to_node;
};

typedef unordered_map<string, size_t> MapType;
//template<typename K, typename V>
//auto find_or_insert_iter(unordered_map<K,V>& map, const K &key) {
//}

struct StringTable {
typedef unordered_map<string, size_t> MapType;

MapType map;
vector<string> strings;

StringTable() {}
StringTable(std::initializer_list<string> strs) : strings(strs) {
for (const auto& str : strs) {
map.insert({str, map.size()});
}
}

size_t find_or_create_string_id(string key) {
auto val = map.find(key);
if (val == map.end()) {
val = map.insert(val, {key, map.size()});
strings.push_back(key);
}
return val->second;
}

void print_json_array(JL_STREAM *stream, bool newlines) {
jl_printf(stream, "[");
bool first = true;
for (const auto &str : strings) {
if (first) {
first = false;
} else {
jl_printf(stream, ",");
if (newlines) {
jl_printf(stream, "\n");
}
}
jl_printf(stream, "\"%s\"", str.c_str());
}
jl_printf(stream, "]");
}
};

class HeapSnapshot {
struct HeapSnapshot {
public:

// private:
vector<Node> nodes;

vector<Edge> edges;

MapType names;
StringTable names;
StringTable node_types = {"node_type1", "node_type2"};
StringTable edge_types = {"edge_type1", "edge_type2"};
unordered_set<size_t> seen_node_ids;
};


template<typename K, typename V>
auto find_or_insert_iter(unordered_map<K,V>& map, const K &key) {
auto val = map.find(key);
if (val == map.end()) {
val = map.insert(val, {key, map.size()});
}
return val;
}
size_t find_or_create_string_id(HeapSnapshot& snapshot, string key) {
auto &names = snapshot.names;

return find_or_insert_iter(names, key)->second;
}
// TODO: Do we need to refer to nodes by their index in the node array?
//size_t find_or_create_node_id(HeapSnapshot& snapshot, string key) {
// return find_or_insert_iter(snapshot.nodes_map, key)->second;
Expand Down Expand Up @@ -134,11 +166,13 @@ void serialize_heap_snapshot(JL_STREAM *stream, HeapSnapshot &snapshot) {
jl_printf(stream, "{\"snapshot\":{");
jl_printf(stream, "\"meta\":{");
jl_printf(stream, "\"node_fields\":[\"type\",\"name\",\"id\",\"self_size\",\"edge_count\",\"trace_node_id\",\"detachedness\"],");
jl_printf(stream, "\"node_types\":[[");
// TODO: print string table
jl_printf(stream, "], \"string\", \"number\", \"number\", \"number\", \"number\", \"number\"]");
jl_printf(stream, "\"node_types\":[");
snapshot.node_types.print_json_array(stream, false);
jl_printf(stream, ",\"string\", \"number\", \"number\", \"number\", \"number\", \"number\"]");
jl_printf(stream, "\"edge_fields\":[\"type\",\"name_or_index\",\"to_node\"],");
jl_printf(stream, "\"edge_types\":[[\"property\"],\"string_or_number\",\"node\"],");
jl_printf(stream, "\"edge_types\":[");
snapshot.edge_types.print_json_array(stream, false);
jl_printf(stream, "\"string_or_number\",\"node\"],");
jl_printf(stream, "\"trace_function_info_fields\":[],");
jl_printf(stream, "\"trace_node_fields\":[],");
jl_printf(stream, "\"sample_fields\":[],");
Expand All @@ -157,8 +191,8 @@ void serialize_heap_snapshot(JL_STREAM *stream, HeapSnapshot &snapshot) {
jl_printf(stream, ",");
}
// ["type","name","id","self_size","edge_count","trace_node_id","detachedness"]
jl_printf(stream, "%zu", find_or_create_string_id(snapshot, node.type));
jl_printf(stream, ",%zu", find_or_create_string_id(snapshot, node.name));
jl_printf(stream, "%zu", snapshot.names.find_or_create_string_id(node.type));
jl_printf(stream, ",%zu", snapshot.names.find_or_create_string_id(node.name));
jl_printf(stream, ",%zu", node.id);
jl_printf(stream, ",%zu", 0);//XXX); // self_size
jl_printf(stream, ",%zu", 0);//XXX); // edge_count
Expand All @@ -176,12 +210,16 @@ void serialize_heap_snapshot(JL_STREAM *stream, HeapSnapshot &snapshot) {
} else {
jl_printf(stream, ",");
}
jl_printf(stream, "%zu", find_or_create_string_id(snapshot, edge.type));
jl_printf(stream, "%zu", snapshot.names.find_or_create_string_id(edge.type));
jl_printf(stream, ",%zu", edge.name_or_index);
jl_printf(stream, ",%zu", edge.to_node);
jl_printf(stream, "\n");
}
jl_printf(stream, "]"); // end "edges"
jl_printf(stream, "],\n"); // end "edges"

jl_printf(stream, "\"strings\":");

snapshot.names.print_json_array(stream, true);

jl_printf(stream, "}"); // end "snapshot"
jl_printf(stream, "}");
Expand Down

0 comments on commit 23ab632

Please sign in to comment.