-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSerde.lean
More file actions
161 lines (127 loc) · 4.58 KB
/
Serde.lean
File metadata and controls
161 lines (127 loc) · 4.58 KB
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
module
public import Ix.Ixon
public import Ix.Benchmark.Change
public import Ix.Benchmark.OneShot
public section
open Ixon
-- Local Serialize instances for benchmark data types (ephemeral format)
instance : Serialize Nat where
put n := putU64LE n.toUInt64
get := do let v ← getU64LE; return v.toNat
instance [Serialize α] : Serialize (List α) where
put xs := do
putU64LE xs.length.toUInt64
for x in xs do Serialize.put x
get := do
let n ← getU64LE
let mut xs := []
for _ in List.range n.toNat do
xs := (← Serialize.get) :: xs
return xs.reverse
@[inline] def putFloat (x : Float) : PutM Unit := putU64LE x.toBits
@[inline] def getFloat : GetM Float := getU64LE.map Float.ofBits
instance : Serialize Float where
put := putFloat
get := getFloat
def putTupleNat (xy : Nat × Nat) : PutM Unit := do
Serialize.put xy.fst
Serialize.put xy.snd
def getTupleNat : GetM (Nat × Nat) := do
return (← Serialize.get, ← Serialize.get)
instance : Serialize (Nat × Nat) where
put := putTupleNat
get := getTupleNat
def putData (data : Data) : PutM Unit := do
Serialize.put data.d.toList
def getData : GetM Data := do
let data : List (Nat × Nat) ← Serialize.get
return { d := data.toArray }
instance : Serialize Data where
put := putData
get := getData
def putConfidenceInterval (ci : ConfidenceInterval) : PutM Unit := do
putFloat ci.confidenceLevel
putFloat ci.lowerBound
putFloat ci.upperBound
def getConfidenceInterval : GetM ConfidenceInterval := do
return { confidenceLevel := (← getFloat), lowerBound := (← getFloat), upperBound := (← getFloat)}
instance : Serialize ConfidenceInterval where
put := putConfidenceInterval
get := getConfidenceInterval
def putEstimate (est : Estimate) : PutM Unit := do
putConfidenceInterval est.confidenceInterval
putFloat est.pointEstimate
putFloat est.stdErr
def getEstimate : GetM Estimate := do
return { confidenceInterval := (← getConfidenceInterval), pointEstimate := (← getFloat), stdErr := (← getFloat)}
instance : Serialize Estimate where
put := putEstimate
get := getEstimate
def putEstimates (est : Estimates) : PutM Unit := do
putEstimate est.mean
putEstimate est.median
putEstimate est.medianAbsDev
if let .some x := est.slope
then
putU8 1
putEstimate x
else
putU8 0
putEstimate est.stdDev
def getEstimates : GetM Estimates := do
let mean ← getEstimate
let median ← getEstimate
let medianAbsDev ← getEstimate
let slope ← match (← getU8) with
| 1 => pure $ some (← getEstimate)
| _ => pure none
let stdDev ← getEstimate
return { mean, median, medianAbsDev, slope, stdDev }
instance : Serialize Estimates where
put := putEstimates
get := getEstimates
def putChangeEstimates (changeEst : ChangeEstimates) : PutM Unit := do
putEstimate changeEst.mean
putEstimate changeEst.median
def getChangeEstimates : GetM ChangeEstimates := do
let mean ← getEstimate
let median ← getEstimate
return { mean, median }
instance : Serialize ChangeEstimates where
put := putChangeEstimates
get := getChangeEstimates
def getOneShot: GetM OneShot := do
return { benchTime := (← Serialize.get) }
instance : Serialize OneShot where
put os := Serialize.put os.benchTime
get := getOneShot
/-- Writes JSON to disk at `benchPath/fileName` -/
def storeJson [Lean.ToJson α] (data : α) (benchPath : System.FilePath) : IO Unit := do
let json := Lean.toJson data
IO.FS.writeFile benchPath json.pretty
/-- Writes Ixon to disk at `benchPath/fileName` -/
def storeIxon [Serialize α] (data : α) (benchPath : System.FilePath) : IO Unit := do
let ixon := ser data
IO.FS.writeBinFile benchPath ixon
def storeFile [Lean.ToJson α] [Serialize α] (fmt : SerdeFormat) (data: α) (path : System.FilePath) : IO Unit := do
match fmt with
| .json => storeJson data path
| .ixon => storeIxon data path
def loadJson [Lean.FromJson α] (path : System.FilePath) : IO α := do
let jsonStr ← IO.FS.readFile path
let json ← match Lean.Json.parse jsonStr with
| .ok js => pure js
| .error e => throw $ IO.userError s!"{repr e}"
match Lean.fromJson? json with
| .ok d => pure d
| .error e => throw $ IO.userError s!"{repr e}"
def loadIxon [Serialize α] (path : System.FilePath) : IO α := do
let ixonBytes ← IO.FS.readBinFile path
match de ixonBytes with
| .ok d => pure d
| .error e => throw $ IO.userError s!"expected a, got {repr e}"
def loadFile [Lean.FromJson α] [Serialize α] (format : SerdeFormat) (path : System.FilePath) : IO α := do
match format with
| .json => loadJson path
| .ixon => loadIxon path
end