-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize.go
79 lines (65 loc) · 1.81 KB
/
serialize.go
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
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pgo
import (
"bufio"
"fmt"
"io"
)
// Serialization of a Profile allows go tool preprofile to construct the edge
// map only once (rather than once per compile process). The compiler processes
// then parse the pre-processed data directly from the serialized format.
//
// The format of the serialized output is as follows.
//
// GO PREPROFILE V1
// caller_name
// callee_name
// "call site offset" "call edge weight"
// ...
// caller_name
// callee_name
// "call site offset" "call edge weight"
//
// Entries are sorted by "call edge weight", from highest to lowest.
const serializationHeader = "GO PREPROFILE V1\n"
// WriteTo writes a serialized representation of Profile to w.
//
// FromSerialized can parse the format back to Profile.
//
// WriteTo implements io.WriterTo.Write.
func (d *Profile) WriteTo(w io.Writer) (int64, error) {
bw := bufio.NewWriter(w)
var written int64
// Header
n, err := bw.WriteString(serializationHeader)
written += int64(n)
if err != nil {
return written, err
}
for _, edge := range d.NamedEdgeMap.ByWeight {
weight := d.NamedEdgeMap.Weight[edge]
n, err = fmt.Fprintln(bw, edge.CallerName)
written += int64(n)
if err != nil {
return written, err
}
n, err = fmt.Fprintln(bw, edge.CalleeName)
written += int64(n)
if err != nil {
return written, err
}
n, err = fmt.Fprintf(bw, "%d %d\n", edge.CallSiteOffset, weight)
written += int64(n)
if err != nil {
return written, err
}
}
if err := bw.Flush(); err != nil {
return written, err
}
// No need to serialize TotalWeight, it can be trivially recomputed
// during parsing.
return written, nil
}