mirror of
https://github.com/zeek/zeek.git
synced 2025-10-16 13:38:19 +00:00
Conn/net_utils/fmt_mac: Avoid snprintf(), memcpy() and allocation
The fmt_mac() function returning a std::string means the resulting mac is copied at least once upon returning. Then, the Assign() in GetVal() taking a std::string internally allocates a new zeek::String which hits a malloc (no short-string optimization for zeek::String) and then also copies the content from the std::string into the malloced memory. Save a few cycles by directly using the allocated memory with the String instance. This change improves runtime for a SYN-only pcap with just base/protocols/conn loaded by some 1-2%.
This commit is contained in:
parent
c41977057a
commit
29f5b507b6
3 changed files with 87 additions and 16 deletions
12
src/Conn.cc
12
src/Conn.cc
|
@ -196,16 +196,20 @@ const RecordValPtr& Connection::GetVal() {
|
|||
const int l2_len = sizeof(orig_l2_addr);
|
||||
char null[l2_len]{};
|
||||
|
||||
if ( memcmp(&orig_l2_addr, &null, l2_len) != 0 )
|
||||
orig_endp->Assign(5, fmt_mac(orig_l2_addr, l2_len));
|
||||
if ( memcmp(&orig_l2_addr, &null, l2_len) != 0 ) {
|
||||
auto [mac_bytes, mac_len] = fmt_mac_bytes(orig_l2_addr, l2_len);
|
||||
orig_endp->Assign(5, new String(true, mac_bytes.release(), mac_len));
|
||||
}
|
||||
|
||||
auto resp_endp = make_intrusive<RecordVal>(id::endpoint);
|
||||
resp_endp->Assign(0, 0);
|
||||
resp_endp->Assign(1, 0);
|
||||
resp_endp->Assign(4, resp_flow_label);
|
||||
|
||||
if ( memcmp(&resp_l2_addr, &null, l2_len) != 0 )
|
||||
resp_endp->Assign(5, fmt_mac(resp_l2_addr, l2_len));
|
||||
if ( memcmp(&resp_l2_addr, &null, l2_len) != 0 ) {
|
||||
auto [mac_bytes, mac_len] = fmt_mac_bytes(resp_l2_addr, l2_len);
|
||||
resp_endp->Assign(5, new String(true, mac_bytes.release(), mac_len));
|
||||
}
|
||||
|
||||
conn_val->Assign(0, std::move(id_val));
|
||||
conn_val->Assign(1, std::move(orig_endp));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue