Skip to content

Commit

Permalink
optimize FileLogger::Buffer
Browse files Browse the repository at this point in the history
only search the new data for newline (we know the buffer doesn't have any), and only search once
  • Loading branch information
rzblue committed Nov 21, 2024
1 parent 0a3ccf9 commit 072dc9d
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions wpiutil/src/main/native/cpp/FileLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ FileLogger::FileLogger(std::string_view file,
m_inotifyHandle{inotify_init()},
m_inotifyWatchHandle{
inotify_add_watch(m_inotifyHandle, file.data(), IN_MODIFY)},
// capturing this is not safe here...
// also need to think harder about if synchronization is needed here
m_thread{[=, this] {
char buf[8000];
char eventBuf[sizeof(struct inotify_event) + NAME_MAX + 1];
Expand Down Expand Up @@ -87,18 +89,16 @@ FileLogger::~FileLogger() {

std::function<void(std::string_view)> FileLogger::Buffer(
std::function<void(std::string_view)> callback) {
return [callback,
buf = wpi::SmallVector<char, 64>{}](std::string_view data) mutable {
buf.append(data.begin(), data.end());
if (!wpi::contains({data.data(), data.size()}, "\n")) {
return;
return [callback, buf = wpi::SmallVector<char, 64>{}](
std::string_view newData) mutable {
size_t newlineLocation = newData.rfind("\n");
if (newlineLocation != std::string_view::npos) {
buf.append(newData.begin(), newData.begin() + newlineLocation);
callback({buf.data(), buf.size()});
buf.clear();
newData = newData.substr(newlineLocation + 1);
}
auto [wholeData, extra] = wpi::rsplit({buf.data(), buf.size()}, "\n");
std::string leftover{extra};

callback(wholeData);
buf.clear();
buf.append(leftover.begin(), leftover.end());
buf.append(newData.begin(), newData.end());
};
}
} // namespace wpi

0 comments on commit 072dc9d

Please sign in to comment.