Rubensei / windivert-rust

Rust bindings and wrapper around WinDivert user library
GNU Lesser General Public License v3.0
42 stars 8 forks source link

Why do `recv/ex` functions take a buffer? #6

Closed AriFordsham closed 1 year ago

AriFordsham commented 1 year ago

Thank you for this package!

Why is it neccessary for the recv(_ex) functions to be supplied with a buffer? Why can't the library allocate this?

Rubensei commented 1 year ago

The main reason is the performance hit when you allocate on each recv(_ex).

Common uses for windivert usually don't need the data to live long and it's usually injected (send) or discarded before calling recv(_ex) again to get the next packet(s)

Even in cases where you might conditionally copy the data to another place, reusing the same buffer offers a better performance

Using a buffer as parameter allows the crate user to decide how and when to allocate

Rubensei commented 1 year ago

I'm closing this for now

Feel free to continue in discussions if you have any other question. I didn't know I had them disabled 😅