-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlexer_bench.cc
118 lines (95 loc) · 2.91 KB
/
lexer_bench.cc
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include "FE/lexer.h"
#include "FE/parse.h"
#include "Util/parse.h"
#include "Util/switch.h"
namespace {
using namespace cwerg;
using namespace cwerg::fe;
#if 0
std::string_view ReadFile(const char* filename) {
std::ifstream finFile;
std::istream* fin;
if (filename == std::string_view("-")) {
fin = &std::cin;
} else {
finFile.open(filename);
fin = &finFile;
}
std::vector<char>* data = SlurpDataFromStream(fin);
return std::string_view(reinterpret_cast<const char*>(data->data()),
data->size());
}
#else
std::string_view ReadFile(const char* filename) {
int fd = open(filename, O_RDONLY, 0);
if (fd < 0) {
std::cerr << "Cannot open input file [" << filename << "] err=" << fd
<< "\n";
return std::string_view();
}
struct stat sb;
fstat(fd, &sb);
// map an extra terminator byte
void* data_bytes = mmap(NULL, sb.st_size + 1, PROT_WRITE, MAP_PRIVATE, fd, 0);
if (data_bytes == MAP_FAILED) {
std::cerr << "Cannot mmap input file " << filename << "\n";
return std::string_view();
}
close(fd);
return std::string_view(reinterpret_cast<char*>(data_bytes), sb.st_size);
}
#endif
void RunLexer(Lexer* lexer) {
while (true) {
auto tk = lexer->Next();
// std::cout << tk << "\n";
if (tk.kind == TK_KIND::SPECIAL_EOF) {
break;
}
}
}
void RunParser(Lexer* lexer, Name name) { ParseDefMod(lexer, name); }
} // namespace
SwitchInt32 sw_multiplier("multiplier", "adjust multiplies for item pool sizes",
16);
SwitchString sw_mode("m", "benchmark mode: `lexer`, `paser`", "lexer");
int main(int argc, const char* argv[]) {
int start_positional = SwitchBase::ParseArgv(argc, argv, &std::cerr);
if (start_positional >= argc) {
std::cerr << "need at least one input file\n";
return 1;
}
// If the synchronization is turned off, the C++ standard streams are allowed
// to buffer their I/O independently from their stdio counterparts, which may
// be considerably faster in some cases.
std::ios_base::sync_with_stdio(false);
InitStripes(sw_multiplier.Value());
InitParser();
int total_bytes = 0;
int total_lines = 0;
for (int i = start_positional; i < argc; ++i) {
auto data = ReadFile(argv[i]);
total_bytes += data.size();
std::cout << "processing: " << argv[i] << " bytes=" << data.size() << "\n";
Lexer lexer(data, i);
std::string_view mode = sw_mode.Value();
if (mode == "lexer") {
RunLexer(&lexer);
} else if (mode == "parser") {
RunParser(&lexer, NameNew(argv[i]));
} else {
std::cerr << "unknown mode " << mode << "\n";
return 1;
}
total_lines += lexer.LinesProcessed();
}
std::cout << "Processed: bytes=" << total_bytes << " lines=" << total_lines
<< "\n";
}