Skip to content

Commit

Permalink
Switch fx_ver_t::as_str from stringstream to append/to_string (#90652)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
elinor-fung authored Aug 17, 2023
1 parent 92786fd commit 266bd00
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 33 deletions.
31 changes: 9 additions & 22 deletions src/native/corehost/fxr/fx_ver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions src/native/corehost/fxr/fx_ver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/native/corehost/hostmisc/pal.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <fcntl.h>
#include <fnmatch.h>
#include <ctime>
#include <locale>
#include <pwd.h>
#include "config.h"
#include <minipal/getexepath.h>
Expand Down
1 change: 0 additions & 1 deletion src/native/corehost/hostmisc/pal.windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "longfile.h"

#include <cassert>
#include <locale>
#include <ShlObj.h>
#include <ctime>

Expand Down
16 changes: 9 additions & 7 deletions src/native/corehost/hostpolicy/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 266bd00

Please sign in to comment.