diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2023-05-05 14:24:04 +0200 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2023-05-06 07:24:38 +0200 |
commit | 73ede24746ce7d06487ca34e20af436c2a119cbf (patch) | |
tree | 5598939948f6aae053e570c5716bb3433751692a /include/rtl | |
parent | 7dbc0e991f65b51f2033b8c8c60df055926d37b9 (diff) |
Optimize O(U)StringBuffer's append/insert taking float/double
rtl_(u)str_valueOf(Float/Double) are the most inefficient choice:
their implementations call rtl_math_doubleTo(U)String, asking to
allocate a new rtl_(u)String; then copy the string to the provided
buffer on stack, and destroy the rtl_(u)String; and then copy the
buffer into self. Even using O(U)String::number() would be more
efficient, avoiding the intermediate stack buffer copy.
But rtl_math_doubleTo(U)String can take also the O(U)StringBuffer's
data directly, allowing to insert efficiently. Use that.
Change-Id: I720ea7be6f4b2b92865a18eafcdb9ca9a089b854
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151377
Tested-by: Mike Kaganski <mike.kaganski@collabora.com>
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'include/rtl')
-rw-r--r-- | include/rtl/strbuf.hxx | 20 | ||||
-rw-r--r-- | include/rtl/ustrbuf.hxx | 20 |
2 files changed, 24 insertions, 16 deletions
diff --git a/include/rtl/strbuf.hxx b/include/rtl/strbuf.hxx index e5ae1ebd0a7b..cb842bb5839e 100644 --- a/include/rtl/strbuf.hxx +++ b/include/rtl/strbuf.hxx @@ -756,8 +756,7 @@ public: */ OStringBuffer & append(float f) { - char sz[RTL_STR_MAX_VALUEOFFLOAT]; - return append( sz, rtl_str_valueOfFloat( sz, f ) ); + return insert(getLength(), f); } /** @@ -773,8 +772,7 @@ public: */ OStringBuffer & append(double d) { - char sz[RTL_STR_MAX_VALUEOFDOUBLE]; - return append( sz, rtl_str_valueOfDouble( sz, d ) ); + return insert(getLength(), d); } /** @@ -1032,8 +1030,11 @@ public: */ OStringBuffer & insert(sal_Int32 offset, float f) { - char sz[RTL_STR_MAX_VALUEOFFLOAT]; - return insert( offset, sz, rtl_str_valueOfFloat( sz, f ) ); + // Same as rtl::str::valueOfFP, used for rtl_str_valueOfFloat + rtl_math_doubleToString(&pData, &nCapacity, offset, f, rtl_math_StringFormat_G, + RTL_STR_MAX_VALUEOFFLOAT - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.', + NULL, 0, true); + return *this; } /** @@ -1055,8 +1056,11 @@ public: */ OStringBuffer & insert(sal_Int32 offset, double d) { - char sz[RTL_STR_MAX_VALUEOFDOUBLE]; - return insert( offset, sz, rtl_str_valueOfDouble( sz, d ) ); + // Same as rtl::str::valueOfFP, used for rtl_str_valueOfDouble + rtl_math_doubleToString(&pData, &nCapacity, offset, d, rtl_math_StringFormat_G, + RTL_STR_MAX_VALUEOFDOUBLE - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.', + NULL, 0, true); + return *this; } /** diff --git a/include/rtl/ustrbuf.hxx b/include/rtl/ustrbuf.hxx index 363b4f24757f..227eed842443 100644 --- a/include/rtl/ustrbuf.hxx +++ b/include/rtl/ustrbuf.hxx @@ -916,8 +916,7 @@ public: */ OUStringBuffer & append(float f) { - sal_Unicode sz[RTL_USTR_MAX_VALUEOFFLOAT]; - return append( sz, rtl_ustr_valueOfFloat( sz, f ) ); + return insert(getLength(), f); } /** @@ -933,8 +932,7 @@ public: */ OUStringBuffer & append(double d) { - sal_Unicode sz[RTL_USTR_MAX_VALUEOFDOUBLE]; - return append( sz, rtl_ustr_valueOfDouble( sz, d ) ); + return insert(getLength(), d); } /** @@ -1288,8 +1286,11 @@ public: */ OUStringBuffer & insert(sal_Int32 offset, float f) { - sal_Unicode sz[RTL_USTR_MAX_VALUEOFFLOAT]; - return insert( offset, sz, rtl_ustr_valueOfFloat( sz, f ) ); + // Same as rtl::str::valueOfFP, used for rtl_ustr_valueOfFloat + rtl_math_doubleToUString(&pData, &nCapacity, offset, f, rtl_math_StringFormat_G, + RTL_USTR_MAX_VALUEOFFLOAT - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.', + NULL, 0, true); + return *this; } /** @@ -1312,8 +1313,11 @@ public: */ OUStringBuffer & insert(sal_Int32 offset, double d) { - sal_Unicode sz[RTL_USTR_MAX_VALUEOFDOUBLE]; - return insert( offset, sz, rtl_ustr_valueOfDouble( sz, d ) ); + // Same as rtl::str::valueOfFP, used for rtl_ustr_valueOfDouble + rtl_math_doubleToUString(&pData, &nCapacity, offset, d, rtl_math_StringFormat_G, + RTL_USTR_MAX_VALUEOFDOUBLE - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.', + NULL, 0, true); + return *this; } /** |