Skip to content

Commit

Permalink
Use std::string_view to avoid one copy / log message
Browse files Browse the repository at this point in the history
  • Loading branch information
flomnes committed Mar 20, 2023
1 parent c05195a commit 3dd1f1f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
15 changes: 13 additions & 2 deletions src/ext/yuni/src/yuni/core/string/traits/append.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "../../traits/length.h"
#include "integer.h"
#include <stdio.h>
#include <string_view>
#include <cassert>

#ifdef YUNI_OS_MSVC
Expand Down Expand Up @@ -143,6 +144,17 @@ class Append<CStringT, wchar_t[N]> final
}
};

// std::string_view
template<class CStringT>
class Append<CStringT, std::string_view> final
{
public:
static void Perform(CStringT& string, std::string_view rhs)
{
string.append(rhs, rhs.length());
}
};

// void*
template<class CStringT>
class Append<CStringT, void*> final
Expand Down Expand Up @@ -272,7 +284,7 @@ class Append<CStringT, std::vector<T>> final
}
};

// std::vector<>
// std::list<>
template<class CStringT, class T>
class Append<CStringT, std::list<T>> final
{
Expand All @@ -293,7 +305,6 @@ class Append<CStringT, std::list<T>> final
s += ']';
}
};

} // namespace CString
} // namespace Extension
} // namespace Yuni
Expand Down
18 changes: 18 additions & 0 deletions src/ext/yuni/src/yuni/core/traits/extension/into-cstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ class IntoCString<char[N]> final
}
};

template<>
class IntoCString<std::string_view> final
{
public:
enum
{
valid = 1,
converted = 0,
zeroTerminated = 0,
};

public:
static const char* Perform(std::string_view v)
{
return v.data();
}
};

template<uint ChunkSizeT, bool ExpandableT>
class IntoCString<Yuni::CString<ChunkSizeT, ExpandableT>> final
{
Expand Down
11 changes: 7 additions & 4 deletions src/solver/log-sink/log_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/

#include <antares/logs.h>
#include <string_view>
#include "log_sink.h"

namespace Antares::Solver
Expand All @@ -38,10 +39,12 @@ void LogSink::send(LogSeverity severity,
const char* message,
size_t message_len)
{
// message *is not* '\0'-terminated
// We could use std::string_view to avoid copies,
// but Yuni::Logs::Logger doesn't support them at the moment.
const std::string msg(message, message_len);
/* NOTES
1. Our logger handles filenames, timestamps, etc. We don't need those.
2. Message *is not* '\0'-terminated, we must use it's length.
Also, we use std::string_view to avoid copies
*/
const std::string_view msg(message, message_len);
switch (severity)
{
case google::GLOG_INFO:
Expand Down

0 comments on commit 3dd1f1f

Please sign in to comment.