Skip to content

Commit

Permalink
Avoid memory copy when parsing log
Browse files Browse the repository at this point in the history
  • Loading branch information
JackyWoo committed Feb 21, 2024
1 parent 163f348 commit 4039693
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
15 changes: 6 additions & 9 deletions src/Service/LogEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ ptr<buffer> LogEntryBody::serialize(ptr<log_entry> & entry)
return entry_buf;
}

ptr<log_entry> LogEntryBody::parse(const char * entry_str, const UInt64 & term, size_t buf_size)
ptr<log_entry> LogEntryBody::parse(const char * entry_str, size_t buf_size)
{
auto entry_buf = buffer::alloc(buf_size);
entry_buf->put_raw(reinterpret_cast<const byte *>(entry_str), buf_size);

entry_buf->pos(0);
nuraft::log_val_type tp = static_cast<nuraft::log_val_type>(entry_buf->get_byte());

ptr<buffer> data = buffer::copy(*entry_buf);
return cs_new<log_entry>(term, data, tp);
nuraft::log_val_type tp = static_cast<nuraft::log_val_type>(entry_str[0]);
auto data = buffer::alloc(buf_size - 1);
data->put_raw(reinterpret_cast<const byte *>(entry_str + 1), buf_size - 1);
data->pos(0);
return cs_new<log_entry>(0, data, tp); /// term is set latter
}

}
2 changes: 1 addition & 1 deletion src/Service/LogEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LogEntryBody
{
public:
static ptr<buffer> serialize(ptr<log_entry> & entry);
static ptr<log_entry> parse(const char * entry_str, const UInt64 & term, size_t buf_size);
static ptr<log_entry> parse(const char * entry_str, size_t buf_size);
};

}
3 changes: 2 additions & 1 deletion src/Service/NuRaftLogSegment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,8 @@ int NuRaftLogSegment::loadLogEntry(int fd, off_t offset, LogEntryHeader * head,
return -1;
}

entry = LogEntryBody::parse(entry_str, head->term, head->data_length);
entry = LogEntryBody::parse(entry_str, head->data_length);
entry->set_term(head->term);

delete[] entry_str;
return 0;
Expand Down
17 changes: 17 additions & 0 deletions src/Service/tests/gtest_raft_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <gtest/gtest.h>
#include <libnuraft/nuraft.hxx>

#include <Service/LogEntry.h>
#include <Service/KeeperUtils.h>
#include <Service/NuRaftFileLogStore.h>
#include <Service/NuRaftLogSegment.h>
Expand Down Expand Up @@ -78,6 +79,22 @@ TEST(RaftLog, serializeEntry)
ASSERT_EQ(zk_create_request->data, data);
}

TEST(RaftLog, parseLogEntrybody)
{
ptr<buffer> data = buffer::alloc(2);
data->pos(0);
data->put(static_cast<byte>('1'));
data->put(static_cast<byte>('2'));
data->pos(0);
ptr<log_entry> log = cs_new<log_entry>(0, data, nuraft::log_val_type::app_log);

ptr<buffer> serialized_log = LogEntryBody::serialize(log);
ptr<log_entry> parsed_log = LogEntryBody::parse(reinterpret_cast<const char *>(serialized_log->data_begin()), serialized_log->size());

ASSERT_EQ(parsed_log->get_val_type(), log->get_val_type());
ASSERT_EQ(parsed_log->get_buf().get_str(), log->get_buf().get_str());
}

TEST(RaftLog, appendEntry)
{
String log_dir(LOG_DIR + "/1");
Expand Down

0 comments on commit 4039693

Please sign in to comment.