tech@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: Ingo Schwarze <schwarze@usta.de>
To: tech@mdocml.bsd.lv
Subject: Re: roff.c question
Date: Sat, 11 Dec 2010 00:12:42 +0100	[thread overview]
Message-ID: <20101210231242.GD18607@iris.usta.de> (raw)
In-Reply-To: <20101210211020.GC18607@iris.usta.de>

Hi,

> i'm tempted to do some measurements...

I took the ksh(1) manual, which is rather large,
but a real-word manual with few user-defined strings,
concatenated it 20 times to iteslf (only one header,
of course), and added one .ds near the beginning,
such that roff_res() gets called.  The result is a bit
above 100k lines, two and a half MB total size.

Timing for mandoc -Tlint -Wfatal test.1 on my notebook is:

  three checks up front:  1.40s
  delayed checks:         1.39s
  with strchr:            1.39s

  calling roff_res N times instead of once:
  N=0:                    1.37s
  N=100, strchr:          3.25s
  N=100, up front:        4.45s

So, time spent in roff_res() is about 2.2 percent of
the total parsing time, and we can save nearly 40 percent
of these 2.2 percent.

Thus, on a typical mdoc(7) manual, we economize
between 0.8 and 0.9 percent parsing time with these
optimizations.  Rendering time is about 0.55s,
so we save about 0.6 percent total time.

Given that the rendering time for the ksh(1) manual
is 100 milliseconds on my notebook, we can save
600 microseconds in absolute numbers, on a large
manual.

Thus, here is a version using these optimizations, which
fortunately does not make the code more complicated.
I diffed against OpenBSD, because that makes the
diff easier to understand.

Yours,
  Ingo


Index: roff.c
===================================================================
RCS file: /cvs/src/usr.bin/mandoc/roff.c,v
retrieving revision 1.23
diff -u -p -r1.23 roff.c
--- roff.c	9 Dec 2010 20:56:30 -0000	1.23
+++ roff.c	10 Dec 2010 22:58:19 -0000
@@ -345,18 +345,11 @@ roff_res(struct roff *r, char **bufp, si
 	size_t		 nsz;
 	char		*n;
 
-	/* String escape sequences have at least three characters. */
+	/* Search for a leading backslash and save a pointer to it. */
 
-	for (cp = *bufp + pos; cp[0] && cp[1] && cp[2]; cp++) {
-
-		/*
-		 * The first character must be a backslash.
-		 * Save a pointer to it.
-		 */
-
-		if ('\\' != *cp)
-			continue;
-		stesc = cp;
+	cp = *bufp + pos;
+	while (NULL != (cp = strchr(cp, '\\'))) {
+		stesc = cp++;
 
 		/*
 		 * The second character must be an asterisk.
@@ -364,7 +357,9 @@ roff_res(struct roff *r, char **bufp, si
 		 * so it can't start another escape sequence.
 		 */
 
-		if ('*' != *(++cp))
+		if ('\0' == *cp)
+			return(1);
+		if ('*' != *cp++)
 			continue;
 
 		/*
@@ -373,7 +368,9 @@ roff_res(struct roff *r, char **bufp, si
 		 * Save a pointer to the name.
 		 */
 
-		switch (*(++cp)) {
+		switch (*cp) {
+		case ('\0'):
+			return(1);
 		case ('('):
 			cp++;
 			maxl = 2;
--
 To unsubscribe send an email to tech+unsubscribe@mdocml.bsd.lv

  parent reply	other threads:[~2010-12-10 23:12 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-01 16:33 exit_status persistence Kristaps Dzonsons
2010-12-01 16:41 ` Kristaps Dzonsons
2010-12-01 21:28 ` Ingo Schwarze
2010-12-02 10:51   ` exit_status persistence (now: roff.c question) Kristaps Dzonsons
2010-12-02 13:29     ` Kristaps Dzonsons
2010-12-02 22:50       ` roff.c question Ingo Schwarze
2010-12-03 21:49         ` Ingo Schwarze
2010-12-05 15:15           ` Kristaps Dzonsons
2010-12-08  1:05             ` Ingo Schwarze
2010-12-10  9:40               ` Kristaps Dzonsons
2010-12-10 20:45                 ` Ingo Schwarze
2010-12-10 20:52                   ` Joerg Sonnenberger
2010-12-10 21:10                     ` Ingo Schwarze
2010-12-10 21:17                       ` Joerg Sonnenberger
2010-12-10 23:12                       ` Ingo Schwarze [this message]
2010-12-03 23:31         ` Ingo Schwarze
2010-12-05 15:17           ` Kristaps Dzonsons
2010-12-09 23:45             ` Ingo Schwarze
2010-12-10  9:32               ` Kristaps Dzonsons
2010-12-02 20:54     ` 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=20101210231242.GD18607@iris.usta.de \
    --to=schwarze@usta.de \
    --cc=tech@mdocml.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).