From: Paul Eggert <eggert@cs.ucla.edu>
To: bug-gnulib@gnu.org, emacs-devel@gnu.org
Cc: Paul Eggert <eggert@cs.ucla.edu>
Subject: [PATCH 3/4] dfa: shrink constraints from 4 bits to 3
Date: Tue, 10 Jan 2017 02:13:31 -0800 [thread overview]
Message-ID: <20170110101332.15354-3-eggert@cs.ucla.edu> (raw)
In-Reply-To: <20170110101332.15354-1-eggert@cs.ucla.edu>
* lib/dfa.c (newline_constraint, letter_constraint)
(other_constraint, prev_newline_dependent)
(prev_letter_dependent, NO_CONSTRAINT, BEGLINE_CONSTRAINT)
(ENDLINE_CONSTRAINT, BEGWORD_CONSTRAINT, ENDWORD_CONSTRAINT)
(LIMWORD_CONSTRAINT, NOTLIMWORD_CONSTRAINT):
Constraints need only 3 bits, not 4. Using smaller integers
shrinks the code a bit and makes grep a tad faster on x86-64.
---
ChangeLog | 9 +++++++++
lib/dfa.c | 32 ++++++++++++++++----------------
2 files changed, 25 insertions(+), 16 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 91cdc6d..e0b73b5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
2017-01-09 Paul Eggert <eggert@cs.ucla.edu>
+ dfa: shrink constraints from 4 bits to 3
+ * lib/dfa.c (newline_constraint, letter_constraint)
+ (other_constraint, prev_newline_dependent)
+ (prev_letter_dependent, NO_CONSTRAINT, BEGLINE_CONSTRAINT)
+ (ENDLINE_CONSTRAINT, BEGWORD_CONSTRAINT, ENDWORD_CONSTRAINT)
+ (LIMWORD_CONSTRAINT, NOTLIMWORD_CONSTRAINT):
+ Constraints need only 3 bits, not 4. Using smaller integers
+ shrinks the code a bit and makes grep a tad faster on x86-64.
+
dfa: omit unnecessary ptrdiff_t check
* lib/dfa.c (alloc_position_set): Do not worry about ptrdiff_t
overflow, since xnmalloc does that now.
diff --git a/lib/dfa.c b/lib/dfa.c
index 509d6d1..28678c2 100644
--- a/lib/dfa.c
+++ b/lib/dfa.c
@@ -137,13 +137,13 @@ enum
/* Sometimes characters can only be matched depending on the surrounding
context. Such context decisions depend on what the previous character
was, and the value of the current (lookahead) character. Context
- dependent constraints are encoded as 12-bit integers. Each bit that
+ dependent constraints are encoded as 9-bit integers. Each bit that
is set indicates that the constraint succeeds in the corresponding
context.
- bit 8-11 - valid contexts when next character is CTX_NEWLINE
- bit 4-7 - valid contexts when next character is CTX_LETTER
- bit 0-3 - valid contexts when next character is CTX_NONE
+ bit 6-8 - valid contexts when next character is CTX_NEWLINE
+ bit 3-5 - valid contexts when next character is CTX_LETTER
+ bit 0-2 - valid contexts when next character is CTX_NONE
succeeds_in_context determines whether a given constraint
succeeds in a particular context. Prev is a bitmask of possible
@@ -152,17 +152,17 @@ enum
static int
newline_constraint (int constraint)
{
- return (constraint >> 8) & 0xf;
+ return (constraint >> 6) & 7;
}
static int
letter_constraint (int constraint)
{
- return (constraint >> 4) & 0xf;
+ return (constraint >> 3) & 7;
}
static int
other_constraint (int constraint)
{
- return constraint & 0xf;
+ return constraint & 7;
}
static bool
@@ -178,12 +178,12 @@ succeeds_in_context (int constraint, int prev, int curr)
static bool
prev_newline_dependent (int constraint)
{
- return ((constraint ^ constraint >> 2) & 0x111) != 0;
+ return ((constraint ^ constraint >> 2) & 0111) != 0;
}
static bool
prev_letter_dependent (int constraint)
{
- return ((constraint ^ constraint >> 1) & 0x111) != 0;
+ return ((constraint ^ constraint >> 1) & 0111) != 0;
}
/* Tokens that match the empty string subject to some constraint actually
@@ -192,13 +192,13 @@ prev_letter_dependent (int constraint)
the constraints corresponding to the special tokens previously defined. */
enum
{
- NO_CONSTRAINT = 0x777,
- BEGLINE_CONSTRAINT = 0x444,
- ENDLINE_CONSTRAINT = 0x700,
- BEGWORD_CONSTRAINT = 0x050,
- ENDWORD_CONSTRAINT = 0x202,
- LIMWORD_CONSTRAINT = 0x252,
- NOTLIMWORD_CONSTRAINT = 0x525
+ NO_CONSTRAINT = 0777,
+ BEGLINE_CONSTRAINT = 0444,
+ ENDLINE_CONSTRAINT = 0700,
+ BEGWORD_CONSTRAINT = 0050,
+ ENDWORD_CONSTRAINT = 0202,
+ LIMWORD_CONSTRAINT = 0252,
+ NOTLIMWORD_CONSTRAINT = 0525
};
/* The regexp is parsed into an array of tokens in postfix form. Some tokens
--
2.9.3
next prev parent reply other threads:[~2017-01-10 10:13 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-01-10 10:13 [PATCH 1/4] dfa: omit unnecessary allocation Paul Eggert
2017-01-10 10:13 ` [PATCH 2/4] dfa: omit unnecessary ptrdiff_t check Paul Eggert
2017-01-10 10:13 ` Paul Eggert [this message]
2017-01-10 10:13 ` [PATCH 4/4] dfa: minor simplification with emptyset Paul Eggert
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170110101332.15354-3-eggert@cs.ucla.edu \
--to=eggert@cs.ucla.edu \
--cc=bug-gnulib@gnu.org \
--cc=emacs-devel@gnu.org \
/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.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
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 read-only IMAP folder(s) and NNTP newsgroup(s).