[libcxx-commits] [PATCH] D101752: Speedup to_string for integers using zero-copy.

Roman Koshelev via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon May 3 04:27:11 PDT 2021


Roman-Koshelev created this revision.
Roman-Koshelev requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D101752

Files:
  libcxx/src/string.cpp


Index: libcxx/src/string.cpp
===================================================================
--- libcxx/src/string.cpp
+++ libcxx/src/string.cpp
@@ -422,8 +422,19 @@
 #endif
 }
 
-template <typename S, typename V>
-S i_to_string(const V v)
+template <typename V>
+std::string i_to_string(const V v)
+{
+    std::string buf;
+    buf.resize(buf.capacity());
+    const auto res = std::to_chars(buf.data(), buf.data() + buf.size(), v);
+    _LIBCPP_ASSERT(res.ec == errc(), "bufsize must be large enough to accomodate the value");
+    buf.resize(res.ptr - buf.data());
+    return buf;
+}
+
+template <typename V>
+wstring i_to_wstring(const V v)
 {
 //  numeric_limits::digits10 returns value less on 1 than desired for unsigned numbers.
 //  For example, for 1-byte unsigned value digits10 is 2 (999 can not be represented),
@@ -432,24 +443,24 @@
     char buf[bufsize];
     const auto res = to_chars(buf, buf + bufsize, v);
     _LIBCPP_ASSERT(res.ec == errc(), "bufsize must be large enough to accomodate the value");
-    return S(buf, res.ptr);
+    return wstring(buf, res.ptr);
 }
 
 }  // unnamed namespace
 
-string  to_string (int val)                { return i_to_string< string>(val); }
-string  to_string (long val)               { return i_to_string< string>(val); }
-string  to_string (long long val)          { return i_to_string< string>(val); }
-string  to_string (unsigned val)           { return i_to_string< string>(val); }
-string  to_string (unsigned long val)      { return i_to_string< string>(val); }
-string  to_string (unsigned long long val) { return i_to_string< string>(val); }
-
-wstring to_wstring(int val)                { return i_to_string<wstring>(val); }
-wstring to_wstring(long val)               { return i_to_string<wstring>(val); }
-wstring to_wstring(long long val)          { return i_to_string<wstring>(val); }
-wstring to_wstring(unsigned val)           { return i_to_string<wstring>(val); }
-wstring to_wstring(unsigned long val)      { return i_to_string<wstring>(val); }
-wstring to_wstring(unsigned long long val) { return i_to_string<wstring>(val); }
+string  to_string (int val)                { return i_to_string(val); }
+string  to_string (long val)               { return i_to_string(val); }
+string  to_string (long long val)          { return i_to_string(val); }
+string  to_string (unsigned val)           { return i_to_string(val); }
+string  to_string (unsigned long val)      { return i_to_string(val); }
+string  to_string (unsigned long long val) { return i_to_string(val); }
+
+wstring to_wstring(int val)                { return i_to_wstring(val); }
+wstring to_wstring(long val)               { return i_to_wstring(val); }
+wstring to_wstring(long long val)          { return i_to_wstring(val); }
+wstring to_wstring(unsigned val)           { return i_to_wstring(val); }
+wstring to_wstring(unsigned long val)      { return i_to_wstring(val); }
+wstring to_wstring(unsigned long long val) { return i_to_wstring(val); }
 
 
 string  to_string (float val)       { return as_string(snprintf,       initial_string< string>()(),   "%f", val); }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101752.342356.patch
Type: text/x-patch
Size: 3113 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210503/61f63b78/attachment-0001.bin>


More information about the libcxx-commits mailing list