refactor: replace bincode with zerocopy

Replaces bincode with zerocopy for deserialization of network data, improving performance and reducing dependencies.
This commit is contained in:
Nils Andreas Svee committed 2026-02-21 18:44:43 +07:00
1 parent f3e7729f43
commit 5838b0b9ee
3 files changed
+31 -44

No files matched your search

Generated
+22 -33
View File
@@ -37,26 +37,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "bincode"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36eaf5d7b090263e8150820482d5d93cd964a81e4019913c972f4edcc6edb740"
dependencies = [
"bincode_derive",
"serde",
"unty",
]
[[package]]
name = "bincode_derive"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf95709a440f45e986983918d0e8a1f30a9b1df04918fc828670606804ac3c09"
dependencies = [
"virtue",
]
[[package]] [[package]]
name = "bindgen" name = "bindgen"
version = "0.58.1" version = "0.58.1"
@@ -786,7 +766,6 @@ name = "sqm-autorate-rust"
version = "0.2.0" version = "0.2.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bincode",
"icmp-socket", "icmp-socket",
"log", "log",
"neli", "neli",
@@ -795,6 +774,8 @@ dependencies = [
"rustix", "rustix",
"thiserror", "thiserror",
"time", "time",
"zerocopy",
"zerocopy-derive",
] ]
[[package]] [[package]]
@@ -907,12 +888,6 @@ version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"
[[package]]
name = "unty"
version = "0.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d49784317cd0d1ee7ec5c716dd598ec5b4483ea832a2dced265471cc0f690ae"
[[package]] [[package]]
name = "vec_map" name = "vec_map"
version = "0.8.2" version = "0.8.2"
@@ -925,12 +900,6 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "virtue"
version = "0.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "051eb1abcf10076295e815102942cc58f9d5e3b4560e46e53c21e8ff6f3af7b1"
[[package]] [[package]]
name = "wasip2" name = "wasip2"
version = "1.0.2+wasi-0.2.9" version = "1.0.2+wasi-0.2.9"
@@ -1200,6 +1169,26 @@ dependencies = [
"wasmparser", "wasmparser",
] ]
[[package]]
name = "zerocopy"
version = "0.8.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db6d35d663eadb6c932438e763b262fe1a70987f9ae936e60158176d710cae4a"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4122cd3169e94605190e77839c9a40d40ed048d305bfdc146e7df40ab0f3e517"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "zmij" name = "zmij"
version = "1.0.21" version = "1.0.21"
+2 -1
View File
@@ -20,13 +20,14 @@ strip = "symbols"
[dependencies] [dependencies]
anyhow = "1.0" anyhow = "1.0"
bincode = "2.0"
icmp-socket = "0.4.0" icmp-socket = "0.4.0"
neli = "0.7" neli = "0.7"
rand = "0.10" rand = "0.10"
rustix = { version = "1.1", features = ["fs", "thread", "time"] } rustix = { version = "1.1", features = ["fs", "thread", "time"] }
rust-uci = { version = "0.1", optional = true } rust-uci = { version = "0.1", optional = true }
thiserror = "2.0" thiserror = "2.0"
zerocopy = "0.8.39"
zerocopy-derive = "0.8.39"
[dependencies.log] [dependencies.log]
features = ["std"] features = ["std"]
+7 -10
View File
@@ -1,5 +1,3 @@
use bincode::error::DecodeError;
use bincode::BorrowDecode;
use neli::consts::nl::NlmF; use neli::consts::nl::NlmF;
use neli::consts::rtnl::{Arphrd, Iff, Ifla, RtAddrFamily, Rtm, Tca}; use neli::consts::rtnl::{Arphrd, Iff, Ifla, RtAddrFamily, Rtm, Tca};
use neli::consts::socket::NlFamily; use neli::consts::socket::NlFamily;
@@ -12,11 +10,13 @@ use neli::utils::Groups;
use std::io; use std::io;
use std::str::Utf8Error; use std::str::Utf8Error;
use thiserror::Error; use thiserror::Error;
use zerocopy::FromBytes;
use zerocopy_derive::{FromBytes, Immutable, KnownLayout};
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum NetlinkError { pub enum NetlinkError {
#[error("Couldn't deserialize to struct")] #[error("Couldn't deserialize to struct: {0}")]
Deserialization(#[from] Box<DecodeError>), Deserialization(String),
#[error("Couldn't find intreface `{0}`")] #[error("Couldn't find intreface `{0}`")]
InterfaceNotFound(String), InterfaceNotFound(String),
@@ -52,7 +52,7 @@ pub struct Qdisc {
parent: u32, parent: u32,
} }
#[derive(BorrowDecode, Copy, Clone, Default, Debug)] #[derive(Copy, Clone, Default, Debug, FromBytes, Immutable, KnownLayout)]
#[repr(C)] #[repr(C)]
pub struct RtnlLinkStats64 { pub struct RtnlLinkStats64 {
pub rx_packets: u64, pub rx_packets: u64,
@@ -172,11 +172,8 @@ impl Netlink {
for attr in p.rtattrs().iter() { for attr in p.rtattrs().iter() {
if attr.rta_type() == &Ifla::Stats64 { if attr.rta_type() == &Ifla::Stats64 {
let buf = attr.rta_payload().as_ref(); let buf = attr.rta_payload().as_ref();
let stats = RtnlLinkStats64::read_from_bytes(buf)
let stats: RtnlLinkStats64 = .map_err(|e| NetlinkError::Deserialization(e.to_string()))?;
bincode::borrow_decode_from_slice(buf, bincode::config::standard())
.map_err(|e| NetlinkError::Deserialization(Box::new(e)))?
.0;
return Ok(stats); return Ok(stats);
} }