diff options
author | Kristaps Dzonsons <kristaps@bsd.lv> | 2009-09-16 14:40:56 +0000 |
---|---|---|
committer | Kristaps Dzonsons <kristaps@bsd.lv> | 2009-09-16 14:40:56 +0000 |
commit | 6345042a457de84d0b4fdb0ffe8e21052479fdbc (patch) | |
tree | a033285b86728a45a837dff5d7d34369d7055e0a | |
parent | 464bf99c7bec33718cdc95c963d3e5d3a28b3124 (diff) | |
download | mandoc-6345042a457de84d0b4fdb0ffe8e21052479fdbc.tar.gz |
Lookup hashes are now static tables, ordered first-level by second character, then randomly along a chain. Improves performance by a small fraction and considerably cleans up hash sources.
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | libman.h | 6 | ||||
-rw-r--r-- | libmdoc.h | 6 | ||||
-rw-r--r-- | man.c | 12 | ||||
-rw-r--r-- | man_hash.c | 39 | ||||
-rw-r--r-- | mdoc.c | 11 | ||||
-rw-r--r-- | mdoc_action.c | 2 | ||||
-rw-r--r-- | mdoc_hash.c | 143 | ||||
-rw-r--r-- | mdoc_macro.c | 2 |
9 files changed, 55 insertions, 168 deletions
@@ -184,7 +184,7 @@ ChangeLog.html: ChangeLog.xml ChangeLog.xsl mdocml-$(VERSION).tar.gz: $(INSTALL) mkdir -p .dist/mdocml/mdocml-$(VERSION)/ - install -m 0644 $(INSTALL) .dist/mdocml/mdocml-$(VERSION)/ + cp -f $(INSTALL) .dist/mdocml/mdocml-$(VERSION)/ ( cd .dist/mdocml/ && tar zcf ../../$@ mdocml-$(VERSION)/ ) rm -rf .dist/ @@ -27,7 +27,6 @@ enum man_next { struct man { void *data; struct man_cb cb; - void *htab; int pflags; int flags; #define MAN_HALT (1 << 0) @@ -95,9 +94,8 @@ int man_body_alloc(struct man *, int, int, int); int man_elem_alloc(struct man *, int, int, int); void man_node_free(struct man_node *); void man_node_freelist(struct man_node *); -void *man_hash_alloc(void); -int man_hash_find(const void *, const char *); -void man_hash_free(void *); +void man_hash_init(void); +int man_hash_find(const char *); int man_macroend(struct man *); int man_args(struct man *, int, int *, char *, char **); #define ARGS_ERROR (-1) @@ -27,7 +27,6 @@ enum mdoc_next { struct mdoc { void *data; struct mdoc_cb cb; - void *htab; int flags; #define MDOC_HALT (1 << 0) /* Error in parse. Halt. */ #define MDOC_LITERAL (1 << 1) /* In a literal scope. */ @@ -139,9 +138,8 @@ int mdoc_tail_alloc(struct mdoc *, int, int, int); int mdoc_body_alloc(struct mdoc *, int, int, int); void mdoc_node_free(struct mdoc_node *); void mdoc_node_freelist(struct mdoc_node *); -void *mdoc_hash_alloc(void); -int mdoc_hash_find(const void *, const char *); -void mdoc_hash_free(void *); +void mdoc_hash_init(void); +int mdoc_hash_find(const char *); int mdoc_iscdelim(char); int mdoc_isdelim(const char *); size_t mdoc_isescape(const char *); @@ -105,9 +105,6 @@ man_free(struct man *man) { man_free1(man); - - if (man->htab) - man_hash_free(man->htab); free(man); } @@ -125,14 +122,11 @@ man_alloc(void *data, int pflags, const struct man_cb *cb) return(NULL); } + man_hash_init(); + p->data = data; p->pflags = pflags; (void)memcpy(&p->cb, cb, sizeof(struct man_cb)); - - if (NULL == (p->htab = man_hash_alloc())) { - free(p); - return(NULL); - } return(p); } @@ -513,7 +507,7 @@ man_pmacro(struct man *m, int ln, char *buf) return(1); } - if (MAN_MAX == (c = man_hash_find(m->htab, mac))) { + if (MAN_MAX == (c = man_hash_find(mac))) { if ( ! (MAN_IGN_MACRO & m->pflags)) { (void)man_perr(m, ln, ppos, WMACRO); goto err; @@ -15,39 +15,25 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <assert.h> +#include <limits.h> #include <stdlib.h> #include <string.h> #include "libman.h" -/* ARGUSED */ -void -man_hash_free(void *htab) -{ - - free(htab); -} +static unsigned char table[26 * 6]; -/* ARGUSED */ -void * -man_hash_alloc(void) +void +man_hash_init(void) { - int *htab; int i, j, x; - /* Initialised to -1. */ - - htab = malloc(26 * 6 * sizeof(int)); - if (NULL == htab) - return(NULL); - for (i = 0; i < 26 * 6; i++) - htab[i] = -1; + memset(table, UCHAR_MAX, sizeof(table)); for (i = 0; i < MAN_MAX; i++) { x = man_macronames[i][0]; - assert((x >= 65 && x <= 90) || (x >= 97 && x <= 122)); @@ -55,25 +41,19 @@ man_hash_alloc(void) x *= 6; for (j = 0; j < 6; j++) - if (-1 == htab[x + j]) { - htab[x + j] = i; + if (UCHAR_MAX == table[x + j]) { + table[x + j] = i; break; } - assert(j < 6); } - - return((void *)htab); } int -man_hash_find(const void *arg, const char *tmp) +man_hash_find(const char *tmp) { int x, i, tok; - const int *htab; - - htab = (const int *)arg; if (0 == (x = tmp[0])) return(MAN_MAX); @@ -84,7 +64,7 @@ man_hash_find(const void *arg, const char *tmp) x *= 6; for (i = 0; i < 6; i++) { - if (-1 == (tok = htab[x + i])) + if (UCHAR_MAX == (tok = table[x + i])) return(MAN_MAX); if (0 == strcmp(tmp, man_macronames[tok])) return(tok); @@ -92,4 +72,3 @@ man_hash_find(const void *arg, const char *tmp) return(MAN_MAX); } - @@ -230,8 +230,6 @@ mdoc_free(struct mdoc *mdoc) { mdoc_free1(mdoc); - if (mdoc->htab) - mdoc_hash_free(mdoc->htab); free(mdoc); } @@ -249,13 +247,12 @@ mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb) if (cb) (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb)); + mdoc_hash_init(); + p->data = data; p->pflags = pflags; - if (NULL == (p->htab = mdoc_hash_alloc())) { - free(p); - return(NULL); - } else if (mdoc_alloc1(p)) + if (mdoc_alloc1(p)) return(p); free(p); @@ -725,7 +722,7 @@ parsemacro(struct mdoc *m, int ln, char *buf) return(1); } - if (MDOC_MAX == (c = mdoc_hash_find(m->htab, mac))) { + if (MDOC_MAX == (c = mdoc_hash_find(mac))) { if ( ! macrowarn(m, ln, mac)) goto err; return(1); diff --git a/mdoc_action.c b/mdoc_action.c index dd8487ad..13267a87 100644 --- a/mdoc_action.c +++ b/mdoc_action.c @@ -617,7 +617,7 @@ post_bl_width(POST_ARGS) if (0 == strcmp(p, "Ds")) width = 6; - else if (MDOC_MAX == (tok = mdoc_hash_find(m->htab, p))) + else if (MDOC_MAX == (tok = mdoc_hash_find(p))) return(1); else if (0 == (width = mdoc_macro2len(tok))) return(mdoc_nwarn(m, n, ENOWIDTH)); diff --git a/mdoc_hash.c b/mdoc_hash.c index d5af031f..44e147f7 100644 --- a/mdoc_hash.c +++ b/mdoc_hash.c @@ -18,149 +18,70 @@ #include <assert.h> #include <ctype.h> +#include <limits.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include "libmdoc.h" -#define ADJUST_MAJOR(x) \ - do if (37 == (x)) \ - (x) = 0; /* % -> 00 */ \ - else if (91 > (x)) \ - (x) -= 64; /* A-Z -> 01 - 26 */ \ - else \ - (x) -= 70; /* a-z -> 27 - 52 */ \ - while (/*CONSTCOND*/0) - -#define ADJUST_MINOR(y) \ - do if (49 == (y)) \ - (y) = 0; /* 1 -> 00 */ \ - else if (91 > (y)) \ - (y) -= 65; /* A-Z -> 00 - 25 */ \ - else \ - (y) -= 97; /* a-z -> 00 - 25 */ \ - while (/*CONSTCOND*/0) - -#define INDEX(maj, min) \ - ((maj) * 26 * 3) + ((min) * 3) - -#define SLOTCMP(slot, val) \ - (mdoc_macronames[(slot)][0] == (val)[0] && \ - mdoc_macronames[(slot)][1] == (val)[1] && \ - (0 == (val)[2] || \ - mdoc_macronames[(slot)][2] == (val)[2])) +static unsigned char table[27 * 12]; void -mdoc_hash_free(void *htab) +mdoc_hash_init(void) { + int i, j, major; + const char *p; - free(htab); -} - - - -void * -mdoc_hash_alloc(void) -{ - int i, major, minor, ind; - const void **htab; - - htab = calloc(26 * 3 * 52, sizeof(struct mdoc_macro *)); - if (NULL == htab) - return(NULL); + memset(table, UCHAR_MAX, sizeof(table)); for (i = 0; i < MDOC_MAX; i++) { - major = mdoc_macronames[i][0]; - assert(isalpha((u_char)major) || 37 == major); + p = mdoc_macronames[i]; - ADJUST_MAJOR(major); + if (isalpha((u_char)p[1])) + major = 12 * (tolower((u_char)p[1]) - 97); + else + major = 12 * 26; - minor = mdoc_macronames[i][1]; - assert(isalpha((u_char)minor) || 49 == minor); + for (j = 0; j < 12; j++) + if (UCHAR_MAX == table[major + j]) { + table[major + j] = i; + break; + } - ADJUST_MINOR(minor); - - ind = INDEX(major, minor); - - if (NULL == htab[ind]) { - htab[ind] = &mdoc_macros[i]; - continue; - } - - if (NULL == htab[++ind]) { - htab[ind] = &mdoc_macros[i]; - continue; - } - - assert(NULL == htab[++ind]); - htab[ind] = &mdoc_macros[i]; + assert(j < 12); } - - return((void *)htab); } int -mdoc_hash_find(const void *arg, const char *tmp) +mdoc_hash_find(const char *p) { - int major, minor, ind, slot; - const void **htab; + int major, i, j; - htab = /* LINTED */ - (const void **)arg; - - if (0 == (major = tmp[0])) - return(MDOC_MAX); - if (0 == (minor = tmp[1])) + if (0 == p[0]) return(MDOC_MAX); - - if (tmp[2] && tmp[3]) + if ( ! isalpha((u_char)p[0]) && '%' != p[0]) return(MDOC_MAX); - if (37 != major && ! isalpha((u_char)major)) + if (isalpha((u_char)p[1])) + major = 12 * (tolower((u_char)p[1]) - 97); + else if ('1' == p[1]) + major = 12 * 26; + else return(MDOC_MAX); - if (49 != minor && ! isalpha((u_char)minor)) - return(MDOC_MAX); - - ADJUST_MAJOR(major); - ADJUST_MINOR(minor); - ind = INDEX(major, minor); - - if (ind < 0 || ind >= 26 * 3 * 52) + if (p[2] && p[3]) return(MDOC_MAX); - if (htab[ind]) { - slot = htab[ind] - /* LINTED */ - (void *)mdoc_macros; - assert(0 == (size_t)slot % sizeof(struct mdoc_macro)); - slot /= sizeof(struct mdoc_macro); - if (SLOTCMP(slot, tmp)) - return(slot); - ind++; - } - - if (htab[ind]) { - slot = htab[ind] - /* LINTED */ - (void *)mdoc_macros; - assert(0 == (size_t)slot % sizeof(struct mdoc_macro)); - slot /= sizeof(struct mdoc_macro); - if (SLOTCMP(slot, tmp)) - return(slot); - ind++; + for (j = 0; j < 12; j++) { + if (UCHAR_MAX == (i = table[major + j])) + break; + if (0 == strcmp(p, mdoc_macronames[i])) + return(i); } - if (NULL == htab[ind]) - return(MDOC_MAX); - slot = htab[ind] - /* LINTED */ - (void *)mdoc_macros; - assert(0 == (size_t)slot % sizeof(struct mdoc_macro)); - slot /= sizeof(struct mdoc_macro); - if (SLOTCMP(slot, tmp)) - return(slot); - return(MDOC_MAX); } diff --git a/mdoc_macro.c b/mdoc_macro.c index e6cf717f..704fbd8b 100644 --- a/mdoc_macro.c +++ b/mdoc_macro.c @@ -275,7 +275,7 @@ lookup_raw(struct mdoc *mdoc, const char *p) { int res; - if (MDOC_MAX == (res = mdoc_hash_find(mdoc->htab, p))) + if (MDOC_MAX == (res = mdoc_hash_find(p))) return(MDOC_MAX); if (MDOC_CALLABLE & mdoc_macros[res].flags) return(res); |