-
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
- Loading branch information
Showing
23 changed files
with
437 additions
and
183 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <numeric> | ||
#include <vector> | ||
|
||
#include "prometheus/detail/core_export.h" | ||
|
||
namespace prometheus { | ||
|
||
struct PROMETHEUS_CPP_CORE_EXPORT IOVector { | ||
bool empty() const { return data.empty() || !size(); } | ||
|
||
using ByteVector = std::vector<std::uint8_t>; | ||
|
||
std::size_t 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 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; | ||
} | ||
|
||
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 Serialize(const MetricFamily& family) const override; | ||
void Serialize(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
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.