ce426f
commit e91a48408662e591e1320b2cab6d3fcda7793b18
ce426f
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
ce426f
Date:   Fri Jun 28 16:24:16 2013 +0530
ce426f
ce426f
    Simplify strcoll implementation
ce426f
ce426f
diff --git glibc-2.17-c758a686/string/strcoll_l.c glibc-2.17-c758a686/string/strcoll_l.c
ce426f
index ecda08f..1bb9e23 100644
ce426f
--- glibc-2.17-c758a686/string/strcoll_l.c
ce426f
+++ glibc-2.17-c758a686/string/strcoll_l.c
ce426f
@@ -41,11 +41,244 @@
ce426f
 
ce426f
 #include "../locale/localeinfo.h"
ce426f
 
ce426f
+/* Track status while looking for sequences in a string.  */
ce426f
+typedef struct
ce426f
+{
ce426f
+  int len;			/* Length of the current sequence.  */
ce426f
+  int val;			/* Position of the sequence relative to the
ce426f
+				   previous non-ignored sequence.  */
ce426f
+  size_t idxnow;		/* Current index in sequences.  */
ce426f
+  size_t idxmax;		/* Maximum index in sequences.  */
ce426f
+  size_t idxcnt;		/* Current count of indeces.  */
ce426f
+  size_t backw;			/* Current Backward sequence index.  */
ce426f
+  size_t backw_stop;		/* Index where the backward sequences stop.  */
ce426f
+  const USTRING_TYPE *us;	/* The string.  */
ce426f
+  int32_t *idxarr;		/* Array to cache weight indeces.  */
ce426f
+  unsigned char *rulearr;	/* Array to cache rules.  */
ce426f
+} coll_seq;
ce426f
+
ce426f
+/* Get next sequence.  The weight indeces are cached, so we don't need to
ce426f
+   traverse the string.  */
ce426f
+static void
ce426f
+get_next_seq_cached (coll_seq *seq, int nrules, int pass,
ce426f
+		     const unsigned char *rulesets,
ce426f
+		     const USTRING_TYPE *weights)
ce426f
+{
ce426f
+  int val = seq->val = 0;
ce426f
+  int len = seq->len;
ce426f
+  size_t backw_stop = seq->backw_stop;
ce426f
+  size_t backw = seq->backw;
ce426f
+  size_t idxcnt = seq->idxcnt;
ce426f
+  size_t idxmax = seq->idxmax;
ce426f
+  size_t idxnow = seq->idxnow;
ce426f
+  unsigned char *rulearr = seq->rulearr;
ce426f
+  int32_t *idxarr = seq->idxarr;
ce426f
+
ce426f
+  while (len == 0)
ce426f
+    {
ce426f
+      ++val;
ce426f
+      if (backw_stop != ~0ul)
ce426f
+	{
ce426f
+	  /* The is something pushed.  */
ce426f
+	  if (backw == backw_stop)
ce426f
+	    {
ce426f
+	      /* The last pushed character was handled.  Continue
ce426f
+		 with forward characters.  */
ce426f
+	      if (idxcnt < idxmax)
ce426f
+		{
ce426f
+		  idxnow = idxcnt;
ce426f
+		  backw_stop = ~0ul;
ce426f
+		}
ce426f
+	      else
ce426f
+		{
ce426f
+		  /* Nothing anymore.  The backward sequence
ce426f
+		     ended with the last sequence in the string.  */
ce426f
+		  idxnow = ~0ul;
ce426f
+		  break;
ce426f
+		}
ce426f
+	    }
ce426f
+	  else
ce426f
+	    idxnow = --backw;
ce426f
+	}
ce426f
+      else
ce426f
+	{
ce426f
+	  backw_stop = idxcnt;
ce426f
+
ce426f
+	  while (idxcnt < idxmax)
ce426f
+	    {
ce426f
+	      if ((rulesets[rulearr[idxcnt] * nrules + pass]
ce426f
+		   & sort_backward) == 0)
ce426f
+		/* No more backward characters to push.  */
ce426f
+		break;
ce426f
+	      ++idxcnt;
ce426f
+	    }
ce426f
+
ce426f
+	  if (backw_stop == idxcnt)
ce426f
+	    {
ce426f
+	      /* No sequence at all or just one.  */
ce426f
+	      if (idxcnt == idxmax)
ce426f
+		/* Note that seq1len is still zero.  */
ce426f
+		break;
ce426f
+
ce426f
+	      backw_stop = ~0ul;
ce426f
+	      idxnow = idxcnt++;
ce426f
+	    }
ce426f
+	  else
ce426f
+	    /* We pushed backward sequences.  */
ce426f
+	    idxnow = backw = idxcnt - 1;
ce426f
+	}
ce426f
+      len = weights[idxarr[idxnow]++];
ce426f
+    }
ce426f
+
ce426f
+  /* Update the structure.  */
ce426f
+  seq->val = val;
ce426f
+  seq->len = len;
ce426f
+  seq->backw_stop = backw_stop;
ce426f
+  seq->backw = backw;
ce426f
+  seq->idxcnt = idxcnt;
ce426f
+  seq->idxnow = idxnow;
ce426f
+}
ce426f
+
ce426f
+/* Get next sequence.  Traverse the string as required.  */
ce426f
+static void
ce426f
+get_next_seq (coll_seq *seq, int nrules, const unsigned char *rulesets,
ce426f
+	      const USTRING_TYPE *weights, const int32_t *table,
ce426f
+	      const USTRING_TYPE *extra, const int32_t *indirect)
ce426f
+{
ce426f
+#include WEIGHT_H
ce426f
+  int val = seq->val = 0;
ce426f
+  int len = seq->len;
ce426f
+  size_t backw_stop = seq->backw_stop;
ce426f
+  size_t backw = seq->backw;
ce426f
+  size_t idxcnt = seq->idxcnt;
ce426f
+  size_t idxmax = seq->idxmax;
ce426f
+  size_t idxnow = seq->idxnow;
ce426f
+  unsigned char *rulearr = seq->rulearr;
ce426f
+  int32_t *idxarr = seq->idxarr;
ce426f
+  const USTRING_TYPE *us = seq->us;
ce426f
+
ce426f
+  while (len == 0)
ce426f
+    {
ce426f
+      ++val;
ce426f
+      if (backw_stop != ~0ul)
ce426f
+	{
ce426f
+	  /* The is something pushed.  */
ce426f
+	  if (backw == backw_stop)
ce426f
+	    {
ce426f
+	      /* The last pushed character was handled.  Continue
ce426f
+		 with forward characters.  */
ce426f
+	      if (idxcnt < idxmax)
ce426f
+		{
ce426f
+		  idxnow = idxcnt;
ce426f
+		  backw_stop = ~0ul;
ce426f
+		}
ce426f
+	      else
ce426f
+		/* Nothing anymore.  The backward sequence ended with
ce426f
+		   the last sequence in the string.  Note that seq2len
ce426f
+		   is still zero.  */
ce426f
+		break;
ce426f
+	    }
ce426f
+	  else
ce426f
+	    idxnow = --backw;
ce426f
+	}
ce426f
+      else
ce426f
+	{
ce426f
+	  backw_stop = idxmax;
ce426f
+
ce426f
+	  while (*us != L('\0'))
ce426f
+	    {
ce426f
+	      int32_t tmp = findidx (&us, -1);
ce426f
+	      rulearr[idxmax] = tmp >> 24;
ce426f
+	      idxarr[idxmax] = tmp & 0xffffff;
ce426f
+	      idxcnt = idxmax++;
ce426f
+
ce426f
+	      if ((rulesets[rulearr[idxcnt] * nrules]
ce426f
+		   & sort_backward) == 0)
ce426f
+		/* No more backward characters to push.  */
ce426f
+		break;
ce426f
+	      ++idxcnt;
ce426f
+	    }
ce426f
+
ce426f
+	  if (backw_stop >= idxcnt)
ce426f
+	    {
ce426f
+	      /* No sequence at all or just one.  */
ce426f
+	      if (idxcnt == idxmax || backw_stop > idxcnt)
ce426f
+		/* Note that seq1len is still zero.  */
ce426f
+		break;
ce426f
+
ce426f
+	      backw_stop = ~0ul;
ce426f
+	      idxnow = idxcnt;
ce426f
+	    }
ce426f
+	  else
ce426f
+	    /* We pushed backward sequences.  */
ce426f
+	    idxnow = backw = idxcnt - 1;
ce426f
+	}
ce426f
+      len = weights[idxarr[idxnow]++];
ce426f
+    }
ce426f
+
ce426f
+  /* Update the structure.  */
ce426f
+  seq->val = val;
ce426f
+  seq->len = len;
ce426f
+  seq->backw_stop = backw_stop;
ce426f
+  seq->backw = backw;
ce426f
+  seq->idxcnt = idxcnt;
ce426f
+  seq->idxmax = idxmax;
ce426f
+  seq->idxnow = idxnow;
ce426f
+  seq->us = us;
ce426f
+}
ce426f
+
ce426f
+/* Compare two sequences.  */
ce426f
+static int
ce426f
+do_compare (coll_seq *seq1, coll_seq *seq2, int position,
ce426f
+	    const USTRING_TYPE *weights)
ce426f
+{
ce426f
+  int seq1len = seq1->len;
ce426f
+  int seq2len = seq2->len;
ce426f
+  int val1 = seq1->val;
ce426f
+  int val2 = seq2->val;
ce426f
+  int32_t *idx1arr = seq1->idxarr;
ce426f
+  int32_t *idx2arr = seq2->idxarr;
ce426f
+  int idx1now = seq1->idxnow;
ce426f
+  int idx2now = seq2->idxnow;
ce426f
+  int result = 0;
ce426f
+
ce426f
+  /* Test for position if necessary.  */
ce426f
+  if (position && val1 != val2)
ce426f
+    {
ce426f
+      result = val1 - val2;
ce426f
+      goto out;
ce426f
+    }
ce426f
+
ce426f
+  /* Compare the two sequences.  */
ce426f
+  do
ce426f
+    {
ce426f
+      if (weights[idx1arr[idx1now]] != weights[idx2arr[idx2now]])
ce426f
+	{
ce426f
+	  /* The sequences differ.  */
ce426f
+	  result = weights[idx1arr[idx1now]] - weights[idx2arr[idx2now]];
ce426f
+	  goto out;
ce426f
+	}
ce426f
+
ce426f
+      /* Increment the offsets.  */
ce426f
+      ++idx1arr[idx1now];
ce426f
+      ++idx2arr[idx2now];
ce426f
+
ce426f
+      --seq1len;
ce426f
+      --seq2len;
ce426f
+    }
ce426f
+  while (seq1len > 0 && seq2len > 0);
ce426f
+
ce426f
+  if (position && seq1len != seq2len)
ce426f
+    result = seq1len - seq2len;
ce426f
+
ce426f
+out:
ce426f
+  seq1->len = seq1len;
ce426f
+  seq2->len = seq2len;
ce426f
+  return result;
ce426f
+}
ce426f
+
ce426f
 int
