Skip to content

Commit 933caf6

Browse files
Use local buffer
1 parent 2b9cc26 commit 933caf6

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

include/cpp-statsd-client/StatsdClient.hpp

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <cstdio>
77
#include <iomanip>
88
#include <memory>
9-
#include <mutex>
109
#include <random>
1110
#include <sstream>
1211
#include <string>
@@ -163,12 +162,6 @@ class StatsdClient {
163162
//! The random number generator for handling sampling
164163
mutable std::mt19937 m_randomEngine;
165164

166-
//! The buffer string format our stats before sending them
167-
mutable std::string m_buffer;
168-
169-
//! The mutex to lock m_buffer
170-
mutable std::mutex m_buffer_mutex;
171-
172165
//! Fixed floating point precision of gauges
173166
int m_gaugePrecision;
174167
};
@@ -200,8 +193,6 @@ inline StatsdClient::StatsdClient(const std::string& host,
200193
m_gaugePrecision(gaugePrecision) {
201194
// Initialize the random generator to be used for sampling
202195
seed();
203-
// Avoid re-allocations by reserving a generous buffer
204-
m_buffer.reserve(256);
205196
}
206197

207198
inline void StatsdClient::setConfig(const std::string& host,
@@ -295,36 +286,36 @@ inline void StatsdClient::send(const std::string& key,
295286
std::stringstream valueStream;
296287
valueStream << std::fixed << std::setprecision(m_gaugePrecision) << value;
297288

298-
std::lock_guard<std::mutex> buffer_lock(m_buffer_mutex);
299-
m_buffer.clear();
289+
std::string buffer;
290+
buffer.reserve(256);
300291

301-
m_buffer.append(m_prefix);
292+
buffer.append(m_prefix);
302293
if (!m_prefix.empty() && !key.empty()) {
303-
m_buffer.push_back('.');
294+
buffer.push_back('.');
304295
}
305296

306-
m_buffer.append(key);
307-
m_buffer.push_back(':');
308-
m_buffer.append(valueStream.str());
309-
m_buffer.push_back('|');
310-
m_buffer.append(type);
297+
buffer.append(key);
298+
buffer.push_back(':');
299+
buffer.append(valueStream.str());
300+
buffer.push_back('|');
301+
buffer.append(type);
311302

312303
if (frequency < 1.f) {
313-
m_buffer.append("|@0.");
314-
m_buffer.append(std::to_string(static_cast<int>(frequency * 100)));
304+
buffer.append("|@0.");
305+
buffer.append(std::to_string(static_cast<int>(frequency * 100)));
315306
}
316307

317308
if (!tags.empty()) {
318-
m_buffer.append("|#");
309+
buffer.append("|#");
319310
for (const auto& tag : tags) {
320-
m_buffer.append(tag);
321-
m_buffer.push_back(',');
311+
buffer.append(tag);
312+
buffer.push_back(',');
322313
}
323-
m_buffer.pop_back();
314+
buffer.pop_back();
324315
}
325316

326317
// Send the message via the UDP sender
327-
m_sender->send(m_buffer);
318+
m_sender->send(buffer);
328319
}
329320

330321
inline void StatsdClient::seed(unsigned int seed) noexcept {

0 commit comments

Comments
 (0)