Using "ryu" library to format floats

This commit is contained in:
Alexey Milovidov 2020-01-06 06:22:11 +03:00
parent 5545ce90d1
commit 2d47572603
6 changed files with 29 additions and 22 deletions

3
.gitmodules vendored
View File

@ -134,3 +134,6 @@
[submodule "contrib/libc-headers"]
path = contrib/libc-headers
url = https://github.com/ClickHouse-Extras/libc-headers.git
[submodule "contrib/ryu"]
path = contrib/ryu
url = https://github.com/ulfjack/ryu.git

View File

@ -32,6 +32,8 @@ if (USE_INTERNAL_DOUBLE_CONVERSION_LIBRARY)
add_subdirectory (double-conversion-cmake)
endif ()
add_subdirectory (ryu-cmake)
if (USE_INTERNAL_CITYHASH_LIBRARY)
add_subdirectory (cityhash102)
endif ()

1
contrib/ryu vendored Submodule

@ -0,0 +1 @@
Subproject commit 447d6998bd3af17d675955186ab5b3cc3c585f00

View File

@ -0,0 +1,10 @@
SET(LIBRARY_DIR ${ClickHouse_SOURCE_DIR}/contrib/ryu)
add_library(ryu
${LIBRARY_DIR}/ryu/d2fixed.c
${LIBRARY_DIR}/ryu/d2s.c
${LIBRARY_DIR}/ryu/f2s.c
${LIBRARY_DIR}/ryu/generic_128.c
)
target_include_directories(ryu SYSTEM BEFORE PUBLIC "${LIBRARY_DIR}")

View File

@ -330,6 +330,7 @@ target_link_libraries (clickhouse_common_io
${LINK_LIBRARIES_ONLY_ON_X86_64}
PUBLIC
${DOUBLE_CONVERSION_LIBRARIES}
ryu
PUBLIC
${Poco_Net_LIBRARY}
${Poco_Util_LIBRARY}

View File

@ -27,8 +27,11 @@
#include <IO/DoubleConverter.h>
#include <IO/WriteBufferFromString.h>
#include <ryu/ryu.h>
#include <Formats/FormatSettings.h>
namespace DB
{
@ -115,20 +118,17 @@ inline void writeBoolText(bool x, WriteBuffer & buf)
}
template <typename T>
inline size_t writeFloatTextFastPath(T x, char * buffer, int len)
inline size_t writeFloatTextFastPath(T x, char * buffer)
{
using Converter = DoubleConverter<false>;
double_conversion::StringBuilder builder{buffer, len};
bool result = false;
int result = 0;
if constexpr (std::is_same_v<T, double>)
result = Converter::instance().ToShortest(x, &builder);
result = d2s_buffered_n(x, buffer);
else
result = Converter::instance().ToShortestSingle(x, &builder);
result = f2s_buffered_n(x, buffer);
if (!result)
if (result <= 0)
throw Exception("Cannot print floating point number", ErrorCodes::CANNOT_PRINT_FLOAT_OR_DOUBLE_NUMBER);
return builder.position();
return result;
}
template <typename T>
@ -139,23 +139,13 @@ inline void writeFloatText(T x, WriteBuffer & buf)
using Converter = DoubleConverter<false>;
if (likely(buf.available() >= Converter::MAX_REPRESENTATION_LENGTH))
{
buf.position() += writeFloatTextFastPath(x, buf.position(), Converter::MAX_REPRESENTATION_LENGTH);
buf.position() += writeFloatTextFastPath(x, buf.position());
return;
}
Converter::BufferType buffer;
double_conversion::StringBuilder builder{buffer, sizeof(buffer)};
bool result = false;
if constexpr (std::is_same_v<T, double>)
result = Converter::instance().ToShortest(x, &builder);
else
result = Converter::instance().ToShortestSingle(x, &builder);
if (!result)
throw Exception("Cannot print floating point number", ErrorCodes::CANNOT_PRINT_FLOAT_OR_DOUBLE_NUMBER);
buf.write(buffer, builder.position());
size_t result = writeFloatTextFastPath(x, buffer);
buf.write(buffer, result);
}