Skip to content

Commit fe7835b

Browse files
authored
Merge pull request #246 from sysprog21/improve-perf
Minor performance improvements
2 parents b1d171f + 78d5b14 commit fe7835b

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

src/defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ typedef struct arena_block {
7979
typedef struct {
8080
arena_block_t *head;
8181
int total_bytes; /* Track total allocation for profiling */
82+
int block_size; /* Default block size for new blocks */
8283
} arena_t;
8384

8485
/* string-based hash map definitions */

src/globals.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ arena_t *arena_init(int initial_capacity)
144144
}
145145
arena->head = arena_block_create(initial_capacity);
146146
arena->total_bytes = initial_capacity;
147+
/* Use the initial capacity as the default block size for future growth. */
148+
arena->block_size = initial_capacity;
147149
return arena;
148150
}
149151

@@ -166,9 +168,12 @@ void *arena_alloc(arena_t *arena, int size)
166168
size = (size + PTR_SIZE - 1) & ~(PTR_SIZE - 1);
167169

168170
if (!arena->head || arena->head->offset + size > arena->head->capacity) {
169-
/* Need a new block: choose capacity = max(DEFAULT_ARENA_SIZE, size) */
170-
int new_capacity =
171-
(size > DEFAULT_ARENA_SIZE ? size : DEFAULT_ARENA_SIZE);
171+
/* Need a new block: choose capacity = max(DEFAULT_ARENA_SIZE,
172+
* arena->block_size, size) */
173+
int base =
174+
(arena->block_size > DEFAULT_ARENA_SIZE ? arena->block_size
175+
: DEFAULT_ARENA_SIZE);
176+
int new_capacity = (size > base ? size : base);
172177
arena_block_t *new_block = arena_block_create(new_capacity);
173178
new_block->next = arena->head;
174179
arena->head = new_block;

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)