Skip to content
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

Merged
merged 5 commits into from
Dec 30, 2024

Conversation

mzfr
Copy link
Contributor

@mzfr mzfr commented Dec 28, 2024

Fixes #1082

I tested the changes with libpng fuzzer, and noticed quite a significant performance improvement, though I haven't done any formal benchmarking.

Current Implementation Issue:
The fuzzer panics after 4-5 minutes of execution with a "SerializeBuffer full" error. While increasing the initial buffer size could help, it might not be the most elegant solution(?)

Questions/Feedback Needed:

  1. Should I implement a fallback mechanism to use to_allocvec when the pre-allocated buffer is full?
  2. Any other way to handle the Buffer full issue?

mzfr added 2 commits December 28, 2024 13:05
- Added event_buffer field to LlmpEventManager
- Used to_slice instead of to_allocvec
- Pre-allocated buffer size is 4KB

Fixes AFLplusplus#1082
@tokatoka
Copy link
Member

The fuzzer panics after 4-5 minutes of execution with a "SerializeBuffer full" error. While increasing the initial buffer size could help, it might not be the most elegant solution(?)

Yeah it's difficult...
@domenukk any idea?

imo i don't think using pre-allocated buffer makes it significantly efficient, but on the other hand it makes the code unnecessarily complex.

@domenukk
Copy link
Member

In the error case, can't we just use to_allocvec, and replace the current event_buffer with the newly allocated vec?

@tokatoka
Copy link
Member

sounds good

mzfr added 2 commits December 29, 2024 19:36
Also combined the shared logic between compressed & uncompressed event
firing while keeping the same behavior
@@ -199,6 +201,7 @@ impl<EMH> LlmpEventManagerBuilder<EMH> {
time_ref,
phantom: PhantomData,
custom_buf_handlers: vec![],
event_buffer: Vec::with_capacity(1024 * 4),
Copy link
Member

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?

flags | LLMP_FLAG_COMPRESSED,
&comp_buf,
)?;
self.event_buffer.clear();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why clear?

.compressor
.maybe_compress(&self.event_buffer[..written_len])
{
Some(comp_buf) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically this still allocates, so we have future room for improvements

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes in theory this also could use a preallocated buffer

Also removed the unnecessary event_buffer.clear(), since we are already
resizing it
@domenukk domenukk merged commit 8cd069c into AFLplusplus:main Dec 30, 2024
37 of 103 checks passed
@domenukk
Copy link
Member

Thank you! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TODO replace to_allocvec with an incremental buffer in the LLMP manager
3 participants