Skip to content

Commit fbcd3fc

Browse files
committed
Inline three more functions, and lower the hash threshold for locals
1 parent b5b88ba commit fbcd3fc

5 files changed

Lines changed: 43 additions & 31 deletions

File tree

include/prism/util/pm_char.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ pm_char_is_inline_whitespace(const uint8_t b) {
4646
return (pm_byte_table[b] & PRISM_CHAR_BIT_INLINE_WHITESPACE) != 0;
4747
}
4848

49+
/**
50+
* Returns the number of characters at the start of the string that are inline
51+
* whitespace (space/tab). Scans the byte table directly for use in hot paths.
52+
*
53+
* @param string The string to search.
54+
* @param length The maximum number of characters to search.
55+
* @return The number of characters at the start of the string that are inline
56+
* whitespace.
57+
*/
58+
static PRISM_FORCE_INLINE size_t
59+
pm_strspn_inline_whitespace(const uint8_t *string, ptrdiff_t length) {
60+
if (length <= 0) return 0;
61+
size_t size = 0;
62+
size_t maximum = (size_t) length;
63+
while (size < maximum && (pm_byte_table[string[size]] & PRISM_CHAR_BIT_INLINE_WHITESPACE)) size++;
64+
return size;
65+
}
66+
4967
/**
5068
* Returns the number of characters at the start of the string that are
5169
* whitespace. Disallows searching past the given maximum number of characters.
@@ -73,17 +91,6 @@ size_t pm_strspn_whitespace(const uint8_t *string, ptrdiff_t length);
7391
*/
7492
size_t pm_strspn_whitespace_newlines(const uint8_t *string, ptrdiff_t length, pm_arena_t *arena, pm_line_offset_list_t *line_offsets, uint32_t start_offset);
7593

76-
/**
77-
* Returns the number of characters at the start of the string that are inline
78-
* whitespace. Disallows searching past the given maximum number of characters.
79-
*
80-
* @param string The string to search.
81-
* @param length The maximum number of characters to search.
82-
* @return The number of characters at the start of the string that are inline
83-
* whitespace.
84-
*/
85-
size_t pm_strspn_inline_whitespace(const uint8_t *string, ptrdiff_t length);
86-
8794
/**
8895
* Returns the number of characters at the start of the string that are decimal
8996
* digits. Disallows searching past the given maximum number of characters.

include/prism/util/pm_line_offset_list.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,30 @@ void pm_line_offset_list_init(pm_arena_t *arena, pm_line_offset_list_t *list, si
6464
*/
6565
void pm_line_offset_list_clear(pm_line_offset_list_t *list);
6666

67+
/**
68+
* Append a new offset to the list (slow path with resize).
69+
*
70+
* @param arena The arena to allocate from.
71+
* @param list The list to append to.
72+
* @param cursor The offset to append.
73+
*/
74+
void pm_line_offset_list_append_slow(pm_arena_t *arena, pm_line_offset_list_t *list, uint32_t cursor);
75+
6776
/**
6877
* Append a new offset to the list.
6978
*
7079
* @param arena The arena to allocate from.
7180
* @param list The list to append to.
7281
* @param cursor The offset to append.
7382
*/
74-
void pm_line_offset_list_append(pm_arena_t *arena, pm_line_offset_list_t *list, uint32_t cursor);
83+
static PRISM_FORCE_INLINE void
84+
pm_line_offset_list_append(pm_arena_t *arena, pm_line_offset_list_t *list, uint32_t cursor) {
85+
if (list->size < list->capacity) {
86+
list->offsets[list->size++] = cursor;
87+
} else {
88+
pm_line_offset_list_append_slow(arena, list, cursor);
89+
}
90+
}
7591

7692
/**
7793
* Returns the line of the given offset. If the offset is not in the list, the

src/prism.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ pm_parser_scope_shareable_constant_set(pm_parser_t *parser, pm_shareable_constan
773773
/**
774774
* The point at which the set of locals switches from being a list to a hash.
775775
*/
776-
#define PM_LOCALS_HASH_THRESHOLD 9
776+
#define PM_LOCALS_HASH_THRESHOLD 5
777777

778778
static void
779779
pm_locals_free(pm_locals_t *locals) {

src/util/pm_char.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,6 @@ pm_strspn_whitespace_newlines(const uint8_t *string, ptrdiff_t length, pm_arena_
9898
return size;
9999
}
100100

101-
/**
102-
* Returns the number of characters at the start of the string that are inline
103-
* whitespace. Disallows searching past the given maximum number of characters.
104-
*/
105-
size_t
106-
pm_strspn_inline_whitespace(const uint8_t *string, ptrdiff_t length) {
107-
return pm_strspn_char_kind(string, length, PRISM_CHAR_BIT_INLINE_WHITESPACE);
108-
}
109-
110101
/**
111102
* Returns the number of characters at the start of the string that are regexp
112103
* options. Disallows searching past the given maximum number of characters.

src/util/pm_line_offset_list.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,17 @@ pm_line_offset_list_clear(pm_line_offset_list_t *list) {
2222
}
2323

2424
/**
25-
* Append a new offset to the newline list.
25+
* Append a new offset to the newline list (slow path: resize and store).
2626
*/
2727
void
28-
pm_line_offset_list_append(pm_arena_t *arena, pm_line_offset_list_t *list, uint32_t cursor) {
29-
if (list->size == list->capacity) {
30-
size_t new_capacity = (list->capacity * 3) / 2;
31-
uint32_t *new_offsets = (uint32_t *) pm_arena_alloc(arena, new_capacity * sizeof(uint32_t), PRISM_ALIGNOF(uint32_t));
28+
pm_line_offset_list_append_slow(pm_arena_t *arena, pm_line_offset_list_t *list, uint32_t cursor) {
29+
size_t new_capacity = (list->capacity * 3) / 2;
30+
uint32_t *new_offsets = (uint32_t *) pm_arena_alloc(arena, new_capacity * sizeof(uint32_t), PRISM_ALIGNOF(uint32_t));
3231

33-
memcpy(new_offsets, list->offsets, list->size * sizeof(uint32_t));
32+
memcpy(new_offsets, list->offsets, list->size * sizeof(uint32_t));
3433

35-
list->offsets = new_offsets;
36-
list->capacity = new_capacity;
37-
}
34+
list->offsets = new_offsets;
35+
list->capacity = new_capacity;
3836

3937
assert(list->size == 0 || cursor > list->offsets[list->size - 1]);
4038
list->offsets[list->size++] = cursor;

0 commit comments

Comments
 (0)