1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
use std::{borrow::Cow, fmt, fmt::Display, rc::Rc};

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

use crate::{graph::Graph, perf::PerfCounter};

use serde::{
    de::{self, DeserializeSeed, SeqAccess, Visitor},
    Deserialize, Deserializer,
};

#[allow(dead_code)]
pub(crate) struct Root {
    pub snapshot: Snapshot,
    pub graph: PetGraph,
    pub strings: Rc<Vec<String>>,
    pub trace_function_infos: Vec<u32>,
    pub trace_tree: Vec<u32>,
    pub samples: Vec<u32>,
    pub locations: Vec<u32>,
}

#[derive(Deserialize)]
pub(crate) struct Snapshot {
    pub meta: Meta,
    pub root_index: Option<usize>,
    // unused:
    // pub node_count: u64,
    // pub edge_count: u64,
    // pub trace_function_count: u64,
}

#[derive(Deserialize)]
#[serde(untagged)]
pub(crate) enum StringOrArray {
    Single(String),
    Arr(Vec<String>),
}

#[derive(Deserialize)]
#[allow(dead_code)]
pub(crate) struct Meta {
    pub node_fields: Vec<String>,
    pub node_types: Vec<StringOrArray>,
    pub edge_fields: Vec<String>,
    pub edge_types: Vec<StringOrArray>,
    pub trace_function_info_fields: Vec<String>,
    pub trace_node_fields: Vec<String>,
    pub sample_fields: Vec<String>,
}

impl<'de> Deserialize<'de> for Root {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_map(RootVisitor)
    }
}

struct RootVisitor;

impl<'de> Visitor<'de> for RootVisitor {
    // This Visitor constructs a single Vec<T> to hold the flattened
    // contents of the inner arrays.
    type Value = Root;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "an object map")
    }

    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: de::MapAccess<'de>,
    {
        let mut snapshot: Option<Snapshot> = None;
        let mut graph: Option<PetGraph> = None;
        let mut has_edges = false;

        let mut trace_function_infos = None;
        let mut trace_tree = None;
        let mut samples = None;
        let mut locations = None;
        let mut strings: Option<Vec<String>> = None;

        while let Some(key) = map.next_key::<Cow<'_, str>>()? {
            match key.as_ref() {
                "snapshot" => {
                    snapshot = map.next_value()?;
                }
                "nodes" => {
                    let snapshot = snapshot.as_ref().ok_or_else(|| {
                        de::Error::custom("expected 'snapshot' before 'nodes' field")
                    })?;

                    graph = Some(map.next_value_seed(NodesDeserializer(snapshot))?);
                }
                "edges" => {
                    let snapshot = snapshot.as_ref().ok_or_else(|| {
                        de::Error::custom("expected 'snapshot' before 'edges' field")
                    })?;
                    let graph = graph.as_mut().ok_or_else(|| {
                        de::Error::custom("expected 'nodes' before 'edges' field")
                    })?;

                    map.next_value_seed(EdgesDeserializer(snapshot, graph))?;
                    has_edges = true;
                }

                "trace_function_infos" => {
                    trace_function_infos = Some(map.next_value()?);
                }
                "trace_tree" => {
                    trace_tree = Some(map.next_value()?);
                }
                "samples" => {
                    samples = Some(map.next_value()?);
                }
                "locations" => {
                    locations = Some(map.next_value()?);
                }
                "strings" => {
                    strings = Some(map.next_value()?);
                }
                _ => {}
            }
        }

        if !has_edges {
            return Err(de::Error::missing_field("edges"));
        }
        let snapshot = snapshot.ok_or_else(|| de::Error::missing_field("snapshot"))?;
        let mut graph = graph.ok_or_else(|| de::Error::missing_field("nodes"))?;
        let strings = Rc::new(strings.ok_or_else(|| de::Error::missing_field("strings"))?);

        for node in graph.node_weights_mut() {
            node.strings = Some(strings.clone());
        }

        Ok(Root {
            snapshot,
            graph,
            trace_function_infos: trace_function_infos.unwrap_or_default(),
            trace_tree: trace_tree.unwrap_or_default(),
            samples: samples.unwrap_or_default(),
            locations: locations.unwrap_or_default(),
            strings,
        })
    }
}
struct NodesDeserializer<'a>(&'a Snapshot);

