Skip to content

Commit 58279cf

Browse files
authored
Some Puffin http cleanup (#236)
### Checklist * [X] I have read the [Contributor Guide](../CONTRIBUTING.md) * [X] I have read and agree to the [Code of Conduct](../CODE_OF_CONDUCT.md) * [X] I have added a description of my changes and why I'd like them included in the section below ### Description of Changes Some small cleanup of scopes collection management in serialization and `puffin_http`. - replace `FrameView` var by `ScopeCollection` in `PuffinServerImpl`, the other members of FrameView wasn't used. - use an Option<&ScopeCollection> as parameter of FrameData.write_into instead of `&ScopeCollection` + a boolean. I think this changes make the code easier to understand.
1 parent 4002687 commit 58279cf

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

puffin/src/frame_data.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,7 @@ impl FrameData {
568568
#[cfg(feature = "serialization")]
569569
pub fn write_into(
570570
&self,
571-
scope_collection: &crate::ScopeCollection,
572-
send_all_scopes: bool,
571+
scope_collection: Option<&crate::ScopeCollection>,
573572
write: &mut impl std::io::Write,
574573
) -> anyhow::Result<()> {
575574
use bincode::Options as _;
@@ -589,7 +588,7 @@ impl FrameData {
589588
write.write_u8(packed_streams.compression_kind as u8)?;
590589
write.write_all(&packed_streams.bytes)?;
591590

592-
let to_serialize_scopes: Vec<_> = if send_all_scopes {
591+
let to_serialize_scopes: Vec<_> = if let Some(scope_collection) = scope_collection {
593592
scope_collection.scopes_by_id().values().cloned().collect()
594593
} else {
595594
self.scope_delta.clone()

puffin/src/profile_view.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct FrameView {
2626
/// Maintain stats as we add/remove frames
2727
stats: FrameStats,
2828

29+
/// Collect all scope infos(id/name) from the start of the profiling.
2930
scope_collection: ScopeCollection,
3031
}
3132

@@ -229,7 +230,7 @@ impl FrameView {
229230
write.write_all(b"PUF0")?;
230231

231232
for frame in self.all_uniq() {
232-
frame.write_into(&self.scope_collection, false, write)?;
233+
frame.write_into(None, write)?;
233234
}
234235
Ok(())
235236
}

puffin_http/src/server.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::Context as _;
2-
use puffin::{FrameSinkId, FrameView, GlobalProfiler};
2+
use puffin::{FrameSinkId, GlobalProfiler, ScopeCollection};
33
use std::{
44
io::Write,
55
net::{SocketAddr, TcpListener, TcpStream},
@@ -249,11 +249,10 @@ impl Server {
249249
clients: Default::default(),
250250
num_clients: num_clients_cloned,
251251
send_all_scopes: false,
252-
frame_view: Default::default(),
252+
scope_collection: Default::default(),
253253
};
254254

255255
while let Ok(frame) = rx.recv() {
256-
server_impl.frame_view.add_frame(frame.clone());
257256
if let Err(err) = server_impl.accept_new_clients() {
258257
log::warn!("puffin server failure: {}", err);
259258
}
@@ -325,7 +324,7 @@ struct PuffinServerImpl {
325324
clients: Vec<Client>,
326325
num_clients: Arc<AtomicUsize>,
327326
send_all_scopes: bool,
328-
frame_view: FrameView,
327+
scope_collection: ScopeCollection,
329328
}
330329

331330
impl PuffinServerImpl {
@@ -372,18 +371,25 @@ impl PuffinServerImpl {
372371
}
373372
puffin::profile_function!();
374373

374+
// Keep scope_collection up-to-date
375+
frame.scope_delta.iter().for_each(|new_scope| {
376+
self.scope_collection.insert(new_scope.clone());
377+
});
378+
375379
let mut packet = vec![];
376380

377381
packet
378382
.write_all(&crate::PROTOCOL_VERSION.to_le_bytes())
379383
.unwrap();
380384

385+
let scope_collection = if self.send_all_scopes {
386+
Some(&self.scope_collection)
387+
} else {
388+
None
389+
};
390+
381391
frame
382-
.write_into(
383-
self.frame_view.scope_collection(),
384-
self.send_all_scopes,
385-
&mut packet,
386-
)
392+
.write_into(scope_collection, &mut packet)
387393
.context("Encode puffin frame")?;
388394
self.send_all_scopes = false;
389395

0 commit comments

Comments
 (0)