Skip to content
Closed

fix #1196

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
409 changes: 409 additions & 0 deletions benchmark/0019.formatting/format_vs_fmt.cc

Large diffs are not rendered by default.

162 changes: 162 additions & 0 deletions benchmark/0020.teju_vs_dragonbox/teju_vs_dragonbox.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include <fast_io.h>
#include <fast_io_device.h>
#include <random>
#include <vector>
#include <limits>
#include <cstring>
#include <fast_io_driver/timer.h>
#include <fast_io_unit/floating/roundtrip.h>
#include <fast_io_unit/floating/punning.h>
#include <teju/float.h>
#include <teju/double.h>
#include <dragonbox/dragonbox.h>
#include <dragonbox/dragonbox_to_chars.h>

using namespace fast_io::io;

template <class T>
static std::vector<T> make_random_values(std::size_t n)
{
std::mt19937_64 eng{123456789u};
std::uniform_real_distribution<T> dist(std::numeric_limits<T>::denorm_min(), std::numeric_limits<T>::max());
std::vector<T> v;
v.reserve(n);
for (std::size_t i = 0; i < n; ++i)
{
v.push_back(dist(eng)); // strictly positive finite by construction
}
return v;
}

static void bench_float(std::size_t n)
{
auto values = make_random_values<float>(n);

if(!values.empty())
{
char fio_buf[128];
char dbx_buf[128];
char teju_buf[128];
auto const x0 = values.front();
char* fio_p = fast_io::pr_rsv_to_c_array(fio_buf, fast_io::mnp::scientific(x0));
char* dbx_p = jkj::dragonbox::to_chars(x0, dbx_buf);
char* teju_p = jkj::dragonbox::to_chars(x0, teju_buf);
fast_io::println(
"sample fast_io=", fast_io::mnp::strvw(fio_buf, fio_p),
" dragonbox=", fast_io::mnp::strvw(dbx_buf, dbx_p),
" teju=", fast_io::mnp::strvw(teju_buf, teju_p));
}

{
fast_io::timer t(u8"fastio_float");
std::uint64_t acc{};
for (auto const x : values)
{
// auto const [mantissa, exponent, sign] = fast_io::details::get_punned_result(x);
// (void)sign;
// auto const r = fast_io::details::dragonbox_impl<float>(mantissa, static_cast<::std::int_least32_t>(exponent));
// acc += static_cast<std::uint64_t>(r.m10) + static_cast<std::uint64_t>(r.e10);
char buf[128];
auto *p = fast_io::pr_rsv_to_c_array(buf, fast_io::mnp::scientific(x));
acc += static_cast<std::uint64_t>(p - buf);
}
std::uint64_t volatile sink = acc;
(void)sink;
}

{
using namespace jkj::dragonbox;
fast_io::timer t(u8"dragonbox_float");
std::uint64_t acc{};
for (auto const x : values)
{
char buf[128]{0};
auto *p = to_chars(x, buf);
acc += static_cast<std::uint64_t>(p - buf);
}
std::uint64_t volatile sink = acc;
(void)sink;
}

{
fast_io::timer t(u8"teju_float");
std::uint64_t acc{};
for (auto const x : values)
{
char buf[128]{0};
auto *p = jkj::dragonbox::to_chars(x, buf);
acc += static_cast<std::uint64_t>(p - buf);
}
std::uint64_t volatile sink = acc;
(void)sink;
}
}

static void bench_double(std::size_t n)
{
auto values = make_random_values<double>(n);

if(!values.empty())
{
char fio_buf[128];
char dbx_buf[128];
char teju_buf[128];
auto const x0 = values.front();
char* fio_p = fast_io::pr_rsv_to_c_array(fio_buf, fast_io::mnp::scientific(x0));
char* dbx_p = jkj::dragonbox::to_chars(x0, dbx_buf);
char* teju_p = jkj::dragonbox::to_chars(x0, teju_buf);
fast_io::println(
"sample fast_io=", fast_io::mnp::strvw(fio_buf, fio_p),
" dragonbox=", fast_io::mnp::strvw(dbx_buf, dbx_p),
" teju=", fast_io::mnp::strvw(teju_buf, teju_p));
}

{
// fast_io core (dragonbox_impl only, no string assembly)
fast_io::timer t(u8"fastio_double");
std::uint64_t acc{};
for (auto const x : values)
{
char buf[128]{0};
auto *p = fast_io::pr_rsv_to_c_array(buf, fast_io::mnp::scientific(x));
acc += static_cast<std::uint64_t>(p - buf);
}
std::uint64_t volatile sink = acc;
(void)sink;
}

{
using namespace jkj::dragonbox;
fast_io::timer t(u8"dragonbox_double");
std::uint64_t acc{};
for (auto const x : values)
{
char buf[128]{0};
auto *p = to_chars(x, buf);
acc += static_cast<std::uint64_t>(p - buf);
}
std::uint64_t volatile sink = acc;
(void)sink;
}

{
fast_io::timer t(u8"teju_double");
std::uint64_t acc{};
for (auto const x : values)
{
char buf[128]{0};
auto *p = jkj::dragonbox::to_chars(x, buf);
acc += static_cast<std::uint64_t>(p - buf);
}
std::uint64_t volatile sink = acc;
(void)sink;
}
}

int main()
{
constexpr std::size_t N = 1u << 20; // ~1M samples
bench_float(N);
print("\n");
bench_double(N);
}
49 changes: 49 additions & 0 deletions benchmark/0021.io/file_vs_stdio.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <fast_io.h>
#include <fast_io_device.h>
#include <fast_io_driver/tsc_timer.h>
#include <fast_io_driver/timer.h>
#include <vector>
#include <cstdio>
using namespace fast_io::io;

