Skip to content

Commit e5cd69b

Browse files
cpovirkcopybara-github
authored andcommitted
Memory-map the command-line file.
PiperOrigin-RevId: 833347912 Change-Id: I6051da61f1029cde8abc78637d38aeac18876bd2
1 parent 7bacce2 commit e5cd69b

File tree

3 files changed

+34
-45
lines changed

3 files changed

+34
-45
lines changed

src/tools/singlejar/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ cc_library(
515515
hdrs = ["token_stream.h"],
516516
deps = [
517517
":diag",
518+
":mapped_file",
518519
"//src/main/cpp/util",
519520
],
520521
)

src/tools/singlejar/token_stream.h

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "src/main/cpp/util/path_platform.h"
2929
#include "src/tools/singlejar/diag.h"
30+
#include "src/tools/singlejar/mapped_file.h"
3031

3132
/*
3233
* Tokenize command line containing indirect command line arguments.
@@ -59,39 +60,26 @@ class ArgTokenStream {
5960
// Internal class to handle indirect command files.
6061
class FileTokenStream {
6162
public:
62-
FileTokenStream(const char *filename) {
63-
#ifdef _WIN32
64-
std::wstring wpath;
65-
std::string error;
66-
if (!blaze_util::AsAbsoluteWindowsPath(filename, &wpath, &error)) {
67-
diag_err(1, "%s:%d: AsAbsoluteWindowsPath failed: %s", __FILE__,
68-
__LINE__, error.c_str());
69-
}
70-
fp_ = _wfopen(wpath.c_str(), L"r");
71-
#else
72-
fp_ = fopen(filename, "r");
73-
#endif
74-
75-
if (!fp_) {
76-
diag_err(1, "%s", filename);
63+
FileTokenStream(const char* filename)
64+
: next_ptr_(nullptr), end_ptr_(nullptr) {
65+
if (!mapped_file_.Open(filename)) {
66+
diag_err(1, "Cannot open param file: %s", filename);
7767
}
7868
filename_ = filename;
69+
next_ptr_ = static_cast<const unsigned char*>(mapped_file_.start());
70+
end_ptr_ = static_cast<const unsigned char*>(mapped_file_.end());
7971
next_char();
8072
}
8173

82-
~FileTokenStream() { close(); }
74+
~FileTokenStream() = default;
8375

8476
// Assign next token to TOKEN, return true on success, false on EOF.
8577
bool next_token(std::string *token) {
86-
if (!fp_) {
87-
return false;
88-
}
8978
*token = "";
9079
while (current_char_ != EOF && isspace(current_char_)) {
9180
next_char();
9281
}
9382
if (current_char_ == EOF) {
94-
close();
9583
return false;
9684
}
9785
for (;;) {
@@ -123,14 +111,6 @@ class ArgTokenStream {
123111
}
124112

125113
private:
126-
void close() {
127-
if (fp_) {
128-
fclose(fp_);
129-
fp_ = nullptr;
130-
}
131-
filename_.clear();
132-
}
133-
134114
// Append the quoted string to the TOKEN. The quote character (which can be
135115
// single or double quote) is in the current character. Everything up to the
136116
// matching quote character is appended.
@@ -164,34 +144,41 @@ class ArgTokenStream {
164144
// Get the next character from the input stream. Skip backslash followed
165145
// by the newline.
166146
void next_char() {
167-
if (feof(fp_)) {
168-
current_char_ = EOF;
169-
return;
170-
}
171-
current_char_ = getc(fp_);
172-
// Eat "\\\n" sequence.
173-
while (current_char_ == '\\') {
174-
int c = getc(fp_);
175-
if (c == '\n') {
176-
current_char_ = getc(fp_);
177-
} else {
178-
if (c != EOF) {
179-
ungetc(c, fp_);
180-
}
181-
break;
182-
}
147+
do {
148+
current_char_ = raw_next();
149+
} while (eat_line_continuation());
150+
}
151+
152+
bool eat_line_continuation() {
153+
if (current_char_ == '\\' && peek_raw_next() == '\n') {
154+
next_ptr_++;
155+
return true;
156+
} else if (current_char_ == '\\' && peek_raw_next() == '\r' &&
157+
peek_raw_next(1) == '\n') {
158+
next_ptr_ += 2;
159+
return true;
183160
}
161+
return false;
162+
}
163+
164+
int raw_next() { return next_ptr_ < end_ptr_ ? *next_ptr_++ : EOF; }
165+
166+
int peek_raw_next(size_t offset = 0) {
167+
return (next_ptr_ + offset) < end_ptr_ ? *(next_ptr_ + offset) : EOF;
184168
}
185169

186-
FILE *fp_;
170+
MappedFile mapped_file_;
187171
std::string filename_;
172+
const unsigned char* next_ptr_;
173+
const unsigned char* end_ptr_;
188174
int current_char_;
189175
};
190176

191177
public:
192178
// Constructor. Automatically reads the first token.
193179
ArgTokenStream(int argc, const char *const *argv)
194180
: argv_(argv), argv_end_(argv + argc) {
181+
token_.reserve(1024);
195182
next();
196183
}
197184

tools/jdk/BUILD.java_tools

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ cc_library(
566566
deps = [
567567
":diag",
568568
":filesystem",
569+
":mapped_file",
569570
],
570571
)
571572

0 commit comments

Comments
 (0)