-
Notifications
You must be signed in to change notification settings - Fork 27.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Turbopack] improve performance of the trace server #71661
Changes from all commits
afaf21b
865e07a
7800611
463cbf0
c6767d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
use std::{ | ||
collections::{hash_map::Entry, HashMap, HashSet}, | ||
mem::transmute, | ||
ops::{Deref, DerefMut}, | ||
sync::Arc, | ||
}; | ||
|
||
|
@@ -294,8 +296,15 @@ impl TurbopackFormat { | |
} | ||
|
||
impl TraceFormat for TurbopackFormat { | ||
fn read(&mut self, mut buffer: &[u8]) -> Result<usize> { | ||
let mut rows = Vec::new(); | ||
type Reused = Vec<TraceRow<'static>>; | ||
|
||
fn read(&mut self, mut buffer: &[u8], reuse: &mut Self::Reused) -> Result<usize> { | ||
reuse.clear(); | ||
let mut reuse = ClearOnDrop(reuse); | ||
// Safety: The Vec is empty and is cleared on leaving this scope, so it's safe to cast the | ||
// lifetime of data, since there is no data and data can't leave this function. | ||
let rows = | ||
unsafe { transmute::<&mut Vec<TraceRow<'_>>, &mut Vec<TraceRow<'_>>>(&mut *reuse) }; | ||
let mut bytes_read = 0; | ||
loop { | ||
match postcard::take_from_bytes(buffer) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole "write rows to a vec" thing makes sense if one of the following is true:
Number 1 seems like it shouldn't matter because deserialization should be fast. Number 2 might matter, but we can probably assume that deserialization doesn't fail and panic if it does? If one of those two isn't true, you could call |
||
|
@@ -314,7 +323,7 @@ impl TraceFormat for TurbopackFormat { | |
} | ||
if !rows.is_empty() { | ||
let store = self.store.clone(); | ||
let mut iter = rows.into_iter(); | ||
let mut iter = rows.drain(..); | ||
{ | ||
let mut store = store.write(); | ||
for row in iter.by_ref() { | ||
|
@@ -327,3 +336,25 @@ impl TraceFormat for TurbopackFormat { | |
Ok(bytes_read) | ||
} | ||
} | ||
|
||
struct ClearOnDrop<'l, T>(&'l mut Vec<T>); | ||
|
||
impl<T> Drop for ClearOnDrop<'_, T> { | ||
fn drop(&mut self) { | ||
self.0.clear(); | ||
} | ||
} | ||
|
||
impl<T> Deref for ClearOnDrop<'_, T> { | ||
type Target = Vec<T>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.0 | ||
} | ||
} | ||
|
||
impl<T> DerefMut for ClearOnDrop<'_, T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
self.0 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://doc.rust-lang.org/std/fmt/#precision