Merge pull request #5226 from apple/anoyes/assert-literal-string

Static assert that LiteralStringRef arg is literal string
This commit is contained in:
Andrew Noyes 2021-07-19 17:00:30 -07:00 committed by GitHub
commit 059ff9b9ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 15 deletions

View File

@ -1401,25 +1401,22 @@ struct RedwoodMetrics {
metrics = {};
if (!buildFillPctSketch.isValid() ||
buildFillPctSketch->name() != ("buildFillPct:" + std::to_string(levelCounter))) {
buildFillPctSketch = Histogram::getHistogram(LiteralStringRef("buildFillPct"),
LiteralStringRef(std::to_string(levelCounter).c_str()),
Histogram::Unit::percentage);
modifyFillPctSketch = Histogram::getHistogram(LiteralStringRef("modifyFillPct"),
LiteralStringRef(std::to_string(levelCounter).c_str()),
Histogram::Unit::percentage);
buildStoredPctSketch = Histogram::getHistogram(LiteralStringRef("buildStoredPct"),
LiteralStringRef(std::to_string(levelCounter).c_str()),
Histogram::Unit::percentage);
modifyStoredPctSketch = Histogram::getHistogram(LiteralStringRef("modifyStoredPct"),
LiteralStringRef(std::to_string(levelCounter).c_str()),
Histogram::Unit::percentage);
std::string levelCounterStr = std::to_string(levelCounter);
buildFillPctSketch = Histogram::getHistogram(
LiteralStringRef("buildFillPct"), StringRef(levelCounterStr), Histogram::Unit::percentage);
modifyFillPctSketch = Histogram::getHistogram(
LiteralStringRef("modifyFillPct"), StringRef(levelCounterStr), Histogram::Unit::percentage);
buildStoredPctSketch = Histogram::getHistogram(
LiteralStringRef("buildStoredPct"), StringRef(levelCounterStr), Histogram::Unit::percentage);
modifyStoredPctSketch = Histogram::getHistogram(
LiteralStringRef("modifyStoredPct"), StringRef(levelCounterStr), Histogram::Unit::percentage);
buildItemCountSketch = Histogram::getHistogram(LiteralStringRef("buildItemCount"),
LiteralStringRef(std::to_string(levelCounter).c_str()),
StringRef(levelCounterStr),
Histogram::Unit::count,
0,
maxRecordCount);
modifyItemCountSketch = Histogram::getHistogram(LiteralStringRef("modifyItemCount"),
LiteralStringRef(std::to_string(levelCounter).c_str()),
StringRef(levelCounterStr),
Histogram::Unit::count,
0,
maxRecordCount);

View File

@ -662,7 +662,15 @@ struct Traceable<Standalone<T>> : std::conditional<Traceable<T>::value, std::tru
static std::string toString(const Standalone<T>& value) { return Traceable<T>::toString(value); }
};
#define LiteralStringRef(str) StringRef((const uint8_t*)(str), sizeof((str)) - 1)
namespace literal_string_ref {
template <class T, int Size>
StringRef LiteralStringRefHelper(const char* str) {
static_assert(std::is_same_v<T, const char(&)[Size]>, "Argument to LiteralStringRef must be a literal string");
return StringRef(reinterpret_cast<const uint8_t*>(str), Size - 1);
}
} // namespace literal_string_ref
#define LiteralStringRef(str) literal_string_ref::LiteralStringRefHelper<decltype(str), sizeof(str)>(str)
inline StringRef operator"" _sr(const char* str, size_t size) {
return StringRef(reinterpret_cast<const uint8_t*>(str), size);
}