Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .claude/settings.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"permissions": {
"allow": [
"Bash(cargo test)",
"Bash(cargo test:*)",
"Bash(cargo clippy:*)",
"Bash(cargo fmt:*)",
"Bash(cargo:*)",
"Bash(gh pr view:*)"
],
"deny": []
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Cargo.lock
examples/debug-file.rs

.env
.claude
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ All notable changes to this project will be documented in this file.

### Code improvements

* optimized BytesMut buffer allocation patterns for better memory efficiency
* replaced `BytesMut::with_capacity() + resize()` with `BytesMut::zeroed()` for cleaner initialization
* added capacity pre-allocation in ASN encoding to avoid reallocations
* replaced unnecessary BytesMut allocations with direct slice references in MRT header parsing
* added capacity pre-allocation in MRT record encoding for optimal buffer sizing
* fixed non-canonical PartialOrd implementation for ASN type to follow Rust idioms
* added BGP Flow-Spec parsing support following RFC 8955 and RFC 8956
* implemented complete Flow-Spec NLRI parsing for IPv4 and IPv6 Flow Specification rules
* added support for all Flow-Spec component types (destination/source prefix, protocol, ports, ICMP, TCP flags, packet length, DSCP, fragment, flow label)
Expand Down
4 changes: 2 additions & 2 deletions src/models/network/asn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl PartialEq for Asn {

impl PartialOrd for Asn {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.asn.cmp(&other.asn))
Some(self.cmp(other))
}
}

Expand Down Expand Up @@ -278,7 +278,7 @@ impl FromStr for Asn {
#[cfg(feature = "parser")]
impl Asn {
pub fn encode(&self) -> Bytes {
let mut bytes = BytesMut::new();
let mut bytes = BytesMut::with_capacity(if self.four_byte { 4 } else { 2 });
match self.four_byte {
true => bytes.put_u32(self.asn),
false => bytes.put_u16(self.asn as u16),
Expand Down
4 changes: 2 additions & 2 deletions src/parser/mrt/mrt_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use std::io::Read;
pub fn parse_common_header<T: Read>(input: &mut T) -> Result<CommonHeader, ParserError> {
let mut raw_bytes = [0u8; 12];
input.read_exact(&mut raw_bytes)?;
let mut data = BytesMut::from(&raw_bytes[..]);
let mut data = &raw_bytes[..];

let timestamp = data.get_u32();
let entry_type_raw = data.get_u16();
Expand All @@ -56,7 +56,7 @@ pub fn parse_common_header<T: Read>(input: &mut T) -> Result<CommonHeader, Parse
length -= 4;
let mut raw_bytes: [u8; 4] = [0; 4];
input.read_exact(&mut raw_bytes)?;
Some(BytesMut::from(&raw_bytes[..]).get_u32())
Some((&raw_bytes[..]).get_u32())
}
_ => None,
};
Expand Down
5 changes: 2 additions & 3 deletions src/parser/mrt/mrt_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ pub fn parse_mrt_record(input: &mut impl Read) -> Result<MrtRecord, ParserErrorW
};

// read the whole message bytes to buffer
let mut buffer = BytesMut::with_capacity(common_header.length as usize);
buffer.resize(common_header.length as usize, 0);
let mut buffer = BytesMut::zeroed(common_header.length as usize);
match input
.take(common_header.length as u64)
.read_exact(&mut buffer)
Expand Down Expand Up @@ -127,7 +126,6 @@ pub fn parse_mrt_body(

impl MrtRecord {
pub fn encode(&self) -> Bytes {
let mut bytes = BytesMut::new();
let message_bytes = self.message.encode(self.common_header.entry_subtype);
let mut new_header = self.common_header;
if message_bytes.len() < new_header.length as usize {
Expand All @@ -146,6 +144,7 @@ impl MrtRecord {
// assert!(self.message == parsed_body);
// // debug ends

let mut bytes = BytesMut::with_capacity(header_bytes.len() + message_bytes.len());
bytes.put_slice(&header_bytes);
bytes.put_slice(&message_bytes);
bytes.freeze()
Expand Down
Loading