impl<'de, 'a> DeserializeSeed<'de> for NodesDeserializer<'a> {
    // The return type of the `deserialize` method. This implementation
    // appends onto an existing vector but does not create any new data
    // structure, so the return type is ().
    type Value = PetGraph;

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        // Visitor implementation that will walk an inner array of the JSON
        // input.
        struct NodesVisitor<'a>(&'a Snapshot);

        impl<'de, 'a> Visitor<'de> for NodesVisitor<'a> {
            type Value = PetGraph;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                write!(formatter, "an array of integers")
            }

            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
            where
                A: SeqAccess<'de>,
            {
                let mut name_offset = None;
                let mut id_offset = None;
                let mut self_size_offset = None;
                let mut edge_count_offset = None;
                let mut trace_node_id_offset = None;
                let mut detachedness_offset = None;
                let mut type_offset = None;

                for (i, field) in self.0.meta.node_fields.iter().enumerate() {
                    match field.as_str() {
                        "name" => name_offset = Some(i),
                        "id" => id_offset = Some(i),
                        "self_size" => self_size_offset = Some(i),
                        "edge_count" => edge_count_offset = Some(i),
                        "trace_node_id" => trace_node_id_offset = Some(i),
                        "detachedness" => detachedness_offset = Some(i),
                        "type" => type_offset = Some(i),
                        _ => {}
                    }
                }

                let name_offset = name_offset.ok_or(de::Error::missing_field("name"))?;
                let type_offset = type_offset.ok_or(de::Error::missing_field("type"))?;
                let type_types = match self.0.meta.node_types.get(type_offset) {
                    None => return Err(de::Error::missing_field("type")),
                    Some(StringOrArray::Single(_)) => {
                        return Err(de::Error::custom("node `type` should be an array"))
                    }
                    Some(StringOrArray::Arr(a)) => a,
                };

                let row_size = self.0.meta.node_fields.len();

                let mut graph: PetGraph = petgraph::Graph::new();
                let mut buf: Vec<u64> = vec![0; row_size];
                let mut buf_i /* the vampire slayer */ = 0;
                while let Some(elem) = seq.next_element()? {
                    buf[buf_i] = elem;
                    buf_i += 1;

                    if buf_i == row_size {
                        buf_i = 0;
                        graph.add_node(Node {
                            strings: None,
                            name_index: buf[name_offset] as usize,
                            typ: NodeType::from_str(type_types, buf[type_offset] as usize),
                            self_size: self_size_offset.map(|o| buf[o]).unwrap_or_default(),
                            edge_count: edge_count_offset
                                .map(|o| buf[o] as usize)
                                .unwrap_or_default(),
                            trace_node_id: trace_node_id_offset.map(|o| buf[o]).unwrap_or_default(),
                            detachedness: detachedness_offset
                                .map(|o| buf[o] as u32)
                                .unwrap_or_default(),
                            id: id_offset.map(|o| buf[o] as u32).unwrap_or_default(),
                        });
                    }
                }

                Ok(graph)
            }
        }

        deserializer.deserialize_seq(NodesVisitor(self.0))
    }
}

struct EdgesDeserializer<'a>(&'a Snapshot, &'a mut PetGraph);

impl<'de, 'a> DeserializeSeed<'de> for EdgesDeserializer<'a> {
    // The return type of the `deserialize` method. This implementation
    // appends onto an existing vector but does not create any new data
    // structure, so the return type is ().
    type Value = ();

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        // Visitor implementation that will walk an inner array of the JSON
        // input.
        struct EdgesVisitor<'a>(&'a Snapshot, &'a mut PetGraph);

        impl<'de, 'a> Visitor<'de> for EdgesVisitor<'a> {
            type Value = ();

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                write!(formatter, "an array of integers")
            }

