Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.

Commit 2c39cc2

Browse files
dqminhyurishkuro
authored andcommitted
Reuse TMemoryBuffer when calculating size of ThriftType (#185)
Previous approach allocates several max size buffer everytime we want to calculate the size of the serialized thrift type. This changes to reuse only 1 buffer per sender. As sender is single-threaded this should be safe. Signed-off-by: Daniel Dao <[email protected]>
1 parent 5fc9c65 commit 2c39cc2

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/jaegertracing/ThriftSender.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,6 @@ namespace {
3939

4040
constexpr auto kEmitBatchOverhead = 30;
4141

42-
template <typename ThriftType>
43-
int calcSizeOfSerializedThrift(apache::thrift::protocol::TProtocolFactory& factory, const ThriftType& base, int maxPacketSize)
44-
{
45-
std::shared_ptr<apache::thrift::transport::TMemoryBuffer> buffer(
46-
new apache::thrift::transport::TMemoryBuffer(maxPacketSize));
47-
auto protocol = factory.getProtocol(buffer);
48-
base.write(protocol.get());
49-
uint8_t* data = nullptr;
50-
uint32_t size = 0;
51-
buffer->getBuffer(&data, &size);
52-
return size;
53-
}
54-
5542
} // anonymous namespace
5643

5744
ThriftSender::ThriftSender(std::unique_ptr<utils::Transport>&& transporter)
@@ -60,6 +47,7 @@ ThriftSender::ThriftSender(std::unique_ptr<utils::Transport>&& transporter)
6047
, _byteBufferSize(0)
6148
, _processByteSize(0)
6249
, _protocolFactory(_transporter->protocolFactory())
50+
, _thriftBuffer(new apache::thrift::transport::TMemoryBuffer())
6351
{
6452
}
6553

@@ -82,15 +70,13 @@ int ThriftSender::append(const Span& span)
8270
});
8371
_process.__set_tags(thriftTags);
8472

85-
_processByteSize =
86-
calcSizeOfSerializedThrift(*_protocolFactory, _process, _transporter->maxPacketSize());
73+
_processByteSize = calcSizeOfSerializedThrift(_process);
8774
_maxSpanBytes =
8875
_transporter->maxPacketSize() - _processByteSize - kEmitBatchOverhead;
8976
}
9077
thrift::Span jaegerSpan;
9178
span.thrift(jaegerSpan);
92-
const auto spanSize =
93-
calcSizeOfSerializedThrift(*_protocolFactory, jaegerSpan, _transporter->maxPacketSize());
79+
const auto spanSize = calcSizeOfSerializedThrift(jaegerSpan);
9480
if (spanSize > _maxSpanBytes) {
9581
std::ostringstream oss;
9682
throw Sender::Exception("Span is too large", 1);

src/jaegertracing/ThriftSender.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "jaegertracing/Sender.h"
2323
#include "jaegertracing/thrift-gen/jaeger_types.h"
2424
#include "jaegertracing/utils/Transport.h"
25+
#include <thrift/transport/TBufferTransports.h>
2526

2627
namespace jaegertracing {
2728

@@ -50,13 +51,28 @@ class ThriftSender : public Sender {
5051
_byteBufferSize = _processByteSize;
5152
}
5253

54+
template <typename ThriftType>
55+
int calcSizeOfSerializedThrift(const ThriftType& base)
56+
{
57+
_thriftBuffer->resetBuffer();
58+
auto _protocol = _protocolFactory->getProtocol(_thriftBuffer);
59+
base.write(_protocol.get());
60+
uint8_t* data = nullptr;
61+
uint32_t size = 0;
62+
_thriftBuffer->getBuffer(&data, &size);
63+
return size;
64+
}
65+
5366
std::unique_ptr<utils::Transport> _transporter;
5467
int _maxSpanBytes;
5568
int _byteBufferSize;
5669
std::vector<thrift::Span> _spanBuffer;
5770
thrift::Process _process;
5871
int _processByteSize;
5972
std::unique_ptr<apache::thrift::protocol::TProtocolFactory> _protocolFactory;
73+
// reuse buffer across serializations of different ThriftType for size
74+
// calculation
75+
std::shared_ptr<apache::thrift::transport::TMemoryBuffer> _thriftBuffer;
6076
};
6177

6278
} // namespace jaegertracing

0 commit comments

Comments
 (0)