Move the if condition out of the inner loops

This commit is contained in:
Kovid Goyal 2021-06-27 21:07:07 +05:30
parent 691b7215a0
commit ce486e9244
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -25,9 +25,9 @@ typedef struct MatchingCodepoints {
} MatchingCodepoints; } MatchingCodepoints;
static bool static bool
ensure_space(MatchingCodepoints *ans) { ensure_space(MatchingCodepoints *ans, size_t num) {
if (ans->capacity > ans->pos + 1) return true; if (ans->capacity > ans->pos + num) return true;
ans->capacity = MAX(1024u, ans->pos + 256); ans->capacity = MAX(1024u, ans->pos + num + 8);
ans->codepoints = realloc(ans->codepoints, sizeof(ans->codepoints[0]) * ans->capacity); ans->codepoints = realloc(ans->codepoints, sizeof(ans->codepoints[0]) * ans->capacity);
if (ans->codepoints) return true; if (ans->codepoints) return true;
PyErr_NoMemory(); PyErr_NoMemory();
@ -37,8 +37,8 @@ ensure_space(MatchingCodepoints *ans) {
static void static void
add_matches(const word_trie *wt, MatchingCodepoints *ans) { add_matches(const word_trie *wt, MatchingCodepoints *ans) {
size_t num = mark_groups[wt->match_offset]; size_t num = mark_groups[wt->match_offset];
if (!ensure_space(ans, num)) return;
for (size_t i = wt->match_offset + 1; i < wt->match_offset + 1 + num; i++) { for (size_t i = wt->match_offset + 1; i < wt->match_offset + 1 + num; i++) {
if (!ensure_space(ans)) return;
ans->codepoints[ans->pos++] = mark_to_cp[mark_groups[i]]; ans->codepoints[ans->pos++] = mark_to_cp[mark_groups[i]];
} }
} }
@ -48,8 +48,8 @@ process_trie_node(const word_trie *wt, MatchingCodepoints *ans) {
if (wt->match_offset) add_matches(wt, ans); if (wt->match_offset) add_matches(wt, ans);
size_t num_children = children_array[wt->children_offset]; size_t num_children = children_array[wt->children_offset];
if (!num_children) return; if (!num_children) return;
if (!ensure_space(ans, num_children)) return;
for (size_t c = wt->children_offset + 1; c < wt->children_offset + 1 + num_children; c++) { for (size_t c = wt->children_offset + 1; c < wt->children_offset + 1 + num_children; c++) {
if (!ensure_space(ans)) return;
uint32_t x = children_array[c]; uint32_t x = children_array[c];
process_trie_node(&all_trie_nodes[x >> 8], ans); process_trie_node(&all_trie_nodes[x >> 8], ans);
} }