From 266bd003cab4532cf0b0bbf4ca899381bd9aa8ae Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Wed, 16 Aug 2023 21:04:42 -0700 Subject: [PATCH] Switch fx_ver_t::as_str from stringstream to append/to_string (#90652) Stop using std::stringstream to convert fx_ver_t to a string. It pulls in std::locale (and other things) in all the host binaries when we really just need a simple conversion of the numbers to a string. In some binaries, it was also the only usage of stringstream. On my local Windows builds, this was a 15 to 40 kB decrease in size for host entry point components. In theory, this should also be faster, although this is not a particularly perf-sensitive function (most usage is for when tracing is enabled). --- src/native/corehost/fxr/fx_ver.cpp | 31 ++++++-------------- src/native/corehost/fxr/fx_ver.h | 2 -- src/native/corehost/hostmisc/pal.unix.cpp | 1 - src/native/corehost/hostmisc/pal.windows.cpp | 1 - src/native/corehost/hostpolicy/version.cpp | 16 +++++----- 5 files changed, 18 insertions(+), 33 deletions(-) diff --git a/src/native/corehost/fxr/fx_ver.cpp b/src/native/corehost/fxr/fx_ver.cpp index 254f408effe9b..7a857cdff473b 100644 --- a/src/native/corehost/fxr/fx_ver.cpp +++ b/src/native/corehost/fxr/fx_ver.cpp @@ -70,31 +70,18 @@ bool fx_ver_t::operator >=(const fx_ver_t& b) const pal::string_t fx_ver_t::as_str() const { - pal::stringstream_t stream; - stream << m_major << _X(".") << m_minor << _X(".") << m_patch; + pal::string_t version = pal::to_string(m_major); + version += _X('.'); + version += pal::to_string(m_minor); + version += _X('.'); + version += pal::to_string(m_patch); if (!m_pre.empty()) - { - stream << m_pre; - } - if (!m_build.empty()) - { - stream << m_build; - } - return stream.str(); -} + version += m_pre; -pal::string_t fx_ver_t::prerelease_glob() const -{ - pal::stringstream_t stream; - stream << m_major << _X(".") << m_minor << _X(".") << m_patch << _X("-*"); - return stream.str(); -} + if (!m_build.empty()) + version += m_build; -pal::string_t fx_ver_t::patch_glob() const -{ - pal::stringstream_t stream; - stream << m_major << _X(".") << m_minor << _X(".*"); - return stream.str(); + return version; } static pal::string_t getId(const pal::string_t &ids, size_t idStart) diff --git a/src/native/corehost/fxr/fx_ver.h b/src/native/corehost/fxr/fx_ver.h index 5f5348386897b..29f010876e04a 100644 --- a/src/native/corehost/fxr/fx_ver.h +++ b/src/native/corehost/fxr/fx_ver.h @@ -26,8 +26,6 @@ struct fx_ver_t bool is_empty() const { return m_major == -1; } pal::string_t as_str() const; - pal::string_t prerelease_glob() const; - pal::string_t patch_glob() const; bool operator ==(const fx_ver_t& b) const; bool operator !=(const fx_ver_t& b) const; diff --git a/src/native/corehost/hostmisc/pal.unix.cpp b/src/native/corehost/hostmisc/pal.unix.cpp index 46ffaf951adfb..34520aefd7365 100644 --- a/src/native/corehost/hostmisc/pal.unix.cpp +++ b/src/native/corehost/hostmisc/pal.unix.cpp @@ -16,7 +16,6 @@ #include #include #include -#include #include #include "config.h" #include diff --git a/src/native/corehost/hostmisc/pal.windows.cpp b/src/native/corehost/hostmisc/pal.windows.cpp index 98e4efbed72f0..b11610492d321 100644 --- a/src/native/corehost/hostmisc/pal.windows.cpp +++ b/src/native/corehost/hostmisc/pal.windows.cpp @@ -7,7 +7,6 @@ #include "longfile.h" #include -#include #include #include diff --git a/src/native/corehost/hostpolicy/version.cpp b/src/native/corehost/hostpolicy/version.cpp index ea643605ac88f..c08316fe4b5a8 100644 --- a/src/native/corehost/hostpolicy/version.cpp +++ b/src/native/corehost/hostpolicy/version.cpp @@ -51,29 +51,31 @@ bool version_t::operator >=(const version_t& b) const pal::string_t version_t::as_str() const { - pal::stringstream_t stream; - + pal::string_t version; if (m_major >= 0) { - stream << m_major; + version += pal::to_string(m_major); if (m_minor >= 0) { - stream << _X(".") << m_minor; + version += _X('.'); + version += pal::to_string(m_minor); if (m_build >= 0) { - stream << _X(".") << m_build; + version += _X('.'); + version += pal::to_string(m_build); if (m_revision >= 0) { - stream << _X(".") << m_revision; + version += _X('.'); + version += pal::to_string(m_revision); } } } } - return stream.str(); + return version; } /*static*/ int version_t::compare(const version_t&a, const version_t& b)