perf: improve IPv4 `tr_address` comparison (#5651)

This commit is contained in:
tearfur 2023-06-27 10:17:32 +08:00 committed by GitHub
parent 26416f340f
commit 70decc1d9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 1 deletions

View File

@ -597,7 +597,9 @@ int tr_address::compare(tr_address const& that) const noexcept // <=>
return this->is_ipv4() ? 1 : -1; return this->is_ipv4() ? 1 : -1;
} }
return this->is_ipv4() ? memcmp(&this->addr.addr4, &that.addr.addr4, sizeof(this->addr.addr4)) : // in_addr_t is by definition uint32_t, so we convert it to int64_t,
// the smallest signed integer type that can contain all values of uint32_t.
return this->is_ipv4() ? static_cast<int>(int64_t{ ntohl(this->addr.addr4.s_addr) } - ntohl(that.addr.addr4.s_addr)) :
memcmp(&this->addr.addr6.s6_addr, &that.addr.addr6.s6_addr, sizeof(this->addr.addr6.s6_addr)); memcmp(&this->addr.addr6.s6_addr, &that.addr.addr6.s6_addr, sizeof(this->addr.addr6.s6_addr));
} }