ce426f
-STRCOLL (s1, s2, l)
ce426f
-     const STRING_TYPE *s1;
ce426f
-     const STRING_TYPE *s2;
ce426f
-     __locale_t l;
ce426f
+STRCOLL (const STRING_TYPE *s1, const STRING_TYPE *s2, __locale_t l)
ce426f
 {
ce426f
   struct __locale_data *current = l->__locales[LC_COLLATE];
ce426f
   uint_fast32_t nrules = current->values[_NL_ITEM_INDEX (_NL_COLLATE_NRULES)].word;
ce426f
@@ -56,34 +289,6 @@ STRCOLL (s1, s2, l)
ce426f
   const USTRING_TYPE *weights;
ce426f
   const USTRING_TYPE *extra;
ce426f
   const int32_t *indirect;
ce426f
-  uint_fast32_t pass;
ce426f
-  int result = 0;
ce426f
-  const USTRING_TYPE *us1;
ce426f
-  const USTRING_TYPE *us2;
ce426f
-  size_t s1len;
ce426f
-  size_t s2len;
ce426f
-  int32_t *idx1arr;
ce426f
-  int32_t *idx2arr;
ce426f
-  unsigned char *rule1arr;
ce426f
-  unsigned char *rule2arr;
ce426f
-  size_t idx1max;
ce426f
-  size_t idx2max;
ce426f
-  size_t idx1cnt;
ce426f
-  size_t idx2cnt;
ce426f
-  size_t idx1now;
ce426f
-  size_t idx2now;
ce426f
-  size_t backw1_stop;
ce426f
-  size_t backw2_stop;
ce426f
-  size_t backw1;
ce426f
-  size_t backw2;
ce426f
-  int val1;
ce426f
-  int val2;
ce426f
-  int position;
ce426f
-  int seq1len;
ce426f
-  int seq2len;
ce426f
-  int use_malloc;
ce426f
-
ce426f
-#include WEIGHT_H
ce426f
 
ce426f
   if (nrules == 0)
ce426f
     return STRCMP (s1, s2);
ce426f
@@ -98,7 +303,6 @@ STRCOLL (s1, s2, l)
ce426f
     current->values[_NL_ITEM_INDEX (CONCAT(_NL_COLLATE_EXTRA,SUFFIX))].string;
ce426f
   indirect = (const int32_t *)
ce426f
     current->values[_NL_ITEM_INDEX (CONCAT(_NL_COLLATE_INDIRECT,SUFFIX))].string;
ce426f
-  use_malloc = 0;
ce426f
 
ce426f
   assert (((uintptr_t) table) % __alignof__ (table[0]) == 0);
ce426f
   assert (((uintptr_t) weights) % __alignof__ (weights[0]) == 0);
ce426f
@@ -106,18 +310,13 @@ STRCOLL (s1, s2, l)
ce426f
   assert (((uintptr_t) indirect) % __alignof__ (indirect[0]) == 0);
ce426f
 
ce426f
   /* We need this a few times.  */
ce426f
-  s1len = STRLEN (s1);
ce426f
-  s2len = STRLEN (s2);
ce426f
+  size_t s1len = STRLEN (s1);
ce426f
+  size_t s2len = STRLEN (s2);
ce426f
 
ce426f
   /* Catch empty strings.  */
ce426f
-  if (__builtin_expect (s1len == 0, 0) || __builtin_expect (s2len == 0, 0))
ce426f
+  if (__glibc_unlikely (s1len == 0) || __glibc_unlikely (s2len == 0))
ce426f
     return (s1len != 0) - (s2len != 0);
ce426f
 
ce426f
-  /* We need the elements of the strings as unsigned values since they
ce426f
-     are used as indeces.  */
ce426f
-  us1 = (const USTRING_TYPE *) s1;
ce426f
-  us2 = (const USTRING_TYPE *) s2;
ce426f
-
ce426f
   /* Perform the first pass over the string and while doing this find
ce426f
      and store the weights for each character.  Since we want this to
ce426f
      be as fast as possible we are using `alloca' to store the temporary
ce426f
@@ -127,14 +326,27 @@ STRCOLL (s1, s2, l)
ce426f
 
ce426f
      Please note that the localedef programs makes sure that `position'
ce426f
      is not used at the first level.  */
ce426f
+
ce426f
+  coll_seq seq1, seq2;
ce426f
+  bool use_malloc = false;
ce426f
+  int result = 0;
ce426f
+
ce426f
+  memset (&seq1, 0, sizeof (seq1));
ce426f
+  seq2 = seq1;
ce426f
+
ce426f
+  /* We need the elements of the strings as unsigned values since they
ce426f
+     are used as indeces.  */
ce426f
+  seq1.us = (const USTRING_TYPE *) s1;
ce426f
+  seq2.us = (const USTRING_TYPE *) s2;
ce426f
+
ce426f
   if (! __libc_use_alloca ((s1len + s2len) * (sizeof (int32_t) + 1)))
ce426f
     {
ce426f
-      idx1arr = (int32_t *) malloc ((s1len + s2len) * (sizeof (int32_t) + 1));
ce426f
-      idx2arr = &idx1arr[s1len];
ce426f
-      rule1arr = (unsigned char *) &idx2arr[s2len];
ce426f
-      rule2arr = &rule1arr[s1len];
ce426f
+      seq1.idxarr = (int32_t *) malloc ((s1len + s2len) * (sizeof (int32_t) + 1));
ce426f
+      seq2.idxarr = &seq1.idxarr[s1len];
ce426f
+      seq1.rulearr = (unsigned char *) &seq2.idxarr[s2len];
ce426f
+      seq2.rulearr = &seq1.rulearr[s1len];
ce426f
 
ce426f
-      if (idx1arr == NULL)
ce426f
+      if (seq1.idxarr == NULL)
ce426f
 	/* No memory.  Well, go with the stack then.
ce426f
 
ce426f
 	   XXX Once this implementation is stable we will handle this
ce426f
@@ -142,396 +354,73 @@ STRCOLL (s1, s2, l)
ce426f
 	   do this in time.  This means, though, that this happens for
ce426f
 	   every pass again.  */
ce426f
 	goto try_stack;
ce426f
-      use_malloc = 1;
ce426f
+      use_malloc = true;
ce426f
     }
ce426f
   else
ce426f
     {
ce426f
     try_stack:
ce426f
-      idx1arr = (int32_t *) alloca (s1len * sizeof (int32_t));
ce426f
-      idx2arr = (int32_t *) alloca (s2len * sizeof (int32_t));
ce426f
-      rule1arr = (unsigned char *) alloca (s1len);
ce426f
-      rule2arr = (unsigned char *) alloca (s2len);
ce426f
+      seq1.idxarr = (int32_t *) alloca (s1len * sizeof (int32_t));
ce426f
+      seq2.idxarr = (int32_t *) alloca (s2len * sizeof (int32_t));
ce426f
+      seq1.rulearr = (unsigned char *) alloca (s1len);
ce426f
+      seq2.rulearr = (unsigned char *) alloca (s2len);
ce426f
     }
ce426f
 
ce426f
-  idx1cnt = 0;
ce426f
-  idx2cnt = 0;
ce426f
-  idx1max = 0;
ce426f
-  idx2max = 0;
ce426f
-  idx1now = 0;
ce426f
-  idx2now = 0;
ce426f
-  backw1_stop = ~0ul;
ce426f
-  backw2_stop = ~0ul;
ce426f
-  backw1 = ~0ul;
ce426f
-  backw2 = ~0ul;
ce426f
-  seq1len = 0;
ce426f
-  seq2len = 0;
ce426f
-  position = rulesets[0] & sort_position;
ce426f
-  while (1)
ce426f
-    {
ce426f
-      val1 = 0;
ce426f
-      val2 = 0;
ce426f
-
ce426f
-      /* Get the next non-IGNOREd element for string `s1'.  */
ce426f
-      if (seq1len == 0)
ce426f
-	do
ce426f
-	  {
ce426f
-	    ++val1;
ce426f
-
ce426f
-	    if (backw1_stop != ~0ul)
ce426f
-	      {
ce426f
-		/* The is something pushed.  */
ce426f
-		if (backw1 == backw1_stop)
ce426f
-		  {
ce426f
-		    /* The last pushed character was handled.  Continue
ce426f
-		       with forward characters.  */
ce426f
-		    if (idx1cnt < idx1max)
ce426f
-		      {
ce426f
-			idx1now = idx1cnt;
ce426f
-			backw1_stop = ~0ul;
ce426f
-		      }
ce426f
-		    else
ce426f
-		      /* Nothing anymore.  The backward sequence ended with
ce426f
-			 the last sequence in the string.  Note that seq1len
ce426f
-			 is still zero.  */
ce426f
-		      break;
ce426f
-		  }
ce426f
-		else
ce426f
-		  idx1now = --backw1;
ce426f
-	      }
ce426f
-	    else
ce426f
-	      {
ce426f
-		backw1_stop = idx1max;
ce426f
-
ce426f
-		while (*us1 != L('\0'))
ce426f
-		  {
ce426f
-		    int32_t tmp = findidx (&us1, -1);
ce426f
-		    rule1arr[idx1max] = tmp >> 24;
ce426f
-		    idx1arr[idx1max] = tmp & 0xffffff;
ce426f
-		    idx1cnt = idx1max++;
ce426f
-
ce426f
-		    if ((rulesets[rule1arr[idx1cnt] * nrules]
ce426f
-			 & sort_backward) == 0)
ce426f
-		      /* No more backward characters to push.  */
ce426f
-		      break;
ce426f
-		    ++idx1cnt;
ce426f
-		  }
ce426f
-
ce426f
-		if (backw1_stop >= idx1cnt)
ce426f
-		  {
ce426f
-		    /* No sequence at all or just one.  */
ce426f
-		    if (idx1cnt == idx1max || backw1_stop > idx1cnt)
ce426f
-		      /* Note that seq1len is still zero.  */
ce426f
-		      break;
ce426f
-
ce426f
-		    backw1_stop = ~0ul;
ce426f
-		    idx1now = idx1cnt;
ce426f
-		  }
ce426f
-		else
ce426f
-		  /* We pushed backward sequences.  */
ce426f
-		  idx1now = backw1 = idx1cnt - 1;
ce426f
-	      }
ce426f
-	  }
ce426f
-	while ((seq1len = weights[idx1arr[idx1now]++]) == 0);
ce426f
-
ce426f
-      /* And the same for string `s2'.  */
ce426f
-      if (seq2len == 0)
ce426f
-	do
ce426f
-	  {
ce426f
-	    ++val2;
ce426f
-
ce426f
-	    if (backw2_stop != ~0ul)
ce426f
-	      {
ce426f
-		/* The is something pushed.  */
ce426f
-		if (backw2 == backw2_stop)
ce426f
-		  {
ce426f
-		    /* The last pushed character was handled.  Continue
ce426f
-		       with forward characters.  */
ce426f
-		    if (idx2cnt < idx2max)
ce426f
-		      {
ce426f
-			idx2now = idx2cnt;
ce426f
-			backw2_stop = ~0ul;
ce426f
-		      }
ce426f
-		    else
ce426f
-		      /* Nothing anymore.  The backward sequence ended with
ce426f
-			 the last sequence in the string.  Note that seq2len
ce426f
-			 is still zero.  */
ce426f
-		      break;
ce426f
-		  }
ce426f
-		else
ce426f
-		  idx2now = --backw2;
ce426f
-	      }
ce426f
-	    else
ce426f
-	      {
ce426f
-		backw2_stop = idx2max;
ce426f
-
ce426f
-		while (*us2 != L('\0'))
ce426f
-		  {
ce426f
-		    int32_t tmp = findidx (&us2, -1);
ce426f
-		    rule2arr[idx2max] = tmp >> 24;
ce426f
-		    idx2arr[idx2max] = tmp & 0xffffff;
ce426f
-		    idx2cnt = idx2max++;
ce426f
-
ce426f
-		    if ((rulesets[rule2arr[idx2cnt] * nrules]
ce426f
-			 & sort_backward) == 0)
ce426f
-		      /* No more backward characters to push.  */
ce426f
-		      break;
ce426f
-		    ++idx2cnt;
ce426f
-		  }
ce426f
-
ce426f
-		if (backw2_stop >= idx2cnt)
ce426f
-		  {
ce426f
-		    /* No sequence at all or just one.  */
ce426f
-		    if (idx2cnt == idx2max || backw2_stop > idx2cnt)
ce426f
-		      /* Note that seq1len is still zero.  */
ce426f
-		      break;
ce426f
-
ce426f
-		    backw2_stop = ~0ul;
ce426f
-		    idx2now = idx2cnt;
ce426f
-		  }
ce426f
-		else
ce426f
-		  /* We pushed backward sequences.  */
ce426f
-		  idx2now = backw2 = idx2cnt - 1;
ce426f
-	      }
ce426f
-	  }
ce426f
-	while ((seq2len = weights[idx2arr[idx2now]++]) == 0);
ce426f
-
ce426f
-      /* See whether any or both strings are empty.  */
ce426f
-      if (seq1len == 0 || seq2len == 0)
ce426f
-	{
ce426f
-	  if (seq1len == seq2len)
ce426f
-	    /* Both ended.  So far so good, both strings are equal at the
ce426f
-	       first level.  */
ce426f
-	    break;
ce426f
-
ce426f
-	  /* This means one string is shorter than the other.  Find out
ce426f
-	     which one and return an appropriate value.  */
ce426f
-	  result = seq1len == 0 ? -1 : 1;
ce426f
-	  goto free_and_return;
ce426f
-	}
ce426f
+  seq1.rulearr[0] = 0;
ce426f
 
ce426f
-      /* Test for position if necessary.  */
ce426f
-      if (position && val1 != val2)
ce426f
-	{
ce426f
-	  result = val1 - val2;
ce426f
-	  goto free_and_return;
ce426f
-	}
ce426f
-
ce426f
-      /* Compare the two sequences.  */
ce426f
-      do
ce426f
-	{
ce426f
-	  if (weights[idx1arr[idx1now]] != weights[idx2arr[idx2now]])
ce426f
-	    {
ce426f
-	      /* The sequences differ.  */
ce426f
-	      result = weights[idx1arr[idx1now]] - weights[idx2arr[idx2now]];
ce426f
-	      goto free_and_return;
ce426f
-	    }
ce426f
-
ce426f
-	  /* Increment the offsets.  */
ce426f
-	  ++idx1arr[idx1now];
ce426f
-	  ++idx2arr[idx2now];
ce426f
-
ce426f
-	  --seq1len;
ce426f
-	  --seq2len;
ce426f
-	}
ce426f
-      while (seq1len > 0 && seq2len > 0);
ce426f
-
ce426f
-      if (position && seq1len != seq2len)
ce426f
-	{
ce426f
-	  result = seq1len - seq2len;
ce426f
-	  goto free_and_return;
ce426f
-	}
ce426f
-    }
ce426f
-
ce426f
-  /* Now the remaining passes over the weights.  We now use the
ce426f
-     indeces we found before.  */
ce426f
-  for (pass = 1; pass < nrules; ++pass)
ce426f
+  /* Cache values in the first pass and if needed, use them in subsequent
ce426f
+     passes.  */
ce426f
+  for (int pass = 0; pass < nrules; ++pass)
ce426f
     {
ce426f
+      seq1.idxcnt = 0;
ce426f
+      seq1.backw_stop = ~0ul;
ce426f
+      seq1.backw = ~0ul;
ce426f
+      seq2.idxcnt = 0;
ce426f
+      seq2.backw_stop = ~0ul;
ce426f
+      seq2.backw = ~0ul;
ce426f
+
ce426f
       /* We assume that if a rule has defined `position' in one section
ce426f
 	 this is true for all of them.  */
ce426f
-      idx1cnt = 0;
ce426f
-      idx2cnt = 0;
ce426f
-      backw1_stop = ~0ul;
ce426f
-      backw2_stop = ~0ul;
ce426f
-      backw1 = ~0ul;
ce426f
-      backw2 = ~0ul;
ce426f
-      position = rulesets[rule1arr[0] * nrules + pass] & sort_position;
ce426f
+      int position = rulesets[seq1.rulearr[0] * nrules + pass] & sort_position;
ce426f
 
ce426f
       while (1)
ce426f
 	{
ce426f
-	  val1 = 0;
ce426f
-	  val2 = 0;
ce426f
-
ce426f
-	  /* Get the next non-IGNOREd element for string `s1'.  */
ce426f
-	  if (seq1len == 0)
ce426f
-	    do
ce426f
-	      {
ce426f
-		++val1;
ce426f
-
ce426f
-		if (backw1_stop != ~0ul)
ce426f
-		  {
ce426f
-		    /* The is something pushed.  */
ce426f
-		    if (backw1 == backw1_stop)
ce426f
-		      {
ce426f
-			/* The last pushed character was handled.  Continue
ce426f
-			   with forward characters.  */
ce426f
-			if (idx1cnt < idx1max)
ce426f
-			  {
ce426f
-			    idx1now = idx1cnt;
ce426f
-			    backw1_stop = ~0ul;
ce426f
-			  }
ce426f
-			else
ce426f
-			  {
ce426f
-			    /* Nothing anymore.  The backward sequence
ce426f
-			       ended with the last sequence in the string.  */
ce426f
-			    idx1now = ~0ul;
ce426f
-			    break;
ce426f
-			  }
ce426f
-		      }
ce426f
-		    else
ce426f
-		      idx1now = --backw1;
ce426f
-		  }
ce426f
-		else
ce426f
-		  {
ce426f
-		    backw1_stop = idx1cnt;
ce426f
-
ce426f
-		    while (idx1cnt < idx1max)
ce426f
-		      {
ce426f
-			if ((rulesets[rule1arr[idx1cnt] * nrules + pass]
ce426f
-			     & sort_backward) == 0)
ce426f
-			  /* No more backward characters to push.  */
ce426f
-			  break;
ce426f
-			++idx1cnt;
ce426f
-		      }
ce426f
-
ce426f
-		    if (backw1_stop == idx1cnt)
ce426f
-		      {
ce426f
-			/* No sequence at all or just one.  */
ce426f
-			if (idx1cnt == idx1max)
ce426f
-			  /* Note that seq1len is still zero.  */
ce426f
-			  break;
ce426f
-
ce426f
-			backw1_stop = ~0ul;
ce426f
-			idx1now = idx1cnt++;
ce426f
-		      }
ce426f
-		    else
ce426f
-		      /* We pushed backward sequences.  */
ce426f
-		      idx1now = backw1 = idx1cnt - 1;
ce426f
-		  }
ce426f
-	      }
ce426f
-	    while ((seq1len = weights[idx1arr[idx1now]++]) == 0);
ce426f
-
ce426f
-	  /* And the same for string `s2'.  */
ce426f
-	  if (seq2len == 0)
ce426f
-	    do
ce426f
-	      {
ce426f
-		++val2;
ce426f
-
ce426f
-		if (backw2_stop != ~0ul)
ce426f
-		  {
ce426f
-		    /* The is something pushed.  */
ce426f
-		    if (backw2 == backw2_stop)
ce426f
-		      {
ce426f
-			/* The last pushed character was handled.  Continue
ce426f
-			   with forward characters.  */
ce426f
-			if (idx2cnt < idx2max)
ce426f
-			  {
ce426f
-			    idx2now = idx2cnt;
ce426f
-			    backw2_stop = ~0ul;
ce426f
-			  }
ce426f
-			else
ce426f
-			  {
ce426f
-			    /* Nothing anymore.  The backward sequence
ce426f
-			       ended with the last sequence in the string.  */
ce426f
-			    idx2now = ~0ul;
ce426f
-			    break;
ce426f
-			  }
ce426f
-		      }
ce426f
-		    else
ce426f
-		      idx2now = --backw2;
ce426f
-		  }
ce426f
-		else
ce426f
-		  {
ce426f
-		    backw2_stop = idx2cnt;
ce426f
-
ce426f
-		    while (idx2cnt < idx2max)
ce426f
-		      {
ce426f
-			if ((rulesets[rule2arr[idx2cnt] * nrules + pass]
ce426f
-			     & sort_backward) == 0)
ce426f
-			  /* No more backward characters to push.  */
ce426f
-			  break;
ce426f
-			++idx2cnt;
ce426f
-		      }
ce426f
-
ce426f
-		    if (backw2_stop == idx2cnt)
ce426f
-		      {
ce426f
-			/* No sequence at all or just one.  */
ce426f
-			if (idx2cnt == idx2max)
ce426f
-			  /* Note that seq2len is still zero.  */
ce426f
-			  break;
ce426f
-
ce426f
-			backw2_stop = ~0ul;
ce426f
-			idx2now = idx2cnt++;
ce426f
-		      }
ce426f
-		    else
ce426f
-		      /* We pushed backward sequences.  */
ce426f
-		      idx2now = backw2 = idx2cnt - 1;
ce426f
-		  }
ce426f
-	      }
ce426f
-	    while ((seq2len = weights[idx2arr[idx2now]++]) == 0);
ce426f
+	  if (pass == 0)
ce426f
+	    {
ce426f
+	      get_next_seq (&seq1, nrules, rulesets, weights, table, extra,
ce426f
+			    indirect);
ce426f
+	      get_next_seq (&seq2, nrules, rulesets, weights, table, extra,
ce426f
+			    indirect);
ce426f
+	    }
ce426f
+	  else
ce426f
+	    {
ce426f
+	      get_next_seq_cached (&seq1, nrules, pass, rulesets, weights);
ce426f
+	      get_next_seq_cached (&seq2, nrules, pass, rulesets, weights);
ce426f
+	    }
ce426f
 
ce426f
 	  /* See whether any or both strings are empty.  */
ce426f
-	  if (seq1len == 0 || seq2len == 0)
ce426f
+	  if (seq1.len == 0 || seq2.len == 0)
ce426f
 	    {
ce426f
-	      if (seq1len == seq2len)
ce426f
+	      if (seq1.len == seq2.len)
ce426f
 		/* Both ended.  So far so good, both strings are equal
ce426f
 		   at this level.  */
ce426f
 		break;
ce426f
 
ce426f
 	      /* This means one string is shorter than the other.  Find out
ce426f
 		 which one and return an appropriate value.  */
ce426f
-	      result = seq1len == 0 ? -1 : 1;
ce426f
+	      result = seq1.len == 0 ? -1 : 1;
ce426f
 	      goto free_and_return;
ce426f
 	    }
ce426f
 
ce426f
-	  /* Test for position if necessary.  */
ce426f
-	  if (position && val1 != val2)
ce426f
-	    {
ce426f
-	      result = val1 - val2;
ce426f
-	      goto free_and_return;
ce426f
-	    }
ce426f
-
ce426f
-	  /* Compare the two sequences.  */
ce426f
-	  do
ce426f
-	    {
ce426f
-	      if (weights[idx1arr[idx1now]] != weights[idx2arr[idx2now]])
ce426f
-		{
ce426f
-		  /* The sequences differ.  */
ce426f
-		  result = (weights[idx1arr[idx1now]]
ce426f
-			    - weights[idx2arr[idx2now]]);
ce426f
-		  goto free_and_return;
ce426f
-		}
ce426f
-
ce426f
-	      /* Increment the offsets.  */
ce426f
-	      ++idx1arr[idx1now];
ce426f
-	      ++idx2arr[idx2now];
ce426f
-
ce426f
-	      --seq1len;
ce426f
-	      --seq2len;
ce426f
-	    }
ce426f
-	  while (seq1len > 0 && seq2len > 0);
ce426f
-
ce426f
-	  if (position && seq1len != seq2len)
ce426f
-	    {
ce426f
-	      result = seq1len - seq2len;
ce426f
-	      goto free_and_return;
ce426f
-	    }
ce426f
+	  result = do_compare (&seq1, &seq2, position, weights);
ce426f
+	  if (result != 0)
ce426f
+	    goto free_and_return;
ce426f
 	}
ce426f
     }
ce426f
 
ce426f
   /* Free the memory if needed.  */
ce426f
  free_and_return:
ce426f
   if (use_malloc)
ce426f
-    free (idx1arr);
ce426f
+    free (seq1.idxarr);
ce426f
 
ce426f
   return result;
ce426f
 }