-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfast_parser.cpp
More file actions
51 lines (43 loc) · 1.19 KB
/
fast_parser.cpp
File metadata and controls
51 lines (43 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
namespace py = pybind11;
struct MarketTick {
uint64_t timestamp;
char symbol[8];
double price;
};
class FastParser {
public:
void* mapped_data;
size_t filesize;
int fd;
FastParser(const std::string& filepath) {
fd = open(filepath.c_str(), O_RDONLY);
struct stat st;
fstat(fd, &st);
filesize = st.st_size;
mapped_data = mmap(NULL, filesize, PROT_READ, MAP_PRIVATE, fd, 0);
}
~FastParser() {
munmap(mapped_data, filesize);
close(fd);
}
py::array_t<double> get_prices() {
size_t num_ticks = filesize / sizeof(MarketTick);
return py::array_t<double>(
{ (ssize_t)num_ticks },
{ (ssize_t)sizeof(MarketTick) },
(double*)((char*)mapped_data + 16),
py::cast(this)
);
}
};
PYBIND11_MODULE(fast_parser, m) {
py::class_<FastParser>(m, "FastParser")
.def(py::init<const std::string&>())
.def("get_prices", &FastParser::get_prices);
}