Skip to content

Commit 239697a

Browse files
committed
fix(log): replace unit metadata with index for unit-* event
Reduce log size by using an integer index instead of full metadata. The index is assigned deterministically at initialization.
1 parent 5f839d3 commit 239697a

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/cargo/core/compiler/timings/mod.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::util::{CargoResult, GlobalContext};
1717

1818
use cargo_util::paths;
1919
use indexmap::IndexMap;
20+
use itertools::Itertools as _;
2021
use std::collections::HashMap;
2122
use std::io::BufWriter;
2223
use std::time::{Duration, Instant};
@@ -51,6 +52,11 @@ pub struct Timings<'gctx> {
5152
total_fresh: u32,
5253
/// Total number of dirty units.
5354
total_dirty: u32,
55+
/// A map from unit to index.
56+
///
57+
/// This for saving log size.
58+
/// Only the unit-started event needs to hold the entire unit information.
59+
unit_to_index: HashMap<Unit, u64>,
5460
/// Time tracking for each individual unit.
5561
unit_times: Vec<UnitTime>,
5662
/// Units that are in the process of being built.
@@ -152,6 +158,7 @@ impl<'gctx> Timings<'gctx> {
152158
profile: String::new(),
153159
total_fresh: 0,
154160
total_dirty: 0,
161+
unit_to_index: HashMap::new(),
155162
unit_times: Vec::new(),
156163
active: HashMap::new(),
157164
concurrency: Vec::new(),
@@ -185,6 +192,13 @@ impl<'gctx> Timings<'gctx> {
185192
None
186193
}
187194
};
195+
let unit_to_index = bcx
196+
.unit_graph
197+
.keys()
198+
.sorted()
199+
.enumerate()
200+
.map(|(i, unit)| (unit.clone(), i as u64))
201+
.collect();
188202

189203
Timings {
190204
gctx: bcx.gctx,
@@ -197,6 +211,7 @@ impl<'gctx> Timings<'gctx> {
197211
profile,
198212
total_fresh: 0,
199213
total_dirty: 0,
214+
unit_to_index,
200215
unit_times: Vec::new(),
201216
active: HashMap::new(),
202217
concurrency: Vec::new(),
@@ -244,6 +259,7 @@ impl<'gctx> Timings<'gctx> {
244259
package_id: unit_time.unit.pkg.package_id().to_spec(),
245260
target: (&unit_time.unit.target).into(),
246261
mode: unit_time.unit.mode,
262+
index: self.unit_to_index[&unit_time.unit],
247263
elapsed: start,
248264
});
249265
}
@@ -303,9 +319,7 @@ impl<'gctx> Timings<'gctx> {
303319
}
304320
if let Some(logger) = build_runner.bcx.logger {
305321
logger.log(LogMessage::UnitFinished {
306-
package_id: unit_time.unit.pkg.package_id().to_spec(),
307-
target: (&unit_time.unit.target).into(),
308-
mode: unit_time.unit.mode,
322+
index: self.unit_to_index[&unit_time.unit],
309323
duration: unit_time.duration,
310324
rmeta_time: unit_time.rmeta_time,
311325
sections: unit_time.sections.clone().into_iter().collect(),

src/cargo/util/log_message.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ pub enum LogMessage {
3333
package_id: PackageIdSpec,
3434
target: Target,
3535
mode: CompileMode,
36+
index: u64,
3637
elapsed: f64,
3738
},
3839
/// Emitted when a compilation unit finishes.
3940
UnitFinished {
40-
package_id: PackageIdSpec,
41-
target: Target,
42-
mode: CompileMode,
41+
index: u64,
4342
duration: f64,
4443
#[serde(skip_serializing_if = "Option::is_none")]
4544
rmeta_time: Option<f64>,

tests/testsuite/build_analysis.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ fn log_msg_timing_info() {
132132
},
133133
{
134134
"elapsed": "{...}",
135+
"index": 0,
135136
"mode": "check",
136137
"package_id": "path+[ROOTURL]/foo/bar#0.0.0",
137138
"reason": "unit-started",
@@ -144,19 +145,15 @@ fn log_msg_timing_info() {
144145
},
145146
{
146147
"duration": "{...}",
147-
"mode": "check",
148-
"package_id": "path+[ROOTURL]/foo/bar#0.0.0",
148+
"index": 0,
149149
"reason": "unit-finished",
150150
"rmeta_time": "{...}",
151151
"run_id": "[..]T[..]Z-[..]",
152-
"target": {
153-
"kind": "lib",
154-
"name": "bar"
155-
},
156152
"timestamp": "[..]T[..]Z"
157153
},
158154
{
159155
"elapsed": "{...}",
156+
"index": 1,
160157
"mode": "check",
161158
"package_id": "path+[ROOTURL]/foo#0.0.0",
162159
"reason": "unit-started",
@@ -169,15 +166,10 @@ fn log_msg_timing_info() {
169166
},
170167
{
171168
"duration": "{...}",
172-
"mode": "check",
173-
"package_id": "path+[ROOTURL]/foo#0.0.0",
169+
"index": 1,
174170
"reason": "unit-finished",
175171
"rmeta_time": "{...}",
176172
"run_id": "[..]T[..]Z-[..]",
177-
"target": {
178-
"kind": "lib",
179-
"name": "foo"
180-
},
181173
"timestamp": "[..]T[..]Z"
182174
}
183175
]

0 commit comments

Comments
 (0)