-
-
Notifications
You must be signed in to change notification settings - Fork 329
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
Optimize event serialization with pre-allocated buffer #2794
Changes from 4 commits
c016c7f
0b1483d
58ae8cc
d43487a
6846f9d
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 |
---|---|---|
|
@@ -75,6 +75,7 @@ where | |
should_serialize_cnt: usize, | ||
pub(crate) time_ref: Option<Handle<TimeObserver>>, | ||
phantom: PhantomData<S>, | ||
event_buffer: Vec<u8>, | ||
} | ||
|
||
impl LlmpEventManager<(), NopState<NopInput>, NopShMemProvider> { | ||
|
@@ -165,6 +166,7 @@ impl<EMH> LlmpEventManagerBuilder<EMH> { | |
time_ref, | ||
phantom: PhantomData, | ||
custom_buf_handlers: vec![], | ||
event_buffer: Vec::with_capacity(1024 * 4), | ||
}) | ||
} | ||
|
||
|
@@ -199,6 +201,7 @@ impl<EMH> LlmpEventManagerBuilder<EMH> { | |
time_ref, | ||
phantom: PhantomData, | ||
custom_buf_handlers: vec![], | ||
event_buffer: Vec::with_capacity(1024 * 4), | ||
}) | ||
} | ||
|
||
|
@@ -233,6 +236,7 @@ impl<EMH> LlmpEventManagerBuilder<EMH> { | |
time_ref, | ||
phantom: PhantomData, | ||
custom_buf_handlers: vec![], | ||
event_buffer: Vec::with_capacity(1024 * 4), | ||
}) | ||
} | ||
|
||
|
@@ -265,6 +269,7 @@ impl<EMH> LlmpEventManagerBuilder<EMH> { | |
time_ref, | ||
phantom: PhantomData, | ||
custom_buf_handlers: vec![], | ||
event_buffer: Vec::with_capacity(1024 * 4), | ||
}) | ||
} | ||
} | ||
|
@@ -409,6 +414,7 @@ where | |
+ EvaluatorObservers<E, Self, <S::Corpus as Corpus>::Input, S> | ||
+ Evaluator<E, Self, <S::Corpus as Corpus>::Input, S>, | ||
{ | ||
println!("Got event in client: {} from {:?}", event.name(), client_id); | ||
if !self.hooks.pre_exec_all(state, client_id, &event)? { | ||
return Ok(()); | ||
} | ||
|
@@ -512,44 +518,57 @@ where | |
true | ||
} | ||
} | ||
|
||
#[cfg(feature = "llmp_compression")] | ||
fn fire( | ||
&mut self, | ||
_state: &mut Self::State, | ||
event: Event<<Self::State as UsesInput>::Input>, | ||
) -> Result<(), Error> { | ||
let serialized = postcard::to_allocvec(&event)?; | ||
#[cfg(feature = "llmp_compression")] | ||
let flags = LLMP_FLAG_INITIALIZED; | ||
|
||
match self.compressor.maybe_compress(&serialized) { | ||
Some(comp_buf) => { | ||
self.llmp.send_buf_with_flags( | ||
LLMP_TAG_EVENT_TO_BOTH, | ||
flags | LLMP_FLAG_COMPRESSED, | ||
&comp_buf, | ||
)?; | ||
self.event_buffer.clear(); | ||
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. Why clear? |
||
self.event_buffer.resize(self.event_buffer.capacity(), 0); | ||
|
||
// Serialize the event, reallocating event_buffer if needed | ||
let written_len = match postcard::to_slice(&event, &mut self.event_buffer) { | ||
Ok(written) => written.len(), | ||
Err(postcard::Error::SerializeBufferFull) => { | ||
let serialized = postcard::to_allocvec(&event)?; | ||
self.event_buffer = serialized; | ||
self.event_buffer.len() | ||
} | ||
None => { | ||
self.llmp.send_buf(LLMP_TAG_EVENT_TO_BOTH, &serialized)?; | ||
Err(e) => return Err(Error::from(e)), | ||
}; | ||
|
||
#[cfg(feature = "llmp_compression")] | ||
{ | ||
match self | ||
.compressor | ||
.maybe_compress(&self.event_buffer[..written_len]) | ||
{ | ||
Some(comp_buf) => { | ||
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. technically this still allocates, so we have future room for improvements 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. Sorry, I don't understand what you mean exactly. Are you referring to the allocation happening in maybe_compress function? 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. Yes in theory this also could use a preallocated buffer |
||
self.llmp.send_buf_with_flags( | ||
LLMP_TAG_EVENT_TO_BOTH, | ||
flags | LLMP_FLAG_COMPRESSED, | ||
&comp_buf, | ||
)?; | ||
} | ||
None => { | ||
self.llmp | ||
.send_buf(LLMP_TAG_EVENT_TO_BOTH, &self.event_buffer[..written_len])?; | ||
} | ||
} | ||
} | ||
self.last_sent = current_time(); | ||
|
||
Ok(()) | ||
} | ||
#[cfg(not(feature = "llmp_compression"))] | ||
{ | ||
self.llmp | ||
.send_buf(LLMP_TAG_EVENT_TO_BOTH, &self.event_buffer[..written_len]); | ||
} | ||
|
||
#[cfg(not(feature = "llmp_compression"))] | ||
fn fire( | ||
&mut self, | ||
_state: &mut Self::State, | ||
event: Event<<Self::State as UsesInput>::Input>, | ||
) -> Result<(), Error> { | ||
let serialized = postcard::to_allocvec(&event)?; | ||
self.llmp.send_buf(LLMP_TAG_EVENT_TO_BOTH, &serialized)?; | ||
self.last_sent = current_time(); | ||
Ok(()) | ||
} | ||
|
||
fn serialize_observers<OT>(&mut self, observers: &OT) -> Result<Option<Vec<u8>>, Error> | ||
where | ||
OT: ObserversTuple<Self::Input, Self::State> + Serialize, | ||
|
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.
Can we make the initial size a const?