aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/mdoc_hash.c
diff options
context:
space:
mode:
authorKristaps Dzonsons <kristaps@bsd.lv>2009-09-16 14:40:56 +0000
committerKristaps Dzonsons <kristaps@bsd.lv>2009-09-16 14:40:56 +0000
commit374dd658a605a794b307642e87295f468d29d56a (patch)
treeeeda12f0d84b8e08c64a3590a881d337b2a26b49 /mdoc_hash.c
parent6cbefd50d90dc947c06ea2212c4d3552c4a36c84 (diff)
downloadmandoc-374dd658a605a794b307642e87295f468d29d56a.tar.gz
mandoc-374dd658a605a794b307642e87295f468d29d56a.tar.zst
mandoc-374dd658a605a794b307642e87295f468d29d56a.zip
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.
Diffstat (limited to 'mdoc_hash.c')
-rw-r--r--mdoc_hash.c145
1 files changed, 33 insertions, 112 deletions
diff --git a/mdoc_hash.c b/mdoc_hash.c
index 5a8ff655..7f033f4e 100644
--- a/mdoc_hash.c
+++ b/mdoc_hash.c
@@ -1,4 +1,4 @@
-/* $Id: mdoc_hash.c,v 1.8 2009/07/20 20:49:22 kristaps Exp $ */
+/* $Id: mdoc_hash.c,v 1.9 2009/09/16 14:40:56 kristaps Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -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);
}