From 8bc9d4b42785425071f87dedf2c0c04524309941 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Sat, 5 Jan 2013 19:09:36 +0100 Subject: [PATCH 01/25] Committing an evil allocator and a trie to speed up the correction stuff --- correct.c | 204 +++++++++++++++++++++++++++++++++++++++++++++--------- gmqcc.h | 14 +++- main.c | 2 +- parser.c | 6 +- 4 files changed, 187 insertions(+), 39 deletions(-) diff --git a/correct.c b/correct.c index 3a4f5c0..48dbe49 100644 --- a/correct.c +++ b/correct.c @@ -22,6 +22,133 @@ */ #include "gmqcc.h" +unsigned char **pools; +unsigned char *poolptr; +size_t poolat; +#define C_POOLSIZE (128*1024*1024) + +static GMQCC_INLINE void newpool() { + poolat = 0; + poolptr = mem_a(C_POOLSIZE); + vec_push(pools, poolptr); +} + +void correct_init(void) { + newpool(); +} + +static GMQCC_INLINE void *correct_alloc(size_t _b) { + size_t *ptr; + size_t b = _b + sizeof(size_t); + if (poolat + b >= C_POOLSIZE) + newpool(); + poolat += b; + ptr = (size_t*)poolptr; + poolptr += b; + *ptr = _b; + return (void*)(ptr+1); +} + +static GMQCC_INLINE void *correct_realloc(void *_ptr, size_t _b) { + size_t *ptr = ((size_t*)_ptr) - 1; + size_t oldlen = *ptr; + size_t len = (oldlen < _b ? oldlen : _b); + size_t b = _b + sizeof(size_t); + size_t *newptr; + + newptr = (size_t*)correct_alloc(b); + *newptr++ = _b; + memcpy(newptr, _ptr, len); + return (void*)(newptr); +} + +void correct_delete(void) { + size_t i; + for (i = 0; i < vec_size(pools); ++i) + mem_d(pools[i]); + pools = NULL; + poolptr = NULL; + poolat = 0; +} + +static GMQCC_INLINE char *correct_outstr(const char *s) { + char *o = util_strdup(s); + correct_delete(); + return o; +} + +correct_trie_t* correct_trie_new() +{ + correct_trie_t *t = (correct_trie_t*)mem_a(sizeof(correct_trie_t)); + t->value = NULL; + t->entries = NULL; + return t; +} + +void correct_trie_del_sub(correct_trie_t *t) +{ + size_t i; + for (i = 0; i < vec_size(t->entries); ++i) + correct_trie_del_sub(&t->entries[i]); + vec_free(t->entries); +} + +void correct_trie_del(correct_trie_t *t) +{ + size_t i; + for (i = 0; i < vec_size(t->entries); ++i) + correct_trie_del_sub(&t->entries[i]); + vec_free(t->entries); + mem_d(t); +} + +void* correct_trie_get(const correct_trie_t *t, const char *key) +{ + const unsigned char *data = (const unsigned char*)key; + while (*data) { + unsigned char ch = *data; + const size_t vs = vec_size(t->entries); + size_t i; + const correct_trie_t *entries = t->entries; + for (i = 0; i < vs; ++i) { + if (entries[i].ch == ch) { + t = &entries[i]; + ++data; + break; + } + } + if (i == vs) + return NULL; + } + return t->value; +} + +void correct_trie_set(correct_trie_t *t, const char *key, void * const value) +{ + const unsigned char *data = (const unsigned char*)key; + while (*data) { + unsigned char ch = *data; + correct_trie_t *entries = t->entries; + const size_t vs = vec_size(t->entries); + size_t i; + for (i = 0; i < vs; ++i) { + if (entries[i].ch == ch) { + t = &entries[i]; + break; + } + } + if (i == vs) { + correct_trie_t *elem = (correct_trie_t*)vec_add(t->entries, 1); + elem->ch = ch; + elem->value = NULL; + elem->entries = NULL; + t = elem; + } + ++data; + } + t->value = value; +} + /* * This is a very clever method for correcting mistakes in QuakeC code * most notably when invalid identifiers are used or inproper assignments; @@ -73,11 +200,11 @@ */ /* some hashtable management for dictonaries */ -static size_t *correct_find(ht table, const char *word) { - return (size_t*)util_htget(table, word); +static size_t *correct_find(correct_trie_t *table, const char *word) { + return (size_t*)correct_trie_get(table, word); } -static int correct_update(ht *table, const char *word) { +static int correct_update(correct_trie_t* *table, const char *word) { size_t *data = correct_find(*table, word); if (!data) return 0; @@ -86,6 +213,38 @@ static int correct_update(ht *table, const char *word) { return 1; } +void correct_add(correct_trie_t* table, size_t ***size, const char *ident) { + size_t *data = NULL; + const char *add = ident; + + if (!correct_update(&table, add)) { + data = (size_t*)mem_a(sizeof(size_t)); + *data = 1; + + vec_push((*size), data); + correct_trie_set(table, add, data); + } +} + +void correct_del(correct_trie_t* dictonary, size_t **data) { + size_t i; + const size_t vs = vec_size(data); + for (i = 0; i < vs; i++) + mem_d(data[i]); + + vec_free(data); + correct_trie_del(dictonary); +} +#if 1 +#undef mem_a +#undef mem_r +#undef mem_d +#define mem_a(x) correct_alloc((x)) +#define mem_r(a,b) correct_realloc((a),(b)) +/* doing this in order to avoid 'unused variable' warnings */ +#define mem_d(x) ((void)(0 && (x))) +#endif + /* * _ is valid in identifiers. I've yet to implement numerics however @@ -154,14 +313,14 @@ static size_t correct_insertion(const char *ident, char **array, size_t index) { size_t itr; size_t jtr; size_t ktr; - size_t len = strlen(ident); + const size_t len = strlen(ident); for (itr = 0, ktr = 0; itr <= len; itr++) { for (jtr = 0; jtr < sizeof(correct_alpha)-1; jtr++, ktr++) { char *a = (char*)mem_a(len+2); memcpy(a, ident, itr); - a[itr] = correct_alpha[jtr]; memcpy(a + itr + 1, ident + itr, len - itr + 1); + a[itr] = correct_alpha[jtr]; array[index + ktr] = a; } } @@ -210,7 +369,7 @@ static int correct_exist(char **array, size_t rows, char *ident) { return 0; } -static char **correct_known(ht table, char **array, size_t rows, size_t *next) { +static char **correct_known(correct_trie_t* table, char **array, size_t rows, size_t *next) { size_t itr; size_t jtr; size_t len; @@ -238,7 +397,7 @@ static char **correct_known(ht table, char **array, size_t rows, size_t *next) { return res; } -static char *correct_maximum(ht table, char **array, size_t rows) { +static char *correct_maximum(correct_trie_t* table, char **array, size_t rows) { char *str = NULL; size_t *itm = NULL; size_t itr; @@ -270,20 +429,8 @@ static void correct_cleanup(char **array, size_t rows) { * the add function works the same. Except the identifier is used to * add to the dictonary. */ -void correct_add(ht table, size_t ***size, const char *ident) { - size_t *data = NULL; - const char *add = ident; - - if (!correct_update(&table, add)) { - data = (size_t*)mem_a(sizeof(size_t)); - *data = 1; - vec_push((*size), data); - util_htset(table, add, data); - } -} - -char *correct_str(ht table, const char *ident) { +char *correct_str(correct_trie_t* table, const char *ident) { char **e1; char **e2; char *e1ident; @@ -293,9 +440,11 @@ char *correct_str(ht table, const char *ident) { size_t e1rows = 0; size_t e2rows = 0; + correct_init(); + /* needs to be allocated for free later */ if (correct_find(table, ident)) - return found; + return correct_outstr(found); if ((e1rows = correct_size(ident))) { e1 = correct_edit(ident); @@ -304,7 +453,7 @@ char *correct_str(ht table, const char *ident) { mem_d(found); found = util_strdup(e1ident); correct_cleanup(e1, e1rows); - return found; + return correct_outstr(found); } } @@ -316,15 +465,6 @@ char *correct_str(ht table, const char *ident) { correct_cleanup(e1, e1rows); correct_cleanup(e2, e2rows); - - return found; -} -void correct_del(ht dictonary, size_t **data) { - size_t i; - for (i = 0; i < vec_size(data); i++) - mem_d(data[i]); - - vec_free(data); - util_htdel(dictonary); + return correct_outstr(found); } diff --git a/gmqcc.h b/gmqcc.h index 036cab9..11c3a3e 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -315,6 +315,14 @@ void _util_vec_grow(void **a, size_t i, size_t s); #define vec_upload(X,Y,S) memcpy(vec_add((X), (S) * sizeof(*(Y))), (Y), (S) * sizeof(*(Y))) #define vec_remove(A,I,N) memmove((A)+(I),(A)+((I)+(N)),sizeof(*(A))*(vec_meta(A)->used-(I)-(N))),vec_meta(A)->used-=(N) +typedef struct trie_s { + struct trie_s *entries; + unsigned char ch; + void *value; +} correct_trie_t; + +correct_trie_t* correct_trie_new(); + typedef struct hash_table_t { size_t size; struct hash_node_t **table; @@ -426,9 +434,9 @@ GMQCC_INLINE FILE *file_open (const char *, const char *); /*===================================================================*/ /*=========================== correct.c =============================*/ /*===================================================================*/ -void correct_del(ht, size_t **); -void correct_add(ht, size_t ***, const char *); -char *correct_str(ht, /********/ const char *); +void correct_del(correct_trie_t*, size_t **); +void correct_add(correct_trie_t*, size_t ***, const char *); +char *correct_str(correct_trie_t*, /********/ const char *); /*===================================================================*/ /*=========================== code.c ================================*/ diff --git a/main.c b/main.c index 7fc176a..850ce0e 100644 --- a/main.c +++ b/main.c @@ -570,7 +570,7 @@ int main(int argc, char **argv) { con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr)); for (itr = 0; itr < COUNT_WARNINGS; ++itr) con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr)); - + con_out("output = %s\n", opts.output); con_out("optimization level = %d\n", opts.O); con_out("standard = %i\n", opts.standard); diff --git a/parser.c b/parser.c index e93c792..aa3c38e 100644 --- a/parser.c +++ b/parser.c @@ -75,8 +75,8 @@ typedef struct { ht *typedefs; /* same as above but for the spelling corrector */ - ht *correct_variables; - size_t ***correct_variables_score; /* vector of vector of size_t* */ + correct_trie_t **correct_variables; + size_t ***correct_variables_score; /* vector of vector of size_t* */ /* not to be used directly, we use the hash table */ ast_expression **_locals; @@ -1999,7 +1999,7 @@ static void parser_enterblock(parser_t *parser) vec_push(parser->_block_ctx, parser_ctx(parser)); /* corrector */ - vec_push(parser->correct_variables, util_htnew(PARSER_HT_SIZE)); + vec_push(parser->correct_variables, correct_trie_new()); vec_push(parser->correct_variables_score, NULL); } From 0fb089fbb712bbff212dad460fd85c917d8be345 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 02:39:07 +0000 Subject: [PATCH 02/25] Cleanup the pool system for correct.c. Use blubs correct_resize stuff. --- correct.c | 125 +++++++++++++++++++++++++----------------------------- 1 file changed, 58 insertions(+), 67 deletions(-) diff --git a/correct.c b/correct.c index 48dbe49..12884e8 100644 --- a/correct.c +++ b/correct.c @@ -22,58 +22,52 @@ */ #include "gmqcc.h" -unsigned char **pools; -unsigned char *poolptr; -size_t poolat; -#define C_POOLSIZE (128*1024*1024) +/* + * A forward allcator for the corrector. This corrector requires a lot + * of allocations. This forward allocator combats all those allocations + * and speeds us up a little. It also saves us space in a way since each + * allocation isn't wasting a little header space for when NOTRACK isn't + * defined. + */ +#define CORRECT_POOLSIZE (128*1024*1024) -static GMQCC_INLINE void newpool() { - poolat = 0; - poolptr = mem_a(C_POOLSIZE); - vec_push(pools, poolptr); +static unsigned char **correct_pool_data = NULL; +static unsigned char *correct_pool_this = NULL; +static size_t correct_pool_addr = 0; + +static GMQCC_INLINE void correct_pool_new(void) { + correct_pool_addr = 0; + correct_pool_this = (unsigned char *)mem_a(CORRECT_POOLSIZE); + + vec_push(correct_pool_data, correct_pool_this); } -void correct_init(void) { - newpool(); +static GMQCC_INLINE void *correct_pool_alloc(size_t bytes) { + void *data; + if (correct_pool_addr + bytes >= CORRECT_POOLSIZE) + correct_pool_new(); + + data = correct_pool_this; + correct_pool_this += bytes; + correct_pool_addr += bytes; + + return data; } -static GMQCC_INLINE void *correct_alloc(size_t _b) { - size_t *ptr; - size_t b = _b + sizeof(size_t); - if (poolat + b >= C_POOLSIZE) - newpool(); - poolat += b; - ptr = (size_t*)poolptr; - poolptr += b; - *ptr = _b; - return (void*)(ptr+1); -} - -static GMQCC_INLINE void *correct_realloc(void *_ptr, size_t _b) { - size_t *ptr = ((size_t*)_ptr) - 1; - size_t oldlen = *ptr; - size_t len = (oldlen < _b ? oldlen : _b); - size_t b = _b + sizeof(size_t); - size_t *newptr; - - newptr = (size_t*)correct_alloc(b); - *newptr++ = _b; - memcpy(newptr, _ptr, len); - return (void*)(newptr); -} - -void correct_delete(void) { +static GMQCC_INLINE void correct_pool_delete(void) { size_t i; - for (i = 0; i < vec_size(pools); ++i) - mem_d(pools[i]); - pools = NULL; - poolptr = NULL; - poolat = 0; + for (i = 0; i < vec_size(correct_pool_data); ++i) + mem_d(correct_pool_data[i]); + + correct_pool_data = NULL; + correct_pool_this = NULL; + correct_pool_addr = 0; } + static GMQCC_INLINE char *correct_outstr(const char *s) { char *o = util_strdup(s); - correct_delete(); + correct_pool_delete(); return o; } @@ -265,7 +259,7 @@ static size_t correct_deletion(const char *ident, char **array, size_t index) { size_t len = strlen(ident); for (itr = 0; itr < len; itr++) { - char *a = (char*)mem_a(len+1); + char *a = (char*)correct_pool_alloc(len+1); memcpy(a, ident, itr); memcpy(a + itr, ident + itr + 1, len - itr); array[index + itr] = a; @@ -280,7 +274,7 @@ static size_t correct_transposition(const char *ident, char **array, size_t inde for (itr = 0; itr < len - 1; itr++) { char tmp; - char *a = (char*)mem_a(len+1); + char *a = (char*)correct_pool_alloc(len+1); memcpy(a, ident, len+1); tmp = a[itr]; a[itr ] = a[itr+1]; @@ -299,7 +293,7 @@ static size_t correct_alteration(const char *ident, char **array, size_t index) for (itr = 0, ktr = 0; itr < len; itr++) { for (jtr = 0; jtr < sizeof(correct_alpha)-1; jtr++, ktr++) { - char *a = (char*)mem_a(len+1); + char *a = (char*)correct_pool_alloc(len+1); memcpy(a, ident, len+1); a[itr] = correct_alpha[jtr]; array[index + ktr] = a; @@ -317,7 +311,7 @@ static size_t correct_insertion(const char *ident, char **array, size_t index) { for (itr = 0, ktr = 0; itr <= len; itr++) { for (jtr = 0; jtr < sizeof(correct_alpha)-1; jtr++, ktr++) { - char *a = (char*)mem_a(len+2); + char *a = (char*)correct_pool_alloc(len+2); memcpy(a, ident, itr); memcpy(a + itr + 1, ident + itr, len - itr + 1); a[itr] = correct_alpha[jtr]; @@ -342,7 +336,7 @@ static GMQCC_INLINE size_t correct_size(const char *ident) { static char **correct_edit(const char *ident) { size_t next; - char **find = (char**)mem_a(correct_size(ident) * sizeof(char*)); + char **find = (char**)correct_pool_alloc(correct_size(ident) * sizeof(char*)); if (!find) return NULL; @@ -369,13 +363,26 @@ static int correct_exist(char **array, size_t rows, char *ident) { return 0; } +static GMQCC_INLINE char **correct_known_resize(char **res, size_t *allocated, size_t size) { + size_t oldallocated = *allocated; + char **out; + if (size+1 < *allocated) + return res; + + *allocated += 32; + out = correct_pool_alloc(sizeof(*res) * *allocated); + memcpy(out, res, sizeof(*res) * oldallocated); + return out; +} + static char **correct_known(correct_trie_t* table, char **array, size_t rows, size_t *next) { size_t itr; size_t jtr; size_t len; size_t row; - char **res = NULL; - char **end; + size_t nxt = 8; + char **res = correct_pool_alloc(sizeof(char *) * nxt); + char **end = NULL; for (itr = 0, len = 0; itr < rows; itr++) { end = correct_edit(array[itr]); @@ -383,14 +390,12 @@ static char **correct_known(correct_trie_t* table, char **array, size_t rows, si for (jtr = 0; jtr < row; jtr++) { if (correct_find(table, end[jtr]) && !correct_exist(res, len, end[jtr])) { - res = mem_r(res, sizeof(char*) * (len + 1)); + res = correct_known_resize(res, &nxt, len+1); res[len++] = end[jtr]; } else { mem_d(end[jtr]); } } - - mem_d(end); } *next = len; @@ -413,14 +418,6 @@ static char *correct_maximum(correct_trie_t* table, char **array, size_t rows) { return str; } -static void correct_cleanup(char **array, size_t rows) { - size_t itr; - for (itr = 0; itr < rows; itr++) - mem_d(array[itr]); - - mem_d(array); -} - /* * This is the exposed interface: * takes a table for the dictonary a vector of sizes (used for internal @@ -440,7 +437,7 @@ char *correct_str(correct_trie_t* table, const char *ident) { size_t e1rows = 0; size_t e2rows = 0; - correct_init(); + correct_pool_new(); /* needs to be allocated for free later */ if (correct_find(table, ident)) @@ -450,21 +447,15 @@ char *correct_str(correct_trie_t* table, const char *ident) { e1 = correct_edit(ident); if ((e1ident = correct_maximum(table, e1, e1rows))) { - mem_d(found); found = util_strdup(e1ident); - correct_cleanup(e1, e1rows); return correct_outstr(found); } } e2 = correct_known(table, e1, e1rows, &e2rows); if (e2rows && ((e2ident = correct_maximum(table, e2, e2rows)))) { - mem_d(found); found = util_strdup(e2ident); } - - correct_cleanup(e1, e1rows); - correct_cleanup(e2, e2rows); return correct_outstr(found); } From efecd160ca0c721d7e491eb506b5efadc7169b32 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 02:40:36 +0000 Subject: [PATCH 03/25] Remove an illegal mem_d --- correct.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/correct.c b/correct.c index 12884e8..34f32b4 100644 --- a/correct.c +++ b/correct.c @@ -392,8 +392,6 @@ static char **correct_known(correct_trie_t* table, char **array, size_t rows, si if (correct_find(table, end[jtr]) && !correct_exist(res, len, end[jtr])) { res = correct_known_resize(res, &nxt, len+1); res[len++] = end[jtr]; - } else { - mem_d(end[jtr]); } } } From 294870c5baa192f9452c00adb585d363d173a7b7 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 02:41:15 +0000 Subject: [PATCH 04/25] Remove override macros --- correct.c | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/correct.c b/correct.c index 34f32b4..494d9b8 100644 --- a/correct.c +++ b/correct.c @@ -229,16 +229,6 @@ void correct_del(correct_trie_t* dictonary, size_t **data) { vec_free(data); correct_trie_del(dictonary); } -#if 1 -#undef mem_a -#undef mem_r -#undef mem_d -#define mem_a(x) correct_alloc((x)) -#define mem_r(a,b) correct_realloc((a),(b)) -/* doing this in order to avoid 'unused variable' warnings */ -#define mem_d(x) ((void)(0 && (x))) -#endif - /* * _ is valid in identifiers. I've yet to implement numerics however From 7e87c61d78b3c4b633f7afab1a48258e02ed54d5 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 03:06:56 +0000 Subject: [PATCH 05/25] Cleanups and documentation --- correct.c | 174 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 99 insertions(+), 75 deletions(-) diff --git a/correct.c b/correct.c index 494d9b8..8e5c759 100644 --- a/correct.c +++ b/correct.c @@ -22,6 +22,77 @@ */ #include "gmqcc.h" +/* + * This is a very clever method for correcting mistakes in QuakeC code + * most notably when invalid identifiers are used or inproper assignments; + * we can proprly lookup in multiple dictonaries (depening on the rules + * of what the task is trying to acomplish) to find the best possible + * match. + * + * + * A little about how it works, and probability theory: + * + * When given an identifier (which we will denote I), we're essentially + * just trying to choose the most likely correction for that identifier. + * (the actual "correction" can very well be the identifier itself). + * There is actually no way to know for sure that certian identifers + * such as "lates", need to be corrected to "late" or "latest" or any + * other permutations that look lexically the same. This is why we + * must advocate the usage of probabilities. This means that instead of + * just guessing, instead we're trying to find the correction for C, + * out of all possible corrections that maximizes the probability of C + * for the original identifer I. + * + * Bayes' Therom suggests something of the following: + * AC P(I|C) P(C) / P(I) + * Since P(I) is the same for every possibly I, we can ignore it giving + * AC P(I|C) P(C) + * + * This greatly helps visualize how the parts of the expression are performed + * there is essentially three, from right to left we perform the following: + * + * 1: P(C), the probability that a proposed correction C will stand on its + * own. This is called the language model. + * + * 2: P(I|C), the probability that I would be used, when the programmer + * really meant C. This is the error model. + * + * 3: AC, the control mechanisim, an enumerator if you will, one that + * enumerates all feasible values of C, to determine the one that + * gives the greatest probability score. + * + * In reality the requirement for a more complex expression involving + * two seperate models is considerably a waste. But one must recognize + * that P(C|I) is already conflating two factors. It's just much simpler + * to seperate the two models and deal with them explicitaly. To properly + * estimate P(C|I) you have to consider both the probability of C and + * probability of the transposition from C to I. It's simply much more + * cleaner, and direct to seperate the two factors. + * + * A little information on additional algorithms used: + * + * Initially when I implemented this corrector, it was very slow. + * Need I remind you this is essentially a brute force attack on strings, + * and since every transformation requires dynamic memory allocations, + * you can easily imagine where most of the runtime conflated. Yes + * It went right to malloc. More than THREE MILLION malloc calls are + * performed for an identifier about 16 bytes long. This was such a + * shock to me. A forward allocator (or as some call it a bump-point + * allocator, or just a memory pool) was implemented. To combat this. + * + * But of course even other factors were making it slow. Initially + * this used a hashtable. And hashtables have a good constant lookup + * time complexity. But the problem wasn't in the hashtable, it was + * in the hashing (despite having one of the fastest hash functions + * known). Remember those 3 million mallocs? Well for every malloc + * there is also a hash. After 3 million hashes .. you start to get + * very slow. To combat this I had suggested burst tries to Blub. + * The next day he had implemented them. Sure enough this brought + * down the runtime by a factory > 100% + */ + + +#define CORRECT_POOLSIZE (128*1024*1024) /* * A forward allcator for the corrector. This corrector requires a lot * of allocations. This forward allocator combats all those allocations @@ -29,8 +100,6 @@ * allocation isn't wasting a little header space for when NOTRACK isn't * defined. */ -#define CORRECT_POOLSIZE (128*1024*1024) - static unsigned char **correct_pool_data = NULL; static unsigned char *correct_pool_this = NULL; static size_t correct_pool_addr = 0; @@ -65,30 +134,35 @@ static GMQCC_INLINE void correct_pool_delete(void) { } -static GMQCC_INLINE char *correct_outstr(const char *s) { - char *o = util_strdup(s); +static GMQCC_INLINE char *correct_pool_claim(const char *data) { + char *claim = util_strdup(data); correct_pool_delete(); - return o; + return claim; } -correct_trie_t* correct_trie_new() -{ +/* + * A fast space efficent trie for a disctonary of identifiers. This is + * faster than a hashtable for one reason. A hashtable itself may have + * fast constant lookup time, but the hash itself must be very fast. We + * have one of the fastest hash functions for strings, but if you do a + * lost of hashing (which we do, almost 3 million hashes per identifier) + * a hashtable becomes slow. Very Very Slow. + */ +correct_trie_t* correct_trie_new() { correct_trie_t *t = (correct_trie_t*)mem_a(sizeof(correct_trie_t)); t->value = NULL; t->entries = NULL; return t; } -void correct_trie_del_sub(correct_trie_t *t) -{ +void correct_trie_del_sub(correct_trie_t *t) { size_t i; for (i = 0; i < vec_size(t->entries); ++i) correct_trie_del_sub(&t->entries[i]); vec_free(t->entries); } -void correct_trie_del(correct_trie_t *t) -{ +void correct_trie_del(correct_trie_t *t) { size_t i; for (i = 0; i < vec_size(t->entries); ++i) correct_trie_del_sub(&t->entries[i]); @@ -96,8 +170,7 @@ void correct_trie_del(correct_trie_t *t) mem_d(t); } -void* correct_trie_get(const correct_trie_t *t, const char *key) -{ +void* correct_trie_get(const correct_trie_t *t, const char *key) { const unsigned char *data = (const unsigned char*)key; while (*data) { unsigned char ch = *data; @@ -117,8 +190,7 @@ void* correct_trie_get(const correct_trie_t *t, const char *key) return t->value; } -void correct_trie_set(correct_trie_t *t, const char *key, void * const value) -{ +void correct_trie_set(correct_trie_t *t, const char *key, void * const value) { const unsigned char *data = (const unsigned char*)key; while (*data) { unsigned char ch = *data; @@ -143,57 +215,11 @@ void correct_trie_set(correct_trie_t *t, const char *key, void * const value) t->value = value; } -/* - * This is a very clever method for correcting mistakes in QuakeC code - * most notably when invalid identifiers are used or inproper assignments; - * we can proprly lookup in multiple dictonaries (depening on the rules - * of what the task is trying to acomplish) to find the best possible - * match. - * - * - * A little about how it works, and probability theory: - * - * When given an identifier (which we will denote I), we're essentially - * just trying to choose the most likely correction for that identifier. - * (the actual "correction" can very well be the identifier itself). - * There is actually no way to know for sure that certian identifers - * such as "lates", need to be corrected to "late" or "latest" or any - * other permutations that look lexically the same. This is why we - * must advocate the usage of probabilities. This implies that we're - * trying to find the correction for C, out of all possible corrections - * that maximizes the probability of C for the original identifer I. - * - * Bayes' Therom suggests something of the following: - * AC P(I|C) P(C) / P(I) - * Since P(I) is the same for every possibly I, we can ignore it giving - * AC P(I|C) P(C) - * - * This greatly helps visualize how the parts of the expression are performed - * there is essentially three, from right to left we perform the following: - * - * 1: P(C), the probability that a proposed correction C will stand on its - * own. This is called the language model. - * - * 2: P(I|C), the probability that I would be used, when the programmer - * really meant C. This is the error model. - * - * 3: AC, the control mechanisim, which implies the enumeration of all - * feasible values of C, and then determine the one that gives the - * greatest probability score. Selecting it as the "correction" - * - * - * The requirement for complex expression involving two models: - * - * In reality the requirement for a more complex expression involving - * two seperate models is considerably a waste. But one must recognize - * that P(C|I) is already conflating two factors. It's just much simpler - * to seperate the two models and deal with them explicitaly. To properly - * estimate P(C|I) you have to consider both the probability of C and - * probability of the transposition from C to I. It's simply much more - * cleaner, and direct to seperate the two factors. - */ -/* some hashtable management for dictonaries */ +/* + * Implementation of the corrector algorithm commences. A very efficent + * brute-force attack (thanks to tries and mempool :-)). + */ static size_t *correct_find(correct_trie_t *table, const char *word) { return (size_t*)correct_trie_get(table, word); } @@ -420,7 +446,6 @@ char *correct_str(correct_trie_t* table, const char *ident) { char **e2; char *e1ident; char *e2ident; - char *found = util_strdup(ident); size_t e1rows = 0; size_t e2rows = 0; @@ -429,21 +454,20 @@ char *correct_str(correct_trie_t* table, const char *ident) { /* needs to be allocated for free later */ if (correct_find(table, ident)) - return correct_outstr(found); + return correct_pool_claim(ident); if ((e1rows = correct_size(ident))) { e1 = correct_edit(ident); - if ((e1ident = correct_maximum(table, e1, e1rows))) { - found = util_strdup(e1ident); - return correct_outstr(found); - } + if ((e1ident = correct_maximum(table, e1, e1rows))) + return correct_pool_claim(e1ident); } e2 = correct_known(table, e1, e1rows, &e2rows); - if (e2rows && ((e2ident = correct_maximum(table, e2, e2rows)))) { - found = util_strdup(e2ident); - } + if (e2rows && ((e2ident = correct_maximum(table, e2, e2rows)))) + return correct_pool_claim(e2ident); - return correct_outstr(found); + + correct_pool_delete(); + return util_strdup(ident); } From e2e4907b606f121e69556c4b4d583172140775d7 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 03:26:09 +0000 Subject: [PATCH 06/25] Nicer loops --- correct.c | 67 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/correct.c b/correct.c index 8e5c759..7056b52 100644 --- a/correct.c +++ b/correct.c @@ -69,8 +69,14 @@ * probability of the transposition from C to I. It's simply much more * cleaner, and direct to seperate the two factors. * + * Research tells us that 80% to 95% of all spelling errors have an edit + * distance no greater than one. Knowing this we can optimize for most + * cases of mistakes without taking a performance hit. Which is what we + * base longer edit distances off of. Opposed to the original method of + * I had concieved of checking everything. + * * A little information on additional algorithms used: - * + * * Initially when I implemented this corrector, it was very slow. * Need I remind you this is essentially a brute force attack on strings, * and since every transformation requires dynamic memory allocations, @@ -89,6 +95,21 @@ * very slow. To combat this I had suggested burst tries to Blub. * The next day he had implemented them. Sure enough this brought * down the runtime by a factory > 100% + * + * Future Work (If we really need it) + * + * Currently we can only distinguishes one source of error in the + * language model we use. This could become an issue for identifiers + * that have close colliding rates, e.g colate->coat yields collate. + * + * Currently the error model has been fairly trivial, the smaller the + * edit distance the smaller the error. This usually causes some un- + * expected problems. e.g reciet->recite yields recipt. For QuakeC + * this could become a problem when lots of identifiers are involved. + * + * Our control mechanisim could use a limit, i.e limit the number of + * sets of edits for distance X. This would also increase execution + * speed considerably. */ @@ -193,10 +214,11 @@ void* correct_trie_get(const correct_trie_t *t, const char *key) { void correct_trie_set(correct_trie_t *t, const char *key, void * const value) { const unsigned char *data = (const unsigned char*)key; while (*data) { - unsigned char ch = *data; - correct_trie_t *entries = t->entries; - const size_t vs = vec_size(t->entries); - size_t i; + const size_t vs = vec_size(t->entries); + unsigned char ch = *data; + correct_trie_t *entries = t->entries; + size_t i; + for (i = 0; i < vs; ++i) { if (entries[i].ch == ch) { t = &entries[i]; @@ -205,10 +227,11 @@ void correct_trie_set(correct_trie_t *t, const char *key, void * const value) { } if (i == vs) { correct_trie_t *elem = (correct_trie_t*)vec_add(t->entries, 1); + elem->ch = ch; elem->value = NULL; elem->entries = NULL; - t = elem; + t = elem; } ++data; } @@ -247,8 +270,9 @@ void correct_add(correct_trie_t* table, size_t ***size, const char *ident) { } void correct_del(correct_trie_t* dictonary, size_t **data) { - size_t i; + size_t i; const size_t vs = vec_size(data); + for (i = 0; i < vs; i++) mem_d(data[i]); @@ -261,7 +285,9 @@ void correct_del(correct_trie_t* dictonary, size_t **data) { * because they're only valid after the first character is of a _, or * alpha character. */ -static const char correct_alpha[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"; +static const char correct_alpha[] = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "_"; /* TODO: Numbers ... */ /* * correcting logic for the following forms of transformations: @@ -392,19 +418,19 @@ static GMQCC_INLINE char **correct_known_resize(char **res, size_t *allocated, s } static char **correct_known(correct_trie_t* table, char **array, size_t rows, size_t *next) { - size_t itr; - size_t jtr; - size_t len; - size_t row; + size_t itr = 0; + size_t jtr = 0; + size_t len = 0; + size_t row = 0; size_t nxt = 8; char **res = correct_pool_alloc(sizeof(char *) * nxt); char **end = NULL; - for (itr = 0, len = 0; itr < rows; itr++) { + for (; itr < rows; itr++) { end = correct_edit(array[itr]); row = correct_size(array[itr]); - for (jtr = 0; jtr < row; jtr++) { + for (; jtr < row; jtr++) { if (correct_find(table, end[jtr]) && !correct_exist(res, len, end[jtr])) { res = correct_known_resize(res, &nxt, len+1); res[len++] = end[jtr]; @@ -417,12 +443,12 @@ static char **correct_known(correct_trie_t* table, char **array, size_t rows, si } static char *correct_maximum(correct_trie_t* table, char **array, size_t rows) { - char *str = NULL; - size_t *itm = NULL; - size_t itr; - size_t top; + char *str = NULL; + size_t *itm = NULL; + size_t itr = 0; + size_t top = 0; - for (itr = 0, top = 0; itr < rows; itr++) { + for (; itr < rows; itr++) { if ((itm = correct_find(table, array[itr])) && (*itm > top)) { top = *itm; str = array[itr]; @@ -439,8 +465,7 @@ static char *correct_maximum(correct_trie_t* table, char **array, size_t rows) { * * the add function works the same. Except the identifier is used to * add to the dictonary. - */ - + */ char *correct_str(correct_trie_t* table, const char *ident) { char **e1; char **e2; From 9841240aab10288fa70ae696891572e13b82021f Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 03:29:55 +0000 Subject: [PATCH 07/25] Some more nicer loops --- correct.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/correct.c b/correct.c index 7056b52..d9ad757 100644 --- a/correct.c +++ b/correct.c @@ -297,10 +297,10 @@ static const char correct_alpha[] = "abcdefghijklmnopqrstuvwxyz" * 4) insertion */ static size_t correct_deletion(const char *ident, char **array, size_t index) { - size_t itr; - size_t len = strlen(ident); + size_t itr = 0; + const size_t len = strlen(ident); - for (itr = 0; itr < len; itr++) { + for (; itr < len; itr++) { char *a = (char*)correct_pool_alloc(len+1); memcpy(a, ident, itr); memcpy(a + itr, ident + itr + 1, len - itr); @@ -311,10 +311,10 @@ static size_t correct_deletion(const char *ident, char **array, size_t index) { } static size_t correct_transposition(const char *ident, char **array, size_t index) { - size_t itr; - size_t len = strlen(ident); + size_t itr = 0; + const size_t len = strlen(ident); - for (itr = 0; itr < len - 1; itr++) { + for (; itr < len - 1; itr++) { char tmp; char *a = (char*)correct_pool_alloc(len+1); memcpy(a, ident, len+1); @@ -328,12 +328,12 @@ static size_t correct_transposition(const char *ident, char **array, size_t inde } static size_t correct_alteration(const char *ident, char **array, size_t index) { - size_t itr; - size_t jtr; - size_t ktr; - size_t len = strlen(ident); + size_t itr = 0; + size_t jtr = 0; + size_t ktr = 0; + const size_t len = strlen(ident); - for (itr = 0, ktr = 0; itr < len; itr++) { + for (; itr < len; itr++) { for (jtr = 0; jtr < sizeof(correct_alpha)-1; jtr++, ktr++) { char *a = (char*)correct_pool_alloc(len+1); memcpy(a, ident, len+1); @@ -346,12 +346,12 @@ static size_t correct_alteration(const char *ident, char **array, size_t index) } static size_t correct_insertion(const char *ident, char **array, size_t index) { - size_t itr; - size_t jtr; - size_t ktr; - const size_t len = strlen(ident); + size_t itr = 0; + size_t jtr = 0; + size_t ktr = 0; + const size_t len = strlen(ident); - for (itr = 0, ktr = 0; itr <= len; itr++) { + for (; itr <= len; itr++) { for (jtr = 0; jtr < sizeof(correct_alpha)-1; jtr++, ktr++) { char *a = (char*)correct_pool_alloc(len+2); memcpy(a, ident, itr); From cc8558025bd263fbe4f20129f9a39a4e778b59bb Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 03:33:21 +0000 Subject: [PATCH 08/25] No more uninitialized --- correct.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/correct.c b/correct.c index d9ad757..3926441 100644 --- a/correct.c +++ b/correct.c @@ -467,13 +467,12 @@ static char *correct_maximum(correct_trie_t* table, char **array, size_t rows) { * add to the dictonary. */ char *correct_str(correct_trie_t* table, const char *ident) { - char **e1; - char **e2; - char *e1ident; - char *e2ident; - - size_t e1rows = 0; - size_t e2rows = 0; + char **e1 = NULL; + char **e2 = NULL; + char *e1ident = NULL; + char *e2ident = NULL; + size_t e1rows = 0; + size_t e2rows = 0; correct_pool_new(); From 106db76b9de624e6f26f16aa3e81ce94ec2c03ef Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 03:34:56 +0000 Subject: [PATCH 09/25] Use memcmp with strlen for correct_exists (it's a hell of a lot faster) --- correct.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/correct.c b/correct.c index 3926441..407d06a 100644 --- a/correct.c +++ b/correct.c @@ -399,7 +399,7 @@ static char **correct_edit(const char *ident) { static int correct_exist(char **array, size_t rows, char *ident) { size_t itr; for (itr = 0; itr < rows; itr++) - if (!strcmp(array[itr], ident)) + if (!memcmp(array[itr], ident, strlen(ident))) return 1; return 0; From 44a7154f58b2cc2f6a426ae1ae2cb87150275ef1 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 03:52:37 +0000 Subject: [PATCH 10/25] Add a notice --- correct.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/correct.c b/correct.c index 407d06a..abad321 100644 --- a/correct.c +++ b/correct.c @@ -1,7 +1,8 @@ /* * Copyright (C) 2012, 2013 * Dale Weiler - * + * Wolfgang Bumiller + * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to @@ -295,6 +296,15 @@ static const char correct_alpha[] = "abcdefghijklmnopqrstuvwxyz" * 2) transposition * 3) alteration * 4) insertion + * + * These functions could take an additional size_t **size paramater + * and store back the results of their new length in an array that + * is the same as **array for the memcmp in correct_exists. I'm just + * not able to figure out how to do that just yet. As my brain is + * not in the mood to figure out that logic. This is a reminder to + * do it, or for someone else to :-) correct_edit however would also + * need to take a size_t ** to carry it along (would all the argument + * overhead be worth it?) */ static size_t correct_deletion(const char *ident, char **array, size_t index) { size_t itr = 0; From d98cc564b14bbcd74d67f958f23edf572f69c54b Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 04:06:38 +0000 Subject: [PATCH 11/25] Fixes and more documentation --- correct.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/correct.c b/correct.c index abad321..d68b3b7 100644 --- a/correct.c +++ b/correct.c @@ -33,7 +33,7 @@ * * A little about how it works, and probability theory: * - * When given an identifier (which we will denote I), we're essentially + * When given an identifier (which we will denote I), we're essentially * just trying to choose the most likely correction for that identifier. * (the actual "correction" can very well be the identifier itself). * There is actually no way to know for sure that certian identifers @@ -44,9 +44,20 @@ * out of all possible corrections that maximizes the probability of C * for the original identifer I. * - * Bayes' Therom suggests something of the following: + * Thankfully there exists some theroies for probalistic interpretations + * of data. Since we're operating on two distictive intepretations, the + * transposition from I to C. We need something that can express how much + * degree of I should rationally change to become C. this is called the + * Bayesian interpretation. You can read more about it from here: + * http://www.celiagreen.com/charlesmccreery/statistics/bayestutorial.pdf + * (which is probably the only good online documentation for bayes theroy + * no lie. Everything else just sucks ..) + * + * Bayes' Thereom suggests something like the following: * AC P(I|C) P(C) / P(I) - * Since P(I) is the same for every possibly I, we can ignore it giving + * + * However since P(I) is the same for every possibility of I, we can + * complete ignore it giving just: * AC P(I|C) P(C) * * This greatly helps visualize how the parts of the expression are performed @@ -78,7 +89,7 @@ * * A little information on additional algorithms used: * - * Initially when I implemented this corrector, it was very slow. + * Initially when I implemented this corrector, it was very slow. * Need I remind you this is essentially a brute force attack on strings, * and since every transformation requires dynamic memory allocations, * you can easily imagine where most of the runtime conflated. Yes @@ -87,7 +98,7 @@ * shock to me. A forward allocator (or as some call it a bump-point * allocator, or just a memory pool) was implemented. To combat this. * - * But of course even other factors were making it slow. Initially + * But of course even other factors were making it slow. Initially * this used a hashtable. And hashtables have a good constant lookup * time complexity. But the problem wasn't in the hashtable, it was * in the hashing (despite having one of the fastest hash functions @@ -99,18 +110,19 @@ * * Future Work (If we really need it) * - * Currently we can only distinguishes one source of error in the + * Currently we can only distinguishes one source of error in the * language model we use. This could become an issue for identifiers * that have close colliding rates, e.g colate->coat yields collate. * - * Currently the error model has been fairly trivial, the smaller the + * Currently the error model has been fairly trivial, the smaller the * edit distance the smaller the error. This usually causes some un- * expected problems. e.g reciet->recite yields recipt. For QuakeC * this could become a problem when lots of identifiers are involved. * - * Our control mechanisim could use a limit, i.e limit the number of + * Our control mechanisim could use a limit, i.e limit the number of * sets of edits for distance X. This would also increase execution - * speed considerably. + * speed considerably. + * */ @@ -163,12 +175,12 @@ static GMQCC_INLINE char *correct_pool_claim(const char *data) { } /* - * A fast space efficent trie for a disctonary of identifiers. This is + * A fast space efficent trie for a dictionary of identifiers. This is * faster than a hashtable for one reason. A hashtable itself may have * fast constant lookup time, but the hash itself must be very fast. We * have one of the fastest hash functions for strings, but if you do a * lost of hashing (which we do, almost 3 million hashes per identifier) - * a hashtable becomes slow. Very Very Slow. + * a hashtable becomes slow. */ correct_trie_t* correct_trie_new() { correct_trie_t *t = (correct_trie_t*)mem_a(sizeof(correct_trie_t)); @@ -440,7 +452,8 @@ static char **correct_known(correct_trie_t* table, char **array, size_t rows, si end = correct_edit(array[itr]); row = correct_size(array[itr]); - for (; jtr < row; jtr++) { + /* removing jtr=0 here speeds it up by 100ms O_o */ + for (jtr = 0; jtr < row; jtr++) { if (correct_find(table, end[jtr]) && !correct_exist(res, len, end[jtr])) { res = correct_known_resize(res, &nxt, len+1); res[len++] = end[jtr]; From 0c59274c54396dd5809e958d1260178dbeb688c4 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 04:09:12 +0000 Subject: [PATCH 12/25] This is a work of art, it deserve nice comments :) --- correct.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/correct.c b/correct.c index d68b3b7..4d981ec 100644 --- a/correct.c +++ b/correct.c @@ -33,7 +33,7 @@ * * A little about how it works, and probability theory: * - * When given an identifier (which we will denote I), we're essentially + * When given an identifier (which we will denote I), we're essentially * just trying to choose the most likely correction for that identifier. * (the actual "correction" can very well be the identifier itself). * There is actually no way to know for sure that certian identifers @@ -44,7 +44,7 @@ * out of all possible corrections that maximizes the probability of C * for the original identifer I. * - * Thankfully there exists some theroies for probalistic interpretations + * Thankfully there exists some theroies for probalistic interpretations * of data. Since we're operating on two distictive intepretations, the * transposition from I to C. We need something that can express how much * degree of I should rationally change to become C. this is called the @@ -57,7 +57,7 @@ * AC P(I|C) P(C) / P(I) * * However since P(I) is the same for every possibility of I, we can - * complete ignore it giving just: + * completley ignore it giving just: * AC P(I|C) P(C) * * This greatly helps visualize how the parts of the expression are performed @@ -73,7 +73,7 @@ * enumerates all feasible values of C, to determine the one that * gives the greatest probability score. * - * In reality the requirement for a more complex expression involving + * In reality the requirement for a more complex expression involving * two seperate models is considerably a waste. But one must recognize * that P(C|I) is already conflating two factors. It's just much simpler * to seperate the two models and deal with them explicitaly. To properly @@ -89,7 +89,7 @@ * * A little information on additional algorithms used: * - * Initially when I implemented this corrector, it was very slow. + * Initially when I implemented this corrector, it was very slow. * Need I remind you this is essentially a brute force attack on strings, * and since every transformation requires dynamic memory allocations, * you can easily imagine where most of the runtime conflated. Yes @@ -98,7 +98,7 @@ * shock to me. A forward allocator (or as some call it a bump-point * allocator, or just a memory pool) was implemented. To combat this. * - * But of course even other factors were making it slow. Initially + * But of course even other factors were making it slow. Initially * this used a hashtable. And hashtables have a good constant lookup * time complexity. But the problem wasn't in the hashtable, it was * in the hashing (despite having one of the fastest hash functions @@ -110,19 +110,18 @@ * * Future Work (If we really need it) * - * Currently we can only distinguishes one source of error in the + * Currently we can only distinguishes one source of error in the * language model we use. This could become an issue for identifiers * that have close colliding rates, e.g colate->coat yields collate. * - * Currently the error model has been fairly trivial, the smaller the + * Currently the error model has been fairly trivial, the smaller the * edit distance the smaller the error. This usually causes some un- * expected problems. e.g reciet->recite yields recipt. For QuakeC * this could become a problem when lots of identifiers are involved. * - * Our control mechanisim could use a limit, i.e limit the number of + * Our control mechanisim could use a limit, i.e limit the number of * sets of edits for distance X. This would also increase execution * speed considerably. - * */ From ee33b4e5cca1099f84e0731f048d141f707a5bb8 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 05:13:19 +0000 Subject: [PATCH 13/25] A little faster, plus some more research --- correct.c | 59 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/correct.c b/correct.c index 4d981ec..6da5a9d 100644 --- a/correct.c +++ b/correct.c @@ -125,7 +125,8 @@ */ -#define CORRECT_POOLSIZE (128*1024*1024) +#define CORRECT_POOL_SIZE (128*1024*1024) +#define CORRECT_POOL_GETLEN(X) *((size_t*)(X) - 1) /* * A forward allcator for the corrector. This corrector requires a lot * of allocations. This forward allocator combats all those allocations @@ -139,20 +140,19 @@ static size_t correct_pool_addr = 0; static GMQCC_INLINE void correct_pool_new(void) { correct_pool_addr = 0; - correct_pool_this = (unsigned char *)mem_a(CORRECT_POOLSIZE); + correct_pool_this = (unsigned char *)mem_a(CORRECT_POOL_SIZE); vec_push(correct_pool_data, correct_pool_this); } static GMQCC_INLINE void *correct_pool_alloc(size_t bytes) { void *data; - if (correct_pool_addr + bytes >= CORRECT_POOLSIZE) + if (correct_pool_addr + bytes>= CORRECT_POOL_SIZE) correct_pool_new(); - data = correct_pool_this; + data = (void*)correct_pool_this; correct_pool_this += bytes; correct_pool_addr += bytes; - return data; } @@ -205,11 +205,13 @@ void correct_trie_del(correct_trie_t *t) { void* correct_trie_get(const correct_trie_t *t, const char *key) { const unsigned char *data = (const unsigned char*)key; + while (*data) { - unsigned char ch = *data; - const size_t vs = vec_size(t->entries); - size_t i; const correct_trie_t *entries = t->entries; + unsigned char ch = *data; + const size_t vs = vec_size(entries); + size_t i; + for (i = 0; i < vs; ++i) { if (entries[i].ch == ch) { t = &entries[i]; @@ -226,9 +228,9 @@ void* correct_trie_get(const correct_trie_t *t, const char *key) { void correct_trie_set(correct_trie_t *t, const char *key, void * const value) { const unsigned char *data = (const unsigned char*)key; while (*data) { - const size_t vs = vec_size(t->entries); - unsigned char ch = *data; correct_trie_t *entries = t->entries; + const size_t vs = vec_size(entries); + unsigned char ch = *data; size_t i; for (i = 0; i < vs; ++i) { @@ -255,17 +257,17 @@ void correct_trie_set(correct_trie_t *t, const char *key, void * const value) { * Implementation of the corrector algorithm commences. A very efficent * brute-force attack (thanks to tries and mempool :-)). */ -static size_t *correct_find(correct_trie_t *table, const char *word) { +static GMQCC_INLINE size_t *correct_find(correct_trie_t *table, const char *word) { return (size_t*)correct_trie_get(table, word); } -static int correct_update(correct_trie_t* *table, const char *word) { +static GMQCC_INLINE bool correct_update(correct_trie_t* *table, const char *word) { size_t *data = correct_find(*table, word); if (!data) - return 0; + return false; (*data)++; - return 1; + return true; } void correct_add(correct_trie_t* table, size_t ***size, const char *ident) { @@ -419,9 +421,36 @@ static char **correct_edit(const char *ident) { */ static int correct_exist(char **array, size_t rows, char *ident) { size_t itr; - for (itr = 0; itr < rows; itr++) + /* + * As an experiment I tried the following assembly for memcmp here: + * + * correct_cmp_loop: + * incl %eax ; eax = LHS + * incl %edx ; edx = LRS + * cmpl %eax, %ebx ; ebx = &LHS[END_POS] + * + * jbe correct_cmp_eq + * movb (%edx), %cl ; micro-optimized on even atoms :-) + * cmpb %cl, (%eax) ; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + * jg correct_cmp_gt + * jge correct_cmp_loop + * ... + * + * Despite how much optimization went in to this, the speed was the + * being conflicted by the strlen(ident) used for &LHS[END_POS] + * If we could eliminate the strlen with what I suggested on line + * 311 ... we can accelerate this whole damn thing quite a bit. + * + * However there is still something we can do here that does give + * us a little more speed. Although one more branch, we know for + * sure there is at least one byte to compare, if that one byte + * simply isn't the same we can skip the full check. Which means + * we skip a whole strlen call. + */ + for (itr = 0; itr < rows; itr++) { if (!memcmp(array[itr], ident, strlen(ident))) return 1; + } return 0; } From cc7e1a33638271d1f7406fd8d1d4504ff4a5b287 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 05:14:57 +0000 Subject: [PATCH 14/25] Screw you clang :-) --- correct.c | 1 - 1 file changed, 1 deletion(-) diff --git a/correct.c b/correct.c index 6da5a9d..cd2ab2d 100644 --- a/correct.c +++ b/correct.c @@ -126,7 +126,6 @@ #define CORRECT_POOL_SIZE (128*1024*1024) -#define CORRECT_POOL_GETLEN(X) *((size_t*)(X) - 1) /* * A forward allcator for the corrector. This corrector requires a lot * of allocations. This forward allocator combats all those allocations From a4c1e63637eec7c2ee10475093a9dba110526eba Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 05:17:30 +0000 Subject: [PATCH 15/25] Less pointer dereferences --- correct.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/correct.c b/correct.c index cd2ab2d..c91cf60 100644 --- a/correct.c +++ b/correct.c @@ -457,12 +457,13 @@ static int correct_exist(char **array, size_t rows, char *ident) { static GMQCC_INLINE char **correct_known_resize(char **res, size_t *allocated, size_t size) { size_t oldallocated = *allocated; char **out; - if (size+1 < *allocated) + if (size+1 < oldallocated) return res; - *allocated += 32; - out = correct_pool_alloc(sizeof(*res) * *allocated); + out = correct_pool_alloc(sizeof(*res) * oldallocated + 32); memcpy(out, res, sizeof(*res) * oldallocated); + + *allocated += 32; return out; } From 6f5a20e76bec7769186817ef27e76d8cf0a24eb1 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 10:29:29 +0000 Subject: [PATCH 16/25] Use -O2 by default --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9e367e0..f7a39e5 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ CYGWIN = $(findstring CYGWIN, $(UNAME)) MINGW = $(findstring MINGW32, $(UNAME)) CC ?= clang -CFLAGS += -Wall -Wextra -I. -fno-strict-aliasing -fsigned-char +CFLAGS += -Wall -Wextra -I. -fno-strict-aliasing -fsigned-char -O2 CFLAGS += -DGMQCC_GITINFO="`git describe`" #turn on tons of warnings if clang is present # but also turn off the STUPID ONES From 056779d3b88bf77190baa2b40d0f59f6d3c5f0f6 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 10:33:09 +0000 Subject: [PATCH 17/25] Remove trailing shitspace --- correct.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/correct.c b/correct.c index c91cf60..6e49def 100644 --- a/correct.c +++ b/correct.c @@ -132,7 +132,7 @@ * and speeds us up a little. It also saves us space in a way since each * allocation isn't wasting a little header space for when NOTRACK isn't * defined. - */ + */ static unsigned char **correct_pool_data = NULL; static unsigned char *correct_pool_this = NULL; static size_t correct_pool_addr = 0; @@ -179,7 +179,7 @@ static GMQCC_INLINE char *correct_pool_claim(const char *data) { * have one of the fastest hash functions for strings, but if you do a * lost of hashing (which we do, almost 3 million hashes per identifier) * a hashtable becomes slow. - */ + */ correct_trie_t* correct_trie_new() { correct_trie_t *t = (correct_trie_t*)mem_a(sizeof(correct_trie_t)); t->value = NULL; @@ -255,7 +255,7 @@ void correct_trie_set(correct_trie_t *t, const char *key, void * const value) { /* * Implementation of the corrector algorithm commences. A very efficent * brute-force attack (thanks to tries and mempool :-)). - */ + */ static GMQCC_INLINE size_t *correct_find(correct_trie_t *table, const char *word) { return (size_t*)correct_trie_get(table, word); } @@ -272,7 +272,7 @@ static GMQCC_INLINE bool correct_update(correct_trie_t* *table, const char *word void correct_add(correct_trie_t* table, size_t ***size, const char *ident) { size_t *data = NULL; const char *add = ident; - + if (!correct_update(&table, add)) { data = (size_t*)mem_a(sizeof(size_t)); *data = 1; @@ -392,7 +392,7 @@ static GMQCC_INLINE size_t correct_size(const char *ident) { * transposition = len - 1 * alteration = len * sizeof(correct_alpha) * insertion = (len + 1) * sizeof(correct_alpha) - */ + */ register size_t len = strlen(ident); return (len) + (len - 1) + (len * (sizeof(correct_alpha)-1)) + ((len + 1) * (sizeof(correct_alpha)-1)); @@ -417,7 +417,7 @@ static char **correct_edit(const char *ident) { * We could use a hashtable but the space complexity isn't worth it * since we're only going to determine the "did you mean?" identifier * on error. - */ + */ static int correct_exist(char **array, size_t rows, char *ident) { size_t itr; /* From cfdeaf478633fc17dc4d192d8af72629bbc471c1 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 10:35:43 +0000 Subject: [PATCH 18/25] Fix comments --- correct.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/correct.c b/correct.c index 6e49def..442afb2 100644 --- a/correct.c +++ b/correct.c @@ -429,7 +429,7 @@ static int correct_exist(char **array, size_t rows, char *ident) { * cmpl %eax, %ebx ; ebx = &LHS[END_POS] * * jbe correct_cmp_eq - * movb (%edx), %cl ; micro-optimized on even atoms :-) + * movb (%edx), %cl ; micro-optimized even on atoms :-) * cmpb %cl, (%eax) ; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * jg correct_cmp_gt * jge correct_cmp_loop @@ -512,10 +512,7 @@ static char *correct_maximum(correct_trie_t* table, char **array, size_t rows) { /* * This is the exposed interface: * takes a table for the dictonary a vector of sizes (used for internal - * probability calculation, and an identifier to "correct" - * - * the add function works the same. Except the identifier is used to - * add to the dictonary. + * probability calculation), and an identifier to "correct". */ char *correct_str(correct_trie_t* table, const char *ident) { char **e1 = NULL; From 0f4090402d3f7955cf6d68f8025ed926a6d2845e Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 10:37:09 +0000 Subject: [PATCH 19/25] No need to +1 for comparision in correct_known_resize --- correct.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/correct.c b/correct.c index 442afb2..5be4f35 100644 --- a/correct.c +++ b/correct.c @@ -457,7 +457,7 @@ static int correct_exist(char **array, size_t rows, char *ident) { static GMQCC_INLINE char **correct_known_resize(char **res, size_t *allocated, size_t size) { size_t oldallocated = *allocated; char **out; - if (size+1 < oldallocated) + if (size < oldallocated) return res; out = correct_pool_alloc(sizeof(*res) * oldallocated + 32); From 4afe61060d62668e054e2f8e9d2b734b6da2b426 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 10:55:22 +0000 Subject: [PATCH 20/25] Add a "depend" rule for the makefile. It uses makedepend to generate dependinces that are catted to the current Makefile. --- Makefile | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index f7a39e5..f804fd1 100644 --- a/Makefile +++ b/Makefile @@ -101,14 +101,15 @@ check: all clean: rm -f *.o $(GMQCC) $(QCVM) $(TESTSUITE) *.dat -# deps -$(OBJ_D) $(OBJ_C) $(OBJ_X): gmqcc.h opts.def -main.o: lexer.h -parser.o: ast.h lexer.h -ftepp.o: lexer.h -lexer.o: lexer.h -ast.o: ast.h ir.h -ir.o: ir.h +depend: + makedepend -Y -w 65536 \ + $(subst .o,.c,$(OBJ_D)) + makedepend -a -Y -w 65536 \ + $(subst .o,.c,$(OBJ_T)) + makedepend -a -Y -w 65536 \ + $(subst .o,.c,$(OBJ_C)) + makedepend -a -Y -w 65536 \ + $(subst .o,.c,$(OBJ_X)) #install rules install: install-gmqcc install-qcvm install-doc @@ -122,3 +123,29 @@ install-doc: install -d -m755 $(DESTDIR)$(MANDIR)/man1 install -m755 doc/gmqcc.1 $(DESTDIR)$(MANDIR)/man1/ install -m755 doc/qcvm.1 $(DESTDIR)$(MANDIR)/man1/ + +# DO NOT DELETE +util.o: gmqcc.h opts.def +code.o: gmqcc.h opts.def +ast.o: gmqcc.h opts.def ast.h ir.h +ir.o: gmqcc.h opts.def ir.h +conout.o: gmqcc.h opts.def +ftepp.o: gmqcc.h opts.def lexer.h +opts.o: gmqcc.h opts.def +file.o: gmqcc.h opts.def +utf8.o: gmqcc.h opts.def +correct.o: gmqcc.h opts.def + +test.o: gmqcc.h opts.def +util.o: gmqcc.h opts.def +conout.o: gmqcc.h opts.def +file.o: gmqcc.h opts.def + +main.o: gmqcc.h opts.def lexer.h +lexer.o: gmqcc.h opts.def lexer.h +parser.o: gmqcc.h opts.def lexer.h ast.h ir.h +file.o: gmqcc.h opts.def + +util.o: gmqcc.h opts.def +conout.o: gmqcc.h opts.def +file.o: gmqcc.h opts.def From 3fa771f51d920b9816894fc0453dc2492a9dbf0b Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 10:56:25 +0000 Subject: [PATCH 21/25] Leave a space here for makedepend --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index f804fd1..b2ca6f8 100644 --- a/Makefile +++ b/Makefile @@ -125,6 +125,7 @@ install-doc: install -m755 doc/qcvm.1 $(DESTDIR)$(MANDIR)/man1/ # DO NOT DELETE + util.o: gmqcc.h opts.def code.o: gmqcc.h opts.def ast.o: gmqcc.h opts.def ast.h ir.h From 1e17b5f696dffeb9eac8f9f8c8562eb7842cd316 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 11:02:39 +0000 Subject: [PATCH 22/25] Cleaner transformation calls (one less size_t for agruments). We can coalesce it in correct_edit. --- correct.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/correct.c b/correct.c index 5be4f35..226579b 100644 --- a/correct.c +++ b/correct.c @@ -318,7 +318,7 @@ static const char correct_alpha[] = "abcdefghijklmnopqrstuvwxyz" * need to take a size_t ** to carry it along (would all the argument * overhead be worth it?) */ -static size_t correct_deletion(const char *ident, char **array, size_t index) { +static size_t correct_deletion(const char *ident, char **array) { size_t itr = 0; const size_t len = strlen(ident); @@ -326,13 +326,13 @@ static size_t correct_deletion(const char *ident, char **array, size_t index) { char *a = (char*)correct_pool_alloc(len+1); memcpy(a, ident, itr); memcpy(a + itr, ident + itr + 1, len - itr); - array[index + itr] = a; + array[itr] = a; } return itr; } -static size_t correct_transposition(const char *ident, char **array, size_t index) { +static size_t correct_transposition(const char *ident, char **array) { size_t itr = 0; const size_t len = strlen(ident); @@ -343,13 +343,13 @@ static size_t correct_transposition(const char *ident, char **array, size_t inde tmp = a[itr]; a[itr ] = a[itr+1]; a[itr+1] = tmp; - array[index + itr] = a; + array[itr] = a; } return itr; } -static size_t correct_alteration(const char *ident, char **array, size_t index) { +static size_t correct_alteration(const char *ident, char **array) { size_t itr = 0; size_t jtr = 0; size_t ktr = 0; @@ -360,14 +360,14 @@ static size_t correct_alteration(const char *ident, char **array, size_t index) char *a = (char*)correct_pool_alloc(len+1); memcpy(a, ident, len+1); a[itr] = correct_alpha[jtr]; - array[index + ktr] = a; + array[ktr] = a; } } return ktr; } -static size_t correct_insertion(const char *ident, char **array, size_t index) { +static size_t correct_insertion(const char *ident, char **array) { size_t itr = 0; size_t jtr = 0; size_t ktr = 0; @@ -379,7 +379,7 @@ static size_t correct_insertion(const char *ident, char **array, size_t index) { memcpy(a, ident, itr); memcpy(a + itr + 1, ident + itr, len - itr + 1); a[itr] = correct_alpha[jtr]; - array[index + ktr] = a; + array[ktr] = a; } } @@ -405,10 +405,10 @@ static char **correct_edit(const char *ident) { if (!find) return NULL; - next = correct_deletion (ident, find, 0); - next += correct_transposition(ident, find, next); - next += correct_alteration (ident, find, next); - /*****/ correct_insertion (ident, find, next); + next = correct_deletion (ident, find); + next += correct_transposition(ident, find+next); + next += correct_alteration (ident, find+next); + /*****/ correct_insertion (ident, find+next); return find; } From 9c8ddb3771ded271e64d710659b615112b42dbcc Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Sun, 6 Jan 2013 13:07:28 +0100 Subject: [PATCH 23/25] An evil optimization to the trie, now has a fixed amount of branches and uses a char-to-index map to index into the branches... --- correct.c | 89 +++++++++++++++++++++++++------------------------------ gmqcc.h | 3 +- 2 files changed, 41 insertions(+), 51 deletions(-) diff --git a/correct.c b/correct.c index 226579b..bd79c63 100644 --- a/correct.c +++ b/correct.c @@ -172,6 +172,26 @@ static GMQCC_INLINE char *correct_pool_claim(const char *data) { return claim; } +/* + * _ is valid in identifiers. I've yet to implement numerics however + * because they're only valid after the first character is of a _, or + * alpha character. + */ +static const char correct_alpha[] = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "_"; /* TODO: Numbers ... */ + +static const size_t correct_alpha_index[0x80] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 52, + 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0 +}; + /* * A fast space efficent trie for a dictionary of identifiers. This is * faster than a hashtable for one reason. A hashtable itself may have @@ -189,16 +209,21 @@ correct_trie_t* correct_trie_new() { void correct_trie_del_sub(correct_trie_t *t) { size_t i; - for (i = 0; i < vec_size(t->entries); ++i) + if (!t->entries) + return; + for (i = 0; i < sizeof(correct_alpha)-1; ++i) { correct_trie_del_sub(&t->entries[i]); - vec_free(t->entries); + } + mem_d(t->entries); } void correct_trie_del(correct_trie_t *t) { size_t i; - for (i = 0; i < vec_size(t->entries); ++i) - correct_trie_del_sub(&t->entries[i]); - vec_free(t->entries); + if (t->entries) { + for (i = 0; i < sizeof(correct_alpha)-1; ++i) + correct_trie_del_sub(&t->entries[i]); + mem_d(t->entries); + } mem_d(t); } @@ -206,20 +231,10 @@ void* correct_trie_get(const correct_trie_t *t, const char *key) { const unsigned char *data = (const unsigned char*)key; while (*data) { - const correct_trie_t *entries = t->entries; - unsigned char ch = *data; - const size_t vs = vec_size(entries); - size_t i; - - for (i = 0; i < vs; ++i) { - if (entries[i].ch == ch) { - t = &entries[i]; - ++data; - break; - } - } - if (i == vs) + if (!t->entries) return NULL; + t = t->entries + correct_alpha_index[*data]; + ++data; } return t->value; } @@ -227,25 +242,11 @@ void* correct_trie_get(const correct_trie_t *t, const char *key) { void correct_trie_set(correct_trie_t *t, const char *key, void * const value) { const unsigned char *data = (const unsigned char*)key; while (*data) { - correct_trie_t *entries = t->entries; - const size_t vs = vec_size(entries); - unsigned char ch = *data; - size_t i; - - for (i = 0; i < vs; ++i) { - if (entries[i].ch == ch) { - t = &entries[i]; - break; - } - } - if (i == vs) { - correct_trie_t *elem = (correct_trie_t*)vec_add(t->entries, 1); - - elem->ch = ch; - elem->value = NULL; - elem->entries = NULL; - t = elem; + if (!t->entries) { + t->entries = (correct_trie_t*)mem_a(sizeof(correct_trie_t)*(sizeof(correct_alpha)-1)); + memset(t->entries, 0, sizeof(correct_trie_t)*(sizeof(correct_alpha)-1)); } + t = t->entries + correct_alpha_index[*data]; ++data; } t->value = value; @@ -293,15 +294,6 @@ void correct_del(correct_trie_t* dictonary, size_t **data) { correct_trie_del(dictonary); } -/* - * _ is valid in identifiers. I've yet to implement numerics however - * because they're only valid after the first character is of a _, or - * alpha character. - */ -static const char correct_alpha[] = "abcdefghijklmnopqrstuvwxyz" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "_"; /* TODO: Numbers ... */ - /* * correcting logic for the following forms of transformations: * 1) deletion @@ -370,20 +362,19 @@ static size_t correct_alteration(const char *ident, char **array) { static size_t correct_insertion(const char *ident, char **array) { size_t itr = 0; size_t jtr = 0; - size_t ktr = 0; const size_t len = strlen(ident); for (; itr <= len; itr++) { - for (jtr = 0; jtr < sizeof(correct_alpha)-1; jtr++, ktr++) { + for (jtr = 0; jtr < sizeof(correct_alpha)-1; jtr++) { char *a = (char*)correct_pool_alloc(len+2); memcpy(a, ident, itr); memcpy(a + itr + 1, ident + itr, len - itr + 1); a[itr] = correct_alpha[jtr]; - array[ktr] = a; + array[itr * (sizeof(correct_alpha)-1) + jtr] = a; } } - return ktr; + return (len+1)*(sizeof(correct_alpha)-1); } static GMQCC_INLINE size_t correct_size(const char *ident) { diff --git a/gmqcc.h b/gmqcc.h index 11c3a3e..f26152f 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -316,9 +316,8 @@ void _util_vec_grow(void **a, size_t i, size_t s); #define vec_remove(A,I,N) memmove((A)+(I),(A)+((I)+(N)),sizeof(*(A))*(vec_meta(A)->used-(I)-(N))),vec_meta(A)->used-=(N) typedef struct trie_s { - struct trie_s *entries; - unsigned char ch; void *value; + struct trie_s *entries; } correct_trie_t; correct_trie_t* correct_trie_new(); From 9a4e21517937ebfa2b55c582d69b27a6d9140892 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 12:24:05 +0000 Subject: [PATCH 24/25] Document the awesome hack --- correct.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/correct.c b/correct.c index bd79c63..3e18eb6 100644 --- a/correct.c +++ b/correct.c @@ -106,11 +106,22 @@ * there is also a hash. After 3 million hashes .. you start to get * very slow. To combat this I had suggested burst tries to Blub. * The next day he had implemented them. Sure enough this brought - * down the runtime by a factory > 100% + * down the runtime by a factor > 100% * + * The trie initially was designed to work on all strings, but later it + * became aparent that not only was this not a requirement. It was also + * slowing down get/sets' for the trie. To fully understand, only + * correct_alpha needs to be understood by the trie system, knowing this + * We can combat the slowness using a very clever but evil optimization. + * By Setting a fixed sized amount of branches for the trie using a + * char-to-index map into the branches. We've complelty made the trie + * accesses entierly constant in lookup time. No really, a lookup is + * literally trie[str[0]] [str[1]] [2] .... .value. + * + * * Future Work (If we really need it) * - * Currently we can only distinguishes one source of error in the + * Currently we can only distinguish one source of error in the * language model we use. This could become an issue for identifiers * that have close colliding rates, e.g colate->coat yields collate. * From ac5cc498407521d07ec6d3952e41c0786bf04832 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 6 Jan 2013 12:28:27 +0000 Subject: [PATCH 25/25] remove a the --- correct.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/correct.c b/correct.c index 3e18eb6..cbd5baa 100644 --- a/correct.c +++ b/correct.c @@ -437,7 +437,7 @@ static int correct_exist(char **array, size_t rows, char *ident) { * jge correct_cmp_loop * ... * - * Despite how much optimization went in to this, the speed was the + * Despite how much optimization went in to this, the speed was * being conflicted by the strlen(ident) used for &LHS[END_POS] * If we could eliminate the strlen with what I suggested on line * 311 ... we can accelerate this whole damn thing quite a bit.