Skip to content

Commit a3bca56

Browse files
committed
[flang][runtime] Tweak GetNextNonBlank() performance
When skipping blanks during input from an ASCII file, scan the buffered characters directly when possible rather than using the more general path. This adds complexity, but shaves a few percent off the runtime of a code that reads in millions of list-directed integers (best time over multiple runs goes from 17.56 to 16.84 sec).
1 parent d5103c1 commit a3bca56

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

flang-rt/include/flang-rt/runtime/connection.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ struct ConnectionState : public ConnectionAttributes {
7373
auto least{leftTabLimit.value_or(0)};
7474
auto newPos{positionInRecord + n};
7575
positionInRecord = newPos < least ? least : newPos;
76-
;
7776
}
7877

7978
RT_API_ATTRS void BeginRecord() {

flang-rt/include/flang-rt/runtime/io-stmt.h

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@ class IoStatementState {
184184
}
185185
connection_.HandleRelativePosition(bytes);
186186
}
187+
RT_API_ATTRS bool SkipBlanks() {
188+
if (at_) {
189+
const char *start{at_};
190+
while (at_ < limit_ && (*at_ == ' ' || *at_ == '\t' || *at_ == '\n')) {
191+
++at_;
192+
}
193+
connection_.HandleRelativePosition(at_ - start);
194+
return true;
195+
} else {
196+
return false;
197+
}
198+
}
187199

188200
// Could there be a list-directed repetition count here?
189201
RT_API_ATTRS bool MightBeRepetitionCount() const {
@@ -289,24 +301,32 @@ class IoStatementState {
289301
// Skips spaces, advances records, and ignores NAMELIST comments
290302
RT_API_ATTRS common::optional<char32_t> GetNextNonBlank(
291303
std::size_t &byteCount, FastAsciiField *fastField = nullptr) {
292-
auto ch{GetCurrentChar(byteCount, fastField)};
293304
bool inNamelist{mutableModes().inNamelist};
305+
if (fastField) {
306+
while (fastField->SkipBlanks()) {
307+
if (auto ch{fastField->Next()}) {
308+
if (inNamelist && *ch == '!') {
309+
// skip namelist comment
310+
} else {
311+
byteCount = 1;
312+
return ch;
313+
}
314+
}
315+
if (!AdvanceRecord()) {
316+
break;
317+
}
318+
fastField->NextRecord(*this);
319+
}
320+
}
321+
auto ch{GetCurrentCharSlow(byteCount)};
294322
while (!ch || *ch == ' ' || *ch == '\t' || *ch == '\n' ||
295323
(inNamelist && *ch == '!')) {
296324
if (ch && (*ch == ' ' || *ch == '\t' || *ch == '\n')) {
297-
if (fastField) {
298-
fastField->Advance(0, byteCount);
299-
} else {
300-
HandleRelativePosition(byteCount);
301-
}
302-
} else if (AdvanceRecord()) {
303-
if (fastField) {
304-
fastField->NextRecord(*this);
305-
}
306-
} else {
325+
HandleRelativePosition(byteCount);
326+
} else if (!AdvanceRecord()) {
307327
return common::nullopt;
308328
}
309-
ch = GetCurrentChar(byteCount, fastField);
329+
ch = GetCurrentCharSlow(byteCount);
310330
}
311331
return ch;
312332
}

0 commit comments

Comments
 (0)