            fn visit_seq<A>(self, mut seq: A) -> Result<(), A::Error>
            where
                A: SeqAccess<'de>,
            {
                let mut to_node_offset = None;
                let mut name_or_index_offset = None;
                let mut type_offset = None;

                for (i, field) in self.0.meta.edge_fields.iter().enumerate() {
                    match field.as_str() {
                        "to_node" => to_node_offset = Some(i),
                        "name_or_index" => name_or_index_offset = Some(i),
                        "type" => type_offset = Some(i),
                        _ => {}
                    }
                }

                let to_node_offset = to_node_offset.ok_or(de::Error::missing_field("to_node"))?;
                let type_offset = type_offset.ok_or(de::Error::missing_field("type"))?;
                let name_or_index_offset =
                    name_or_index_offset.ok_or(de::Error::missing_field("name_or_index"))?;
                let type_types = match self.0.meta.edge_types.get(type_offset) {
                    None => return Err(de::Error::missing_field("type")),
                    Some(StringOrArray::Single(_)) => {
                        return Err(de::Error::custom("edge `type` should be an array"))
                    }
                    Some(StringOrArray::Arr(a)) => a,
                };

                let row_size = self.0.meta.edge_fields.len();
                let node_row_size = self.0.meta.node_fields.len();

                // Each node own the next "edge_count" edges in the array.
                let unexpected_end = || de::Error::custom("unexpected end of edges");
                let nodes_len = self.1.raw_nodes().len();
                for from_index in 0..nodes_len {
                    let edge_count = self.1.raw_nodes()[from_index].weight.edge_count;
                    let from_index = petgraph::graph::NodeIndex::new(from_index);
                    for _ in 0..edge_count {
                        // we know that all the offsets exists and are within
                        // the row_size, so they must be assigned before getting
                        // to the add_edge method.
                        let mut typ: usize = unsafe { std::mem::zeroed() };
                        let mut to_index: usize = unsafe { std::mem::zeroed() };
                        let mut name_or_index: NameOrIndex = unsafe { std::mem::zeroed() };

                        for i in 0..row_size {
                            match i {
                                i if i == to_node_offset => {
                                    to_index = seq.next_element()?.ok_or_else(unexpected_end)?;
                                }
                                i if i == name_or_index_offset => {
                                    name_or_index =
                                        seq.next_element()?.ok_or_else(unexpected_end)?;
                                }
                                i if i == type_offset => {
                                    typ = seq.next_element()?.ok_or_else(unexpected_end)?;
                                }
                                _ => {}
                            }
                        }

                        self.1.add_edge(
                            from_index,
                            petgraph::graph::NodeIndex::new(to_index / node_row_size),
                            PGNodeEdge {
                                typ: EdgeType::from_str(type_types, typ),
                                name: name_or_index,
                            },
                        );
                    }
                }

                Ok(())
            }
        }

        deserializer.deserialize_seq(EdgesVisitor(self.0, self.1))
    }
}

pub(crate) type PetGraph = petgraph::Graph<Node, PGNodeEdge>;

#[derive(Debug)]
pub struct Node {
    name_index: usize,
    strings: Option<Rc<Vec<String>>>,

    pub typ: NodeType,
    pub id: u32,
    pub self_size: u64,
    pub edge_count: usize,
    pub trace_node_id: u64,
    pub detachedness: u32,
}

impl Node {
    pub fn name(&self) -> &str {
        let strs = unsafe { self.strings.as_ref().unwrap_unchecked() };
        &strs[self.name_index]
    }

    pub(crate) fn is_document_dom_trees_root(&self) -> bool {
        self.typ == NodeType::Syntheic && self.name() == "(Document DOM trees)'"
    }

