Skip to content

Commit 1127018

Browse files
jderegclaude
andcommitted
Optimize FastReader.readUntil() scan loop and simplify position update
Use do-while with single array access per iteration instead of double access in while condition. Hoist position assignment above delimiter check to eliminate duplicate write. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent db1dbe1 commit 1127018

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

src/main/java/com/cedarsoftware/util/FastReader.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,21 +225,20 @@ public int readUntil(final char[] dest, int off, int maxLen, char delim1, char d
225225
int pos = position;
226226
int end = pos + Math.min(limit - pos, maxLen - totalRead);
227227
int scanPos = pos;
228-
while (scanPos < end && locBuf[scanPos] != delim1 && locBuf[scanPos] != delim2) {
229-
scanPos++;
230-
}
228+
do {
229+
char c = locBuf[scanPos];
230+
if (c == delim1 || c == delim2) break;
231+
} while (++scanPos < end);
231232
int copyLen = scanPos - pos;
232233
if (copyLen > 0) {
233234
System.arraycopy(locBuf, pos, dest, off, copyLen);
234235
off += copyLen;
235236
totalRead += copyLen;
236237
}
238+
position = scanPos;
237239
if (scanPos < end) {
238-
// Found delimiter — don't consume it
239-
position = scanPos;
240-
return totalRead;
240+
return totalRead; // Found delimiter — don't consume it
241241
}
242-
position = scanPos;
243242
}
244243

245244
return totalRead;

0 commit comments

Comments
 (0)