-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: don't create temporary objects for serialization
Fix: #646
- Loading branch information
Showing
27 changed files
with
541 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "prometheus/detail/core_export.h" | ||
|
||
namespace prometheus { | ||
|
||
struct PROMETHEUS_CPP_CORE_EXPORT IOVector { | ||
using ByteVector = std::vector<std::uint8_t>; | ||
|
||
bool empty() const; | ||
|
||
std::size_t size() const; | ||
|
||
std::size_t copy(std::size_t offset, void* buffer, | ||
std::size_t bufferSize) const; | ||
|
||
void add(const std::string& str, std::size_t chunkSize); | ||
|
||
std::vector<ByteVector> data; | ||
}; | ||
|
||
} // namespace prometheus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,27 @@ | ||
#pragma once | ||
|
||
#include <iosfwd> | ||
#include <vector> | ||
#include <cstddef> | ||
|
||
#include "prometheus/detail/core_export.h" | ||
#include "prometheus/iovector.h" | ||
#include "prometheus/metric_family.h" | ||
#include "prometheus/serializer.h" | ||
|
||
namespace prometheus { | ||
|
||
class PROMETHEUS_CPP_CORE_EXPORT TextSerializer : public Serializer { | ||
public: | ||
using Serializer::Serialize; | ||
void Serialize(std::ostream& out, | ||
const std::vector<MetricFamily>& metrics) const override; | ||
TextSerializer(IOVector& ioVector); | ||
|
||
void SerializeHelp(const MetricFamily& family) const override; | ||
void SerializeMetrics(const MetricFamily& family, | ||
const ClientMetric& metric) const override; | ||
|
||
private: | ||
void Add(const std::ostringstream& stream) const; | ||
|
||
IOVector& ioVector_; | ||
static constexpr std::size_t chunkSize_ = 1 * 1024 * 1024; | ||
}; | ||
|
||
} // namespace prometheus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "prometheus/iovector.h" | ||
|
||
#include <algorithm> | ||
#include <numeric> | ||
|
||
namespace prometheus { | ||
|
||
bool IOVector::empty() const { return data.empty() || !size(); } | ||
|
||
std::size_t IOVector::size() const { | ||
return std::accumulate(begin(data), end(data), std::size_t{0}, | ||
[](std::size_t size, const ByteVector& chunk) { | ||
return size + chunk.size(); | ||
}); | ||
} | ||
|
||
std::size_t IOVector::copy(std::size_t offset, void* buffer, | ||
std::size_t bufferSize) const { | ||
std::size_t copied = 0; | ||
for (const auto& chunk : data) { | ||
if (offset >= chunk.size()) { | ||
offset -= chunk.size(); | ||
continue; | ||
} | ||
|
||
auto chunkSize = std::min(chunk.size() - offset, bufferSize - copied); | ||
std::copy_n(chunk.data() + offset, chunkSize, | ||
reinterpret_cast<std::uint8_t*>(buffer) + copied); | ||
copied += chunkSize; | ||
offset = 0; | ||
|
||
if (copied == bufferSize) { | ||
break; | ||
} | ||
} | ||
return copied; | ||
} | ||
|
||
void IOVector::add(const std::string& str, std::size_t chunkSize) { | ||
std::size_t size = str.size(); | ||
std::size_t offset = 0; | ||
|
||
while (size > 0U) { | ||
if (data.empty() || data.back().size() >= chunkSize) { | ||
data.emplace_back(); | ||
data.back().reserve(chunkSize); | ||
} | ||
auto&& chunk = data.back(); | ||
const auto toAdd = std::min(size, chunkSize - chunk.size()); | ||
chunk.insert(chunk.end(), str.data() + offset, str.data() + offset + toAdd); | ||
|
||
size -= toAdd; | ||
offset += toAdd; | ||
} | ||
} | ||
} // namespace prometheus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.