Skip to content

Commit 9ae99d5

Browse files
committed
Remove the serialization machinery from progress logging, provide dynamic type information instead
1 parent 63db7e1 commit 9ae99d5

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

timely/examples/logging-send.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,23 @@ fn main() {
2222
);
2323

2424
// Register timely progress logging.
25+
// Less generally useful: intended for debugging advanced custom operators or timely
26+
// internals.
2527
worker.log_register().insert::<TimelyProgressEvent,_>("timely/progress", |_time, data|
26-
data.iter().for_each(|x| println!("PROGRESS: {:?}", x))
28+
data.iter().for_each(|x| {
29+
println!("PROGRESS: {:?}", x);
30+
let (_, _, ev) = x;
31+
print!("PROGRESS: TYPED MESSAGES: ");
32+
for (n, p, t, d) in ev.messages.iter() {
33+
print!("{:?}, ", (n, p, t.as_any().downcast_ref::<usize>(), d));
34+
}
35+
println!();
36+
print!("PROGRESS: TYPED INTERNAL: ");
37+
for (n, p, t, d) in ev.internal.iter() {
38+
print!("{:?}, ", (n, p, t.as_any().downcast_ref::<usize>(), d));
39+
}
40+
println!();
41+
})
2742
);
2843

2944
// create a new input, exchange data, and inspect its output

timely/src/logging.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,28 +72,32 @@ pub struct ChannelsEvent {
7272
pub target: (usize, usize),
7373
}
7474

75-
#[cfg(not(feature = "bincode"))]
76-
/// Encapsulates (de)serialization, Debug, Display, for dynamically typed timestamps in logs
77-
pub trait ProgressEventTimestamp: std::fmt::Debug + std::fmt::Display + std::any::Any {
78-
/// Encodes a typed reference into a binary buffer.
75+
/// Encapsulates Any and Debug for dynamically typed timestamps in logs
76+
pub trait ProgressEventTimestamp: std::fmt::Debug + std::any::Any {
77+
/// Upcasts this `ProgressEventTimestamp` to `Any`.
7978
///
80-
/// # Safety
79+
/// NOTE: This is required until https://github.com/rust-lang/rfcs/issues/2765 is fixed
80+
fn as_any(&self) -> &dyn std::any::Any;
81+
82+
/// Returns the name of the concrete type of this object.
83+
///
84+
/// # Note
8185
///
82-
/// This method is unsafe because it is unsafe to transmute typed allocations to binary.
83-
/// Furthermore, Rust currently indicates that it is undefined behavior to observe padding
84-
/// bytes, which will happen when we `memmcpy` structs which contain padding bytes.
85-
unsafe fn encode(&self, write: &mut dyn std::io::Write) -> std::io::Result<()>;
86+
/// This is intended for diagnostic use. The exact contents and format of the
87+
/// string returned are not specified, other than being a best-effort
88+
/// description of the type. For example, amongst the strings
89+
/// that `type_name::<Option<String>>()` might return are `"Option<String>"` and
90+
/// `"std::option::Option<std::string::String>"`.
91+
fn type_name(&self) -> &'static str;
8692
}
93+
impl<T: crate::Data + std::fmt::Debug + std::any::Any> ProgressEventTimestamp for T {
94+
fn as_any(&self) -> &dyn std::any::Any { self }
8795

88-
#[cfg(not(feature = "bincode"))]
89-
impl<T: crate::communication::Data + std::fmt::Debug + std::fmt::Display + std::any::Any> ProgressEventTimestamp for T {
90-
unsafe fn encode(&self, mut write: &mut dyn std::io::Write) -> std::io::Result<()> {
91-
abomonation::encode(self, &mut write)
92-
}
96+
fn type_name(&self) -> &'static str { std::any::type_name::<T>() }
9397
}
9498

9599
/// A vector of progress updates in logs
96-
pub trait ProgressEventTimestampVec: std::fmt::Debug + std::any::Any{
100+
pub trait ProgressEventTimestampVec: std::fmt::Debug + std::any::Any {
97101
/// Iterate over the contents of the vector
98102
fn iter<'a>(&'a self) -> Box<dyn Iterator<Item=(&'a usize, &'a usize, &'a dyn ProgressEventTimestamp, &'a i64)>+'a>;
99103
}

timely/src/progress/broadcast.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,6 @@ impl<T:Timestamp+Send> Progcaster<T> {
5959
changes.compact();
6060
if !changes.is_empty() {
6161

62-
// This logging is relatively more expensive than other logging, as we
63-
// have formatting and string allocations on the main path. We do have
64-
// local type information about the timestamp, and we could log *that*
65-
// explicitly, but the consumer would have to know what to look for and
66-
// interpret appropriately. That's a big ask, so let's start with this,
67-
// and as folks need more performant logging think about allowing users
68-
// to select the more efficient variant.
6962
self.progress_logging.as_ref().map(|l| {
7063

7164
// Pre-allocate enough space; we transfer ownership, so there is not
@@ -74,14 +67,13 @@ impl<T:Timestamp+Send> Progcaster<T> {
7467
let mut messages = Box::new(Vec::with_capacity(changes.len()));
7568
let mut internal = Box::new(Vec::with_capacity(changes.len()));
7669

77-
// TODO: Reconsider `String` type or perhaps re-use allocation.
7870
for ((location, time), diff) in changes.iter() {
7971
match location.port {
8072
Port::Target(port) => {
81-
messages.push((location.node, port, format!("{:?}", time), *diff))
73+
messages.push((location.node, port, time.clone(), *diff))
8274
},
8375
Port::Source(port) => {
84-
internal.push((location.node, port, format!("{:?}", time), *diff))
76+
internal.push((location.node, port, time.clone(), *diff))
8577
}
8678
}
8779
}
@@ -144,15 +136,14 @@ impl<T:Timestamp+Send> Progcaster<T> {
144136
let mut messages = Vec::with_capacity(changes.len());
145137
let mut internal = Vec::with_capacity(changes.len());
146138

147-
// TODO: Reconsider `String` type or perhaps re-use allocation.
148139
for ((location, time), diff) in recv_changes.iter() {
149140

150141
match location.port {
151142
Port::Target(port) => {
152-
messages.push((location.node, port, format!("{:?}", time), *diff))
143+
messages.push((location.node, port, time.clone(), *diff))
153144
},
154145
Port::Source(port) => {
155-
internal.push((location.node, port, format!("{:?}", time), *diff))
146+
internal.push((location.node, port, time.clone(), *diff))
156147
}
157148
}
158149
}

0 commit comments

Comments
 (0)