int main()
{
constexpr std::size_t N = 10'000'000;
// fast_io file write
{
fast_io::timer t(u8"fastio_file_write");
fast_io::obuf_file obf("io_bench.txt");
for (std::size_t i{}; i != N; ++i)
{
println(obf, i);
}
}
// stdio file write
{
fast_io::timer t(u8"stdio_file_write");
fast_io::c_file cf("io_bench_stdio.txt", fast_io::open_mode::out);
for (std::size_t i{}; i != N; ++i)
{
std::fprintf(cf.fp, "%zu\n", i);
}
}
std::vector<std::size_t> vec(N);
// fast_io file read
{
fast_io::timer t(u8"fastio_file_read");
fast_io::ibuf_file ibf("io_bench.txt");
for (std::size_t i{}; i != N; ++i)
{
scan(ibf, vec[i]);
}
}
// stdio file read
{
fast_io::timer t(u8"stdio_file_read");
fast_io::c_file cf("io_bench_stdio.txt", fast_io::open_mode::in);
for (std::size_t i{}; i != N; ++i)
{
std::fscanf(cf.fp, "%zu", &vec[i]);
}
}
}
6 changes: 3 additions & 3 deletions include/fast_io_crypto/tls/cipher_suite.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ rfc
*/

inline constexpr cipher_suite_type tls_aes_128_gcm_sha256{{::std::byte(0x13), ::std::byte(0x01)}};
inline constexpr cipher_suite_type tls_aes_256_gcm_sha256{{::std::byte(0x13), ::std::byte(0x02)}};
inline constexpr cipher_suite_type tls_aes_256_gcm_sha384{{::std::byte(0x13), ::std::byte(0x02)}};
inline constexpr cipher_suite_type tls_chacha20_poly1305_sha256{{::std::byte(0x13), ::std::byte(0x03)}};
inline constexpr cipher_suite_type tls_aes_128_ccm_sha256{{::std::byte(0x13), ::std::byte(0x04)}};
inline constexpr cipher_suite_type tls_aes_128_ccm_8_sha256{{::std::byte(0x13), ::std::byte(0x05)}};
Expand Down Expand Up @@ -214,9 +214,9 @@ inline constexpr void print_define(output &outp, cipher_suite_type const &e)
{
print_freestanding(outp, u8"TLS_AES_128_GCM_SHA256{0x13,0x01}");
}
else if (e == tls_aes_256_gcm_sha256)
else if (e == tls_aes_256_gcm_sha384)
{
print_freestanding(outp, u8"TLS_AES_256_GCM_SHA256{0x13,0x02}");
print_freestanding(outp, u8"TLS_AES_256_GCM_SHA384{0x13,0x02}");
}
else if (e == tls_chacha20_poly1305_sha256)
{
Expand Down
2 changes: 1 addition & 1 deletion include/fast_io_crypto/tls/client_hello.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct client_hello
::std::uint_least8_t session_id_length{32};
::fast_io::freestanding::array<::std::uint_least8_t, 32> session_id{};
::fast_io::freestanding::array<::std::uint_least8_t, 2> cipher_suite_length = {0x00, 0x02};
cipher_suite::cipher_suite_type cipher_suite{cipher_suite::tls_aes_256_gcm_sha256};
cipher_suite::cipher_suite_type cipher_suite{cipher_suite::tls_aes_256_gcm_sha384};
::std::uint_least8_t number_of_compression_length = {0x01};
::std::uint_least8_t compression_method = {};
};
Expand Down
4 changes: 0 additions & 4 deletions include/fast_io_dsal/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
#error "You must be using a C++ compiler"
#endif

#if !defined(__cplusplus)
#error "You must be using a C++ compiler"
#endif

#include "../fast_io_core.h"
#include "impl/misc/push_macros.h"
#include "impl/misc/push_warnings.h"
Expand Down
6 changes: 3 additions & 3 deletions include/fast_io_dsal/impl/index_span.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,11 @@ class index_span
#endif
inline constexpr span_type subspan(size_type pos, size_type count = ::fast_io::containers::npos) const noexcept
{
if (this->n < pos) [[unlikely]]
if (N < pos) [[unlikely]]
{
::fast_io::fast_terminate();
}
size_type const val{this->n - pos};
size_type const val{N - pos};
if (val < count)
{
if (count != ::fast_io::containers::npos) [[unlikely]]
Expand All @@ -295,7 +295,7 @@ class index_span
#endif
inline constexpr span_type subspan_unchecked(size_type pos, size_type count = ::fast_io::containers::npos) const noexcept
{
size_type const val{this->n - pos};
size_type const val{N - pos};
if (count == ::fast_io::containers::npos)
{
count = val;
Expand Down
6 changes: 6 additions & 0 deletions include/fast_io_dsal/impl/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,9 @@ class list

inline constexpr list(list &&other) noexcept
: imp(other.imp)
#if 0
, allochdl(std::move(other.allochdl))
#endif
{
auto prev = static_cast<::fast_io::containers::details::list_node_common *>(imp.prev);
auto next = static_cast<::fast_io::containers::details::list_node_common *>(imp.next);
Expand All @@ -921,6 +924,9 @@ class list
{
this->destroy();
imp = other.imp;
#if 0
allochdl = ::std::move(other.allochdl);
#endif
auto prev = static_cast<::fast_io::containers::details::list_node_common *>(imp.prev);
auto next = static_cast<::fast_io::containers::details::list_node_common *>(imp.next);
next->prev = prev->next = __builtin_addressof(imp);
Expand Down
Loading