Skip to content

Commit

Permalink
Perf[BMQ]: do not create tmp functors in PutEventBuilder (#478)
Browse files Browse the repository at this point in the history
Signed-off-by: Evgeny Malygin <[email protected]>
  • Loading branch information
678098 authored Oct 28, 2024
1 parent 6d3790f commit 8550a81
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ settings.json
.cache/
.venv/

# fuzz-test output folder
**/boofuzz-results

# Symlink from 'src/applications/bmqbrkr/run'
src/applications/bmqbrkr/etc/etc

Expand Down
42 changes: 25 additions & 17 deletions src/groups/bmq/bmqp/bmqp_puteventbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,31 @@ bool isWordAligned(const bdlbb::Blob& blob)
#endif
} // close unnamed namespace

// ---------------------------------
// class PutEventBuilder::ResetGuard
// ---------------------------------

inline PutEventBuilder::ResetGuard::ResetGuard(
PutEventBuilder& putEventBuilder)
: d_putEventBuilder(putEventBuilder)
{
// NOTHING
}

inline PutEventBuilder::ResetGuard::~ResetGuard()
{
d_putEventBuilder.resetFields();
}

// ---------------------
// class PutEventBuilder
// ---------------------

void PutEventBuilder::resetFields(void* ptr)
void PutEventBuilder::resetFields()
{
PutEventBuilder* builder = static_cast<PutEventBuilder*>(ptr);
builder->d_flags = 0;
builder->d_messageGUID = bmqt::MessageGUID();
builder->d_crc32c = 0;
d_flags = 0;
d_messageGUID = bmqt::MessageGUID();
d_crc32c = 0;
}

bmqt::EventBuilderResult::Enum
Expand Down Expand Up @@ -219,9 +234,7 @@ PutEventBuilder::packMessageInOldStyle(int queueId)

// Guid and flags need to be reset after this method (irrespective of its
// success or failure). Create a proctor to auto reset them.
const bsl::function<void()> f =
bdlf::BindUtil::bind(&PutEventBuilder::resetFields, this);
bdlb::ScopeExitAny resetter(f);
const ResetGuard guard(*this);

// Calculate length of entire application data (includes payload, message
// properties and padding, if any).
Expand Down Expand Up @@ -304,12 +317,9 @@ bmqt::EventBuilderResult::Enum PutEventBuilder::packMessage(int queueId)

typedef bmqt::EventBuilderResult Result;

// CorrelationId, guid and flags need to be reset after this method
// (irrespective of its success or failure). Create a proctor to auto
// reset them.
const bsl::function<void()> f =
bdlf::BindUtil::bind(&PutEventBuilder::resetFields, this);
bdlb::ScopeExitAny resetter(f);
// Guid and flags need to be reset after this method (irrespective of its
// success or failure). Create a proctor to auto reset them.
const ResetGuard guard(*this);

// Calculate length of entire application data (includes payload, message
// properties and padding, if any).
Expand Down Expand Up @@ -398,9 +408,7 @@ bmqt::EventBuilderResult::Enum PutEventBuilder::packMessageRaw(int queueId)

// Guid and flags need to be reset after this method (irrespective of its
// success or failure). Create a proctor to auto reset them.
const bsl::function<void()> f =
bdlf::BindUtil::bind(&PutEventBuilder::resetFields, this);
bdlb::ScopeExitAny resetter(f);
const ResetGuard guard(*this);

// Note that the 'd_blobPayload_p' has the entire application data.
return packMessageInternal(*d_blobPayload_p, queueId);
Expand Down
22 changes: 15 additions & 7 deletions src/groups/bmq/bmqp/bmqp_puteventbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
//

// BMQ

#include <bmqp_messageproperties.h>
#include <bmqp_protocol.h>
#include <bmqt_compressionalgorithmtype.h>
Expand Down Expand Up @@ -92,6 +91,18 @@ class PutEventBuilder {
typedef bdlb::NullableValue<bmqp::Protocol::MsgGroupId> NullableMsgGroupId;

private:
// TYPES
/// Mechanism to automatically reset PutEventBuilder on a built message
struct ResetGuard {
// DATA
PutEventBuilder& d_putEventBuilder;

// CREATORS
explicit ResetGuard(PutEventBuilder& putEventBuilder);

~ResetGuard();
};

// DATA
bdlbb::BlobBufferFactory* d_bufferFactory_p;

Expand Down Expand Up @@ -169,13 +180,10 @@ class PutEventBuilder {
PutEventBuilder& operator=(const PutEventBuilder&) BSLS_CPP11_DELETED;

private:
// CLASS LEVEL METHODS

/// Reset flags and message guid of PutEventBuilder instance pointed by
/// the specified `ptr`.
static void resetFields(void* ptr);

// PRIVATE MANIPULATORS
/// Reset flags and message guid of this object.
void resetFields();

bmqt::EventBuilderResult::Enum
packMessageInternal(const bdlbb::Blob& appData, int queueId);

Expand Down

0 comments on commit 8550a81

Please sign in to comment.