Skip to content

Commit 78d5b14

Browse files
committed
Reduce pointer chasing in hot loops
- Cache SOURCE->size into a local index in skip_whitespace() and comment-skipping loops; write back once. - Replace is_linebreak(peek_char(1)) with local pos check for line continuation to keep cached-index correctness.
1 parent 0a196ad commit 78d5b14

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/lexer.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,20 +235,23 @@ bool is_numeric(char buffer[])
235235

236236
void skip_whitespace(void)
237237
{
238+
int pos = SOURCE->size;
238239
while (true) {
239-
if (is_linebreak(next_char)) {
240-
SOURCE->size += 2;
241-
next_char = SOURCE->elements[SOURCE->size];
240+
/* Handle backslash-newline (line continuation) using local pos */
241+
if (next_char == '\\' && SOURCE->elements[pos + 1] == '\n') {
242+
pos += 2;
243+
next_char = SOURCE->elements[pos];
242244
continue;
243245
}
244246
if (is_whitespace(next_char) ||
245247
(skip_newline && is_newline(next_char))) {
246-
SOURCE->size++;
247-
next_char = SOURCE->elements[SOURCE->size];
248+
pos++;
249+
next_char = SOURCE->elements[pos];
248250
continue;
249251
}
250252
break;
251253
}
254+
SOURCE->size = pos;
252255
}
253256

254257
char read_char(bool is_skip_space)
@@ -296,27 +299,42 @@ token_t lex_token_internal(bool aliasing)
296299
/* C-style comments */
297300
if (next_char == '*') {
298301
/* in a comment, skip until end */
302+
int pos = SOURCE->size;
299303
do {
300-
read_char(false);
304+
/* advance one char */
305+
pos++;
306+
next_char = SOURCE->elements[pos];
301307
if (next_char == '*') {
302-
read_char(false);
308+
/* look ahead */
309+
pos++;
310+
next_char = SOURCE->elements[pos];
303311
if (next_char == '/') {
304-
read_char(true);
312+
/* consume closing '/', then commit and skip trailing
313+
* whitespaces
314+
*/
315+
pos++;
316+
next_char = SOURCE->elements[pos];
317+
SOURCE->size = pos;
318+
skip_whitespace();
305319
return lex_token_internal(aliasing);
306320
}
307321
}
308322
} while (next_char);
309323

324+
SOURCE->size = pos;
310325
if (!next_char)
311326
error("Unenclosed C-style comment");
312327
return lex_token_internal(aliasing);
313328
}
314329

315330
/* C++-style comments */
316331
if (next_char == '/') {
332+
int pos = SOURCE->size;
317333
do {
318-
read_char(false);
334+
pos++;
335+
next_char = SOURCE->elements[pos];
319336
} while (next_char && !is_newline(next_char));
337+
SOURCE->size = pos;
320338
return lex_token_internal(aliasing);
321339
}
322340

0 commit comments

Comments
 (0)