Skip to content

Commit

Permalink
ipv4/6 benchmark against ada; #437
Browse files Browse the repository at this point in the history
  • Loading branch information
the-moisrex committed Oct 7, 2023
1 parent 66f71d8 commit 6ae0def
Show file tree
Hide file tree
Showing 8 changed files with 733 additions and 44 deletions.
1 change: 1 addition & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(FILE_SOURCES
bool_array/bool_array_benchmark.cpp
tokenizer/tokenizer_benchmark.cpp
ip_to_string/ip_to_string_benchmark.cpp
ada/ada_benchmark.cpp
)
file(GLOB FILE_PCH *_pch.hpp)

Expand Down
46 changes: 31 additions & 15 deletions benchmarks/ada/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,46 @@ going to benchmark our features against theirs.
GCC:

```
2023-10-07T07:33:24-08:00
2023-10-07T10:09:33-08:00
Running ./a.out
Run on (8 X 3176.74 MHz CPU s)
Run on (8 X 3301.2 MHz CPU s)
CPU Caches:
L1 Data 32 KiB (x4)
L1 Instruction 32 KiB (x4)
L2 Unified 256 KiB (x4)
L3 Unified 6144 KiB (x1)
Load Average: 2.37, 4.32, 4.17
----------------------------------------------------------
Benchmark Time CPU Iterations
----------------------------------------------------------
WebppSchemePort 10.6 ns 10.5 ns 70043092
AdaSchemePort 12.6 ns 12.5 ns 46552076
Load Average: 1.27, 1.13, 1.46
------------------------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------------------------
WebppSchemePort 9.45 ns 9.45 ns 73236889
AdaSchemePort 12.6 ns 12.6 ns 55849600
WebppV1SerializeIPv4 30.8 ns 30.8 ns 22750807
WebppV1OptimizedSerializeIPv4 22.0 ns 22.0 ns 31794079
WebppV2OptimizedSerializeIPv4 21.9 ns 21.9 ns 32114162
WebppSerializeIPv4 31.1 ns 31.1 ns 22445001
AdaSerializeIPv4 22.4 ns 22.4 ns 31388955
AdaSerializeIPv6 44.7 ns 44.6 ns 15783639
WebppSerializeIPv6 58.5 ns 58.5 ns 11833784
WebppV1SerializeIPv6Optimized 59.7 ns 59.6 ns 11853974
```

Clang:

```
2023-10-07T07:32:37-08:00
Load Average: 2.60, 4.65, 4.27
----------------------------------------------------------
Benchmark Time CPU Iterations
----------------------------------------------------------
WebppSchemePort 6.24 ns 6.23 ns 115267454
AdaSchemePort 8.74 ns 8.67 ns 83124377
2023-10-07T10:06:44-08:00
Load Average: 0.87, 1.10, 1.52
------------------------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------------------------
WebppSchemePort 5.68 ns 5.68 ns 122391310
AdaSchemePort 9.37 ns 9.35 ns 76592869
WebppV1SerializeIPv4 26.1 ns 26.1 ns 26753124
WebppV1OptimizedSerializeIPv4 19.1 ns 19.1 ns 36785492
WebppV2OptimizedSerializeIPv4 19.4 ns 19.4 ns 35955354
WebppSerializeIPv4 26.0 ns 26.0 ns 26772427
AdaSerializeIPv4 20.1 ns 20.0 ns 34847434
AdaSerializeIPv6 43.5 ns 43.4 ns 16218986
WebppSerializeIPv6 51.4 ns 51.3 ns 13591171
WebppV1SerializeIPv6Optimized 59.0 ns 58.9 ns 11885098
```
335 changes: 312 additions & 23 deletions benchmarks/ada/ada_benchmark.cpp

Large diffs are not rendered by default.

88 changes: 88 additions & 0 deletions benchmarks/ada/ada_ip_serializers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Created by moisrex on 10/7/23.

#ifndef WEBPP_BENCH_ADA_SERIALIZERS_HPP
#define WEBPP_BENCH_ADA_SERIALIZERS_HPP

#include <array>
#include <charconv>
#include <cstdint>
#include <string>

namespace ada::serializers {

void find_longest_sequence_of_ipv6_pieces(const std::array<uint16_t, 8>& address,
size_t& compress,
size_t& compress_length) noexcept {
for (size_t i = 0; i < 8; i++) {
if (address[i] == 0) {
size_t next = i + 1;
while (next != 8 && address[next] == 0)
++next;
const size_t count = next - i;
if (compress_length < count) {
compress_length = count;
compress = i;
if (next == 8)
break;
i = next;
}
}
}
}

std::string ipv6(const std::array<uint16_t, 8>& address) noexcept {
size_t compress_length = 0; // The length of a long sequence of zeros.
size_t compress = 0; // The start of a long sequence of zeros.
find_longest_sequence_of_ipv6_pieces(address, compress, compress_length);

if (compress_length <= 1) {
// Optimization opportunity: Find a faster way then snprintf for imploding
// and return here.
compress = compress_length = 8;
}

std::string output(4 * 8 + 7 + 2, '\0');
size_t piece_index = 0;
char* point = output.data();
char* point_end = output.data() + output.size();
*point++ = '[';
while (true) {
if (piece_index == compress) {
*point++ = ':';
// If we skip a value initially, we need to write '::', otherwise
// a single ':' will do since it follows a previous ':'.
if (piece_index == 0) {
*point++ = ':';
}
piece_index += compress_length;
if (piece_index == 8) {
break;
}
}
point = std::to_chars(point, point_end, address[piece_index], 16).ptr;
piece_index++;
if (piece_index == 8) {
break;
}
*point++ = ':';
}
*point++ = ']';
output.resize(point - output.data());
return output;
}

std::string ipv4(const uint64_t address) noexcept {
std::string output(15, '\0');
char* point = output.data();
char* point_end = output.data() + output.size();
point = std::to_chars(point, point_end, uint8_t(address >> 24)).ptr;
for (int i = 2; i >= 0; i--) {
*point++ = '.';
point = std::to_chars(point, point_end, uint8_t(address >> (i * 8))).ptr;
}
output.resize(point - output.data());
return output;
}

} // namespace ada::serializers
#endif // WEBPP_BENCH_ADA_SERIALIZERS_HPP
6 changes: 3 additions & 3 deletions benchmarks/ada/ada_scheme.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Created by moisrex on 10/7/23.
// source of this file: ada project / scheme-inl.h

#ifndef WEBPP_ADA_SCHEME_HPP
#define WEBPP_ADA_SCHEME_HPP
#ifndef WEBPP_BENCH_ADA_SCHEME_HPP
#define WEBPP_BENCH_ADA_SCHEME_HPP

#include <cstdint>
#include <string_view>
Expand Down Expand Up @@ -45,4 +45,4 @@ namespace ada::scheme {

} // namespace ada::scheme

#endif // WEBPP_ADA_SCHEME_HPP
#endif // WEBPP_BENCH_ADA_SCHEME_HPP
Loading

0 comments on commit 6ae0def

Please sign in to comment.