Permalink
Browse files

Cache CRLF grapheme.

Saves us resolving it every single time. This, on a read a million
lines benchmark in Perl 6, saves 158 million cycles.
  • Loading branch information...
jnthn committed Jun 16, 2017
1 parent 0a83384 commit 5814822f1319569bce60ba586a1fef62334521ed
Showing with 20 additions and 16 deletions.
  1. +0 −3 src/moar.c
  2. +17 −13 src/strings/nfg.c
  3. +3 −0 src/strings/nfg.h
View
@@ -134,9 +134,6 @@ MVMInstance * MVM_vm_create_instance(void) {
MVM_string_cclass_init(instance->main_thread);
MVM_nfg_init(instance->main_thread);
instance->nfg = calloc(1, sizeof(MVMNFGState));
init_mutex(instance->nfg->update_mutex, "NFG update mutex");
/* Bootstrap 6model. It is assumed the GC will not be called during this. */
MVM_6model_bootstrap(instance->main_thread);
View
@@ -4,17 +4,6 @@
* to grow it. */
#define MVM_SYNTHETIC_GROW_ELEMS 32
/* Initialize NFG subsystem. */
void MVM_nfg_init(MVMThreadContext *tc) {
int init_stat;
tc->instance->nfg = calloc(1, sizeof(MVMNFGState));
if ((init_stat = uv_mutex_init(&(tc->instance->nfg->update_mutex))) < 0) {
fprintf(stderr, "MoarVM: Initialization of NFG update mutex failed\n %s\n",
uv_strerror(init_stat));
exit(1);
}
}
/* Finds the index of a given codepoint within a trie node. Returns it if
* there is one, or negative if there is not (note 0 is a valid index). */
static MVMint32 find_child_node_idx(MVMThreadContext *tc, const MVMNFGTrieNode *node, MVMCodepoint cp) {
@@ -241,8 +230,7 @@ MVMGrapheme32 MVM_nfg_codes_to_grapheme_utf8_c8(MVMThreadContext *tc, MVMCodepoi
/* Gets the \r\n synthetic. */
MVMGrapheme32 MVM_nfg_crlf_grapheme(MVMThreadContext *tc) {
MVMCodepoint codes[2] = { '\r', '\n' };
return lookup_or_add_synthetic(tc, codes, 2, 0);
return tc->instance->nfg->crlf_grapheme;
}
/* Does a lookup of information held about a synthetic. The synth parameter
@@ -401,6 +389,22 @@ MVMint32 MVM_nfg_is_concat_stable(MVMThreadContext *tc, MVMString *a, MVMString
&& (first_b == crlf || codepoint_GCB_Control(tc, first_b) || passes_quickcheck_and_zero_ccc(tc, first_b));
}
/* Initialize NFG subsystem. */
static void cache_crlf(MVMThreadContext *tc) {
MVMCodepoint codes[2] = { '\r', '\n' };
tc->instance->nfg->crlf_grapheme = lookup_or_add_synthetic(tc, codes, 2, 0);
}
void MVM_nfg_init(MVMThreadContext *tc) {
int init_stat;
tc->instance->nfg = calloc(1, sizeof(MVMNFGState));
if ((init_stat = uv_mutex_init(&(tc->instance->nfg->update_mutex))) < 0) {
fprintf(stderr, "MoarVM: Initialization of NFG update mutex failed\n %s\n",
uv_strerror(init_stat));
exit(1);
}
cache_crlf(tc);
}
/* Free all memory allocated to hold synthetic graphemes. These are global
* to a VM instance. */
void MVM_nfg_destroy(MVMThreadContext *tc) {
View
@@ -19,6 +19,9 @@ struct MVMNFGState {
/* Number of synthetics we have. */
MVMint32 num_synthetics;
/* Cached CRLF grapheme index, since we need it so often. */
MVMGrapheme32 crlf_grapheme;
};
/* State held about a synthetic. */

0 comments on commit 5814822

Please sign in to comment.