    pub fn class_name(&self) -> &str {
        match &self.typ {
            NodeType::Object | NodeType::Native => self.name(),
            t => t.as_class_name(),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[non_exhaustive]
pub enum NodeType {
    Hidden,
    Array,
    String,
    Object,
    Code,
    Closure,
    RegExp,
    Number,
    Native,
    Syntheic,
    ConcatString,
    SliceString,
    BigInt,
    Other(usize),
}

impl NodeType {
    fn as_class_name(&self) -> &'static str {
        match self {
            Self::Hidden => "(system)",
            Self::Array => "(array)",
            Self::String => "(string)",
            Self::Object => "(object)",
            Self::Code => "(compiled code)",
            Self::Closure => "(closure)",
            Self::RegExp => "(regexp)",
            Self::Number => "(number)",
            Self::Native => "(native)",
            Self::Syntheic => "(synthetic)",
            Self::ConcatString => "(concatenated string)",
            Self::SliceString => "(sliced string)",
            Self::BigInt => "(bigint)",
            Self::Other(_) => "(unknown)",
        }
    }

    fn from_str(strings: &[String], typ: usize) -> Self {
        match strings.get(typ).map(|s| s.as_str()) {
            Some("hidden") => Self::Hidden,
            Some("array") => Self::Array,
            Some("string") => Self::String,
            Some("object") => Self::Object,
            Some("code") => Self::Code,
            Some("closure") => Self::Closure,
            Some("regexp") => Self::RegExp,
            Some("number") => Self::Number,
            Some("native") => Self::Native,
            Some("synthetic") => Self::Syntheic,
            Some("concatenated string") => Self::ConcatString,
            Some("sliced string") => Self::SliceString,
            Some("bigint") => Self::BigInt,
            _ => Self::Other(typ),
        }
    }
}

#[derive(Debug)]
pub struct NodeEdge {
    pub typ: EdgeType,
    pub from_index: usize,
    pub to_index: usize,
    pub name: NameOrIndex,
}

#[derive(Debug)]
pub struct PGNodeEdge {
    pub typ: EdgeType,
    pub name: NameOrIndex,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum NameOrIndex {
    Index(usize),
    Name(String),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
#[repr(u8)]
pub enum EdgeType {
    Context,
    Element,
    Property,
    Internal,
    Hidden,
    Shortcut,
    Weak,
    Invisible,
    Other(usize),
}

impl From<EdgeType> for u8 {
    fn from(t: EdgeType) -> u8 {
        match t {
            EdgeType::Context => 0,
            EdgeType::Element => 1,
            EdgeType::Property => 2,
            EdgeType::Internal => 3,
            EdgeType::Hidden => 4,
            EdgeType::Shortcut => 5,
            EdgeType::Weak => 6,
            EdgeType::Invisible => 7,
            EdgeType::Other(_) => 8,
        }
    }
}

impl Display for EdgeType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            EdgeType::Context => write!(f, "context"),
            EdgeType::Element => write!(f, "element"),
            EdgeType::Property => write!(f, "property"),
            EdgeType::Internal => write!(f, "internal"),
            EdgeType::Hidden => write!(f, "hidden"),
            EdgeType::Shortcut => write!(f, "shortcut"),
            EdgeType::Weak => write!(f, "weak"),
            EdgeType::Invisible => write!(f, "invisible"),
            EdgeType::Other(s) => write!(f, "other<{}>", s),
        }
    }
}

impl EdgeType {
    fn from_str(strings: &[String], index: usize) -> Self {
        match strings.get(index).map(|s| s.as_str()) {
            Some("context") => Self::Context,
            Some("element") => Self::Element,
            Some("property") => Self::Property,
            Some("internal") => Self::Internal,
            Some("hidden") => Self::Hidden,
            Some("shortcut") => Self::Shortcut,
            Some("weak") => Self::Weak,
            Some("invisible") => Self::Invisible,
            _ => Self::Other(index),
        }
    }
}

pub fn decode_reader(input: impl std::io::Read) -> Result<Graph, serde_json::Error> {
    // todo@connor412: parsing the JSON takes the majority of time when parsing
    // a graph. We might be faster if we use DeserializeSeed to parse data
    // directly into the graph structure.
    // https://docs.rs/serde/latest/serde/de/trait.DeserializeSeed.html
    let perf = PerfCounter::new("json_decode");
    serde_json::from_reader(input).map(|b| {
        drop(perf);
        to_graph(b)
    })
}

pub fn decode_slice(input: &[u8]) -> Result<Graph, serde_json::Error> {
    let perf = PerfCounter::new("json_decode");
    serde_json::from_slice(input).map(|b| {
        drop(perf);
        to_graph(b)
    })
}

pub fn decode_str(input: &str) -> Result<Graph, serde_json::Error> {
    let perf = PerfCounter::new("json_decode");
    serde_json::from_str(input).map(|b| {
        drop(perf);
        to_graph(b)
    })
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn decode_bytes(input: &[u8]) -> std::result::Result<Graph, String> {
    decode_slice(input).map_err(|e| e.to_string())
}

fn to_graph(root: Root) -> Graph {
    let _perf = PerfCounter::new("init_graph");
    let root_index = root.snapshot.root_index.unwrap_or_default();
    Graph::new(root.graph, root_index, root.strings)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn test_basic_heapsnapshot() {
        let contents = fs::read("test/basic.heapsnapshot").unwrap();
        decode_slice(&contents).expect("expect no errors");
    }
}