Skip to content

Commit 116b12b

Browse files
committed
Revert back to using mmap to read the files
1 parent b05893f commit 116b12b

File tree

3 files changed

+16
-28
lines changed

3 files changed

+16
-28
lines changed

extras/analyze/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
66

77
include(FetchContent)
88

9+
FetchContent_Declare(
10+
mio
11+
GIT_REPOSITORY https://github.com/mandreyel/mio.git
12+
GIT_TAG 3f86a95c0784d73ce6815237ec33ed25f233b643
13+
)
14+
FetchContent_MakeAvailable(mio)
15+
916
FetchContent_Declare(
1017
pybind11
1118
GIT_REPOSITORY https://github.com/pybind/pybind11.git
@@ -38,3 +45,4 @@ file(GLOB_RECURSE sources CONFIGURE_DEPENDS "src/*.cpp")
3845
pybind11_add_module(eventanalyze ${sources})
3946
target_include_directories(eventanalyze PUBLIC include)
4047
target_compile_features(eventanalyze PUBLIC cxx_std_20)
48+
target_link_libraries(eventanalyze PRIVATE mio::mio)

extras/analyze/include/types.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "py.hpp"
1616

17+
#include <mio/mmap.hpp>
18+
1719
namespace ev {
1820
using Number = std::variant<std::int64_t, std::uint64_t, double>;
1921

@@ -118,7 +120,8 @@ struct Benchmark {
118120

119121
struct Logs {
120122
std::filesystem::path LogFile;
121-
std::string RawLog;
123+
mio::mmap_source MMap;
124+
std::string_view RawLog;
122125
std::vector<std::shared_ptr<Benchmark>> Benchmarks;
123126
};
124127

extras/analyze/src/parse.cpp

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
#include <array>
55
#include <cassert>
66
#include <charconv>
7-
#include <deque>
87
#include <execution>
98
#include <filesystem>
10-
#include <fstream>
11-
#include <iostream>
129
#include <mutex>
1310
#include <ranges>
1411
#include <string>
@@ -26,28 +23,6 @@ using namespace ev;
2623
namespace py = pybind11;
2724
namespace fs = std::filesystem;
2825

29-
// Read a whole file in at once
30-
std::string slurp(const fs::path &Path) {
31-
// Open first to help ensure that we get the correct file size
32-
std::ifstream File(Path);
33-
34-
std::string Result;
35-
Result.resize(fs::file_size(Path));
36-
37-
File.read(Result.data(), Result.size());
38-
// In case there's anything left over
39-
while (File) {
40-
static constexpr std::size_t BufSize = 1024;
41-
std::array<char, BufSize> Buffer;
42-
File.read(Buffer.data(), Buffer.size());
43-
Result.insert(Result.end(), Buffer.begin(), Buffer.end());
44-
}
45-
46-
Result.erase(Result.find('\0'), Result.size());
47-
48-
return Result;
49-
}
50-
5126
static constexpr std::string_view RegionNameEv =
5227
R"("event_id": "ProcessDag", "name": ")";
5328
static const std::boyer_moore_horspool_searcher
@@ -331,7 +306,8 @@ void ev::defParse(py::module &Mod) {
331306
}
332307
auto Logs = std::make_shared<ev::Logs>();
333308
Logs->LogFile = std::move(Path);
334-
Logs->RawLog = ::slurp(Logs->LogFile);
309+
Logs->MMap = mio::mmap_source(Logs->LogFile.string());
310+
Logs->RawLog = std::string_view(Logs->MMap.data(), Logs->MMap.size());
335311
const std::string_view File = Logs->RawLog;
336312

337313
const std::vector<BenchmarkRegion> BenchmarkSections =
@@ -363,7 +339,8 @@ void ev::defParse(py::module &Mod) {
363339
std::string_view BenchmarkName) {
364340
auto Logs = std::make_shared<ev::Logs>();
365341
Logs->LogFile = std::move(Path);
366-
Logs->RawLog = ::slurp(Logs->LogFile);
342+
Logs->MMap = mio::mmap_source(Logs->LogFile.string());
343+
Logs->RawLog = std::string_view(Logs->MMap.data(), Logs->MMap.size());
367344
const std::string_view File = Logs->RawLog;
368345

369346
Logs->Benchmarks.push_back(

0 commit comments

Comments
 (0)