Skip to content

Commit

Permalink
Merge pull request #109 from bersler/performance_optimizations
Browse files Browse the repository at this point in the history
enhancement: performance optimizations
  • Loading branch information
bersler authored Feb 15, 2024
2 parents 9f38a1f + 669a8c5 commit ed5df09
Show file tree
Hide file tree
Showing 15 changed files with 1,191 additions and 1,155 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
1.5.1
- enhancement: add parameter -p
- enhancement: performance optimizations

1.5.0
- code style cleanup
Expand Down
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0 -Wall -Wextra -Wshado
set(CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
configure_file(config.h.in ../config.h)

if (CPU_ARCH)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=${CPU_ARCH} -mtune=${CPU_ARCH}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -march=${CPU_ARCH} -mtune=${CPU_ARCH}")
add_compile_definitions(CPU_ARCH=${CPU_ARCH})
endif()

find_package(Threads REQUIRED)

# RapidJSON
Expand Down
1 change: 1 addition & 0 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ along with OpenLogReplicator; see the file LICENSE; If not see
#define OpenLogReplicator_VERSION_PATCH @OpenLogReplicator_VERSION_PATCH@
#define OpenLogReplicator_CMAKE_BUILD_TYPE "@CMAKE_BUILD_TYPE@"
#define OpenLogReplicator_CMAKE_BUILD_TIMESTAMP "@CMAKE_BUILD_TIMESTAMP@"
#define OpenLogReplicator_CPU_ARCH "@CPU_ARCH@"

#endif
3 changes: 2 additions & 1 deletion documentation/release-notes/release-notes.adoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= Release Notes
:author: Adam Leszczyński <[email protected]>
:revnumber: 1.5.1
:revdate: 2024-02-14
:revdate: 2024-02-15
:imagesdir: ./images
:url-github: https://github.com/bersler/OpenLogReplicator
:url-db-engines: https://db-engines.com/en/ranking_trend
Expand All @@ -18,6 +18,7 @@ This document describes the release notes of OpenLogReplicator.

=== Version 1.5.1
- enhancement: add parameter -p
- enhancement: performance optimizations

=== Version 1.5.0
- code style cleanup
Expand Down
74 changes: 38 additions & 36 deletions src/builder/Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace OpenLogReplicator {
unknownType(newUnknownType),
unconfirmedLength(0),
messageLength(0),
messagePosition(0),
flushBuffer(newFlushBuffer),
valueBuffer(nullptr),
valueLength(0),
Expand Down Expand Up @@ -236,7 +237,7 @@ namespace OpenLogReplicator {

uint64_t fraction = 0;
if (length == 11)
fraction = Ctx::read32Big(data + 7);
fraction = ctx->read32Big(data + 7);

if (second < 0 || second > 59 || minute < 0 || minute > 59 || hour < 0 || hour > 23 || day < 0 || day > 30 || month < 0 || month > 11 ||
fraction > 999999999) {
Expand Down Expand Up @@ -280,7 +281,7 @@ namespace OpenLogReplicator {

uint64_t fraction = 0;
if (length == 11)
fraction = Ctx::read32Big(data + 7);
fraction = ctx->read32Big(data + 7);

if (second < 0 || second > 59 || minute < 0 || minute > 59 || hour < 0 || hour > 23 || day < 0 || day > 30 || month < 0 || month > 11 ||
fraction > 999999999) {
Expand Down Expand Up @@ -341,7 +342,7 @@ namespace OpenLogReplicator {

uint64_t fraction = 0;
if (length == 13)
fraction = Ctx::read32Big(data + 7);
fraction = ctx->read32Big(data + 7);

const char* tz;
char tz2[7];
Expand All @@ -354,24 +355,24 @@ namespace OpenLogReplicator {

if (data[11] < 20) {
uint64_t val = 20 - data[11];
tz2[1] = ctx->map10[val / 10];
tz2[2] = ctx->map10[val % 10];
tz2[1] = '0' + static_cast<char>(val / 10);
tz2[2] = '0' + static_cast<char>(val % 10);
} else {
uint64_t val = data[11] - 20;
tz2[1] = ctx->map10[val / 10];
tz2[2] = ctx->map10[val % 10];
tz2[1] = '0' + static_cast<char>(val / 10);
tz2[2] = '0' + static_cast<char>(val % 10);
}

tz2[3] = ':';

if (data[12] < 60) {
uint64_t val = 60 - data[12];
tz2[4] = ctx->map10[val / 10];
tz2[5] = ctx->map10[val % 10];
tz2[4] = '0' + static_cast<char>(val / 10);
tz2[5] = '0' + static_cast<char>(val % 10);
} else {
uint64_t val = data[12] - 60;
tz2[4] = ctx->map10[val / 10];
tz2[5] = ctx->map10[val % 10];
tz2[4] = '0' + static_cast<char>(val / 10);
tz2[5] = '0' + static_cast<char>(val % 10);
}
tz2[6] = 0;
tz = tz2;
Expand Down Expand Up @@ -404,9 +405,9 @@ namespace OpenLogReplicator {
bool minus = false;
uint64_t year;
if ((data[0] & 0x80) != 0)
year = Ctx::read32Big(data) - 0x80000000;
year = ctx->read32Big(data) - 0x80000000;
else {
year = 0x80000000 - Ctx::read32Big(data);
year = 0x80000000 - ctx->read32Big(data);
minus = true;
}

Expand Down Expand Up @@ -434,7 +435,7 @@ namespace OpenLogReplicator {
valueBuffer[valueLength++] = '0';
} else {
while (val) {
buffer[len++] = ctx->map10[val % 10];
buffer[len++] = '0' + static_cast<char>(val % 10);
val /= 10;
}
while (len > 0)
Expand All @@ -451,7 +452,7 @@ namespace OpenLogReplicator {
valueBuffer[valueLength++] = '0';
} else {
while (val) {
buffer[len++] = ctx->map10[val % 10];
buffer[len++] = '0' + static_cast<char>(val % 10);
val /= 10;
}
while (len > 0)
Expand All @@ -467,9 +468,9 @@ namespace OpenLogReplicator {

if (month >= 10) {
valueBuffer[valueLength++] = '1';
valueBuffer[valueLength++] = ctx->map10[month - 10];
valueBuffer[valueLength++] = '0' + static_cast<char>(month - 10);
} else
valueBuffer[valueLength++] = ctx->map10[month];
valueBuffer[valueLength++] = '0' + static_cast<char>(month);

columnString(column->name);
}
Expand All @@ -484,17 +485,17 @@ namespace OpenLogReplicator {
bool minus = false;
uint64_t day;
if ((data[0] & 0x80) != 0)
day = Ctx::read32Big(data) - 0x80000000;
day = ctx->read32Big(data) - 0x80000000;
else {
day = 0x80000000 - Ctx::read32Big(data);
day = 0x80000000 - ctx->read32Big(data);
minus = true;
}

int32_t us;
if ((data[7] & 0x80) != 0)
us = Ctx::read32Big(data + 7) - 0x80000000;
us = ctx->read32Big(data + 7) - 0x80000000;
else {
us = 0x80000000 - Ctx::read32Big(data + 7);
us = 0x80000000 - ctx->read32Big(data + 7);
minus = true;
}

Expand Down Expand Up @@ -541,7 +542,7 @@ namespace OpenLogReplicator {
valueBuffer[valueLength++] = '0';
} else {
while (val) {
buffer[len++] = ctx->map10[val % 10];
buffer[len++] = '0' + static_cast<char>(val % 10);
val /= 10;
}
while (len > 0)
Expand All @@ -555,18 +556,18 @@ namespace OpenLogReplicator {
else if (intervalDtsFormat == INTERVAL_DTS_FORMAT_ISO8601_DASH)
valueBuffer[valueLength++] = '-';

valueBuffer[valueLength++] = ctx->map10[hour / 10];
valueBuffer[valueLength++] = ctx->map10[hour % 10];
valueBuffer[valueLength++] = '0' + static_cast<char>(hour / 10);
valueBuffer[valueLength++] = '0' + static_cast<char>(hour % 10);
valueBuffer[valueLength++] = ':';
valueBuffer[valueLength++] = ctx->map10[minute / 10];
valueBuffer[valueLength++] = ctx->map10[minute % 10];
valueBuffer[valueLength++] = '0' + static_cast<char>(minute / 10);
valueBuffer[valueLength++] = '0' + static_cast<char>(minute % 10);
valueBuffer[valueLength++] = ':';
valueBuffer[valueLength++] = ctx->map10[second / 10];
valueBuffer[valueLength++] = ctx->map10[second % 10];
valueBuffer[valueLength++] = '0' + static_cast<char>(second / 10);
valueBuffer[valueLength++] = '0' + static_cast<char>(second % 10);
valueBuffer[valueLength++] = '.';

for (uint64_t j = 0; j < 9; ++j) {
valueBuffer[valueLength + 8 - j] = ctx->map10[us % 10];
valueBuffer[valueLength + 8 - j] = '0' + static_cast<char>(us % 10);
us /= 10;
}
valueLength += 9;
Expand Down Expand Up @@ -598,7 +599,7 @@ namespace OpenLogReplicator {
valueBuffer[valueLength++] = '0';
} else {
while (val) {
buffer[len++] = ctx->map10[val % 10];
buffer[len++] = '0' + static_cast<char>(val % 10);
val /= 10;
}
while (len > 0)
Expand Down Expand Up @@ -719,17 +720,18 @@ namespace OpenLogReplicator {
nextBuffer->data = reinterpret_cast<uint8_t*>(nextBuffer) + sizeof(struct BuilderQueue);

// Message could potentially fit in one buffer
if (copy && msg != nullptr && sizeof(struct BuilderMsg) + messageLength < OUTPUT_BUFFER_DATA_SIZE) {
memcpy(reinterpret_cast<void*>(nextBuffer->data), msg, sizeof(struct BuilderMsg) + messageLength);
if (copy && msg != nullptr && messageLength + messagePosition < OUTPUT_BUFFER_DATA_SIZE) {
memcpy(reinterpret_cast<void*>(nextBuffer->data), msg, messagePosition);
msg = reinterpret_cast<BuilderMsg*>(nextBuffer->data);
msg->data = nextBuffer->data + sizeof(struct BuilderMsg);
nextBuffer->length = sizeof(struct BuilderMsg) + messageLength;
nextBuffer->start = 0;
lastBuilderQueue->length -= sizeof(struct BuilderMsg) + messageLength;
} else {
nextBuffer->length = 0;
lastBuilderQueue->length += messagePosition;
messageLength += messagePosition;
messagePosition = 0;
nextBuffer->start = BUFFER_START_UNDEFINED;
}
nextBuffer->length = 0;

{
std::unique_lock<std::mutex> lck(mtx);
Expand All @@ -740,7 +742,7 @@ namespace OpenLogReplicator {
}

uint64_t Builder::builderSize() const {
return ((messageLength + 7) & 0xFFFFFFFFFFFFFFF8) + sizeof(struct BuilderMsg);
return ((messageLength + messagePosition + 7) & 0xFFFFFFFFFFFFFFF8);
}

uint64_t Builder::getMaxMessageMb() const {
Expand Down
Loading

0 comments on commit ed5df09

Please sign in to comment.