tech@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: Wesley Moore <wes@wezm.net>
To: tech@mandoc.bsd.lv
Cc: Wesley Moore <wes@wezm.net>
Subject: [PATCH 3/3] Use ohash for predefined strings
Date: Mon, 13 Nov 2023 11:42:45 +1000	[thread overview]
Message-ID: <20231113014330.2247710-4-wes@wezm.net> (raw)
In-Reply-To: <20231113014330.2247710-1-wes@wezm.net>

---
 roff.c | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)

diff --git a/roff.c b/roff.c
index 74afaab..ff85a63 100644
--- a/roff.c
+++ b/roff.c
@@ -113,6 +113,7 @@ struct	roff {
 	struct mctx	*mstack; /* stack of macro contexts */
 	int		*rstack; /* stack of inverted `ie' values */
 	struct ohash	*reqtab; /* request lookup table */
+	struct ohash	*pretab; /* predefined strings table */
 	struct roffreg	*regtab; /* number registers */
 	struct ohash	*strtab; /* user-defined strings & macros */
 	struct ohash	*rentab; /* renamed strings & macros */
@@ -241,6 +242,7 @@ static	int		 roff_parse_comment(struct roff *, struct buf *,
 				int, int, char);
 static	int		 roff_parsetext(struct roff *, struct buf *,
 				int, int *);
+static	struct ohash	*roff_pretab_alloc(void);
 static	int		 roff_renamed(ROFF_ARGS);
 static	int		 roff_req_or_macro(ROFF_ARGS);
 static	int		 roff_return(ROFF_ARGS);
@@ -700,6 +702,21 @@ roffhash_find(struct ohash *htab, const char *name, size_t sz)
 
 /* --- key-value table ---------------------------------------------------- */
 
+struct ohash *
+roff_pretab_alloc(void)
+{
+	int i;
+	struct ohash *htab;
+	struct predef pre;
+
+	htab = roff_strhash_alloc();
+	for (i = 0; i < PREDEFS_MAX; i++) {
+		pre = predefs[i];
+		roff_setentry(htab, pre.name, strlen(pre.name), pre.str, strlen(pre.str), 0);
+	}
+	return htab;
+}
+
 struct ohash *
 roff_strhash_alloc(void)
 {
@@ -852,6 +869,7 @@ roff_free(struct roff *r)
 	int		 i;
 
 	roff_free1(r);
+	roff_strhash_free(r->pretab);
 	for (i = 0; i < r->mstacksz; i++)
 		free(r->mstack[i].argv);
 	free(r->mstack);
@@ -866,6 +884,7 @@ roff_alloc(int options)
 
 	r = mandoc_calloc(1, sizeof(struct roff));
 	r->reqtab = roffhash_alloc(0, ROFF_RENAMED);
+	r->pretab = roff_pretab_alloc();
 	r->strtab = roff_strhash_alloc();
 	r->rentab = roff_strhash_alloc();
 	r->options = options | MPARSE_COMMENT;
@@ -4318,8 +4337,8 @@ static const char *
 roff_getstrn(struct roff *r, const char *name, size_t len,
     int *deftype)
 {
-	int			 found, i;
 	const struct roff_entry	*entry;
+	int			 found;
 	enum roff_tok		 tok;
 
 	found = 0;
@@ -4341,16 +4360,13 @@ roff_getstrn(struct roff *r, const char *name, size_t len,
 			found = 1;
 		}
 	}
-	for (i = 0; i < PREDEFS_MAX; i++) {
-		if (strncmp(name, predefs[i].name, len) != 0 ||
-		    predefs[i].name[len] != '\0')
-			continue;
+	entry = roff_strhash_find(r->pretab, name, len);
+	if (entry != NULL) {
 		if (*deftype & ROFFDEF_PRE) {
 			*deftype = ROFFDEF_PRE;
-			return predefs[i].str;
+			return entry->val.p;
 		} else {
 			found = 1;
-			break;
 		}
 	}
 	if (r->man->meta.macroset != MACROSET_MAN) {
-- 
2.42.1

--
 To unsubscribe send an email to tech+unsubscribe@mandoc.bsd.lv


  parent reply	other threads:[~2023-11-13  1:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-13  1:42 Improve performance of makewhatis Wesley Moore
2023-11-13  1:42 ` [PATCH 1/3] Use ohash for strtab Wesley Moore
2023-11-13  1:42 ` [PATCH 2/3] Use ohash for rentab Wesley Moore
2023-11-13  1:42 ` Wesley Moore [this message]
2023-11-15  1:26 ` Improve performance of makewhatis Ingo Schwarze
2023-11-15  8:29   ` Wesley Moore
2023-11-15 17:36     ` Ingo Schwarze

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231113014330.2247710-4-wes@wezm.net \
    --to=wes@wezm.net \
    --cc=tech@mandoc.bsd.lv \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).