Tried on a different computer, so I start slightly faster:
raw: 8.6s
global count: 1.3s
flat escapes: 8.3s
hybrid: 1.3s

This is the result I expected, as your technique flattens the strings,
but doesn't prevent $Token array bloat: we are still increasing $Token
size by 1 each call to BOLTescape. If you add a stopwatch call to the
markup parser, you will see that it is not the unescape calls that
cause the slowdown. A better method would be to remove the unneeded
escapes entirely in some way, as this could potentially free up some
memory. However, I don't believe memory imprint is an issue so it
isn't worth following up.

I wasn't too scientific, I refreshed a few times and got roughly the
same result each time. I should, of course, have noted the times and
given averages. It didnt seem necessary since there was very little
variation. The hybrid function, below, is the winner, being the
easiest to understand and using both speed-increasing techniques.

  function BOLTescape($x, $reverse=true, $spaced=false) {
## USED IN VARIOUS PLACES TO PRESERVE OUTPUT AND BLOCK ANY FURTHER
MARKUP PROCESSING. SET REVERSE TO FALSE TO UNESCAPE A STRING OF TEXT.
    if ($x == '') return;
    global $Token, $cToken;
    $x = preg_replace('/~~([0-9]+)\*?~~/e', '$Token[$1]', $x);
    if ($reverse == false) return $x;
    $Token[++$cToken] = $x;
    if ($spaced) return "~~$cToken*~~";
    return "~~$cToken~~";
  }


I don't understand why count($Token) is slow, but it's difficult to
read my results any other way. If I had to guess, I would think that
being inside a preg_replace call is giving problems counting an array
defined in the global scope, but that sounds about as crazy as count
being slow at all.
Regardless, ++$cToken is way faster than count($Token), at least on
pages with a few thousand escapes. (mine had over 4000).

Oh, and a static variable defined in a function is defined once, and
then accessible each time you call the function. It is similar to
global, but only within the scope of the function. I expected it to be
faster than global, but was greatly mistaken.

Anyway, does anyone else have any slow pages caused by the escape
function, that they could test this on? I believe any form with a few
hundred inputs will act similarly, but I'm not sure. I also don't know
how many people use boltwire for things creating a few hundred form
inputs :p

On Sep 23, 6:46 pm, The Editor <[email protected]> wrote:
> I noticed a similar problem sometime ago and worked out a different
> solution. Let me know how this compares with yours...
>
> It dramatically speeds the processing time because it stops all nested
> escapes--by first unescaping all previously escaped text in a string and
> then escaping the entire string. It also simplifies the code.
>
> I'm not familiar with the static declaration or fully understand it's
> benefits.
>
> function BOLTescape($x, $reverse=true, $spaced=false) {
> ## USED IN VARIOUS PLACES TO PRESERVE OUTPUT AND BLOCK ANY FURTHER MARKUP
> PROCESSING. SET REVERSE TO FALSE TO UNESCAPE A STRING OF TEXT.
> if ($x == '') return;
> global $Token;
> $x = preg_replace('/~~([0-9]+)\*?~~/e', '$Token[$1]', $x);
> if ($reverse == false) return $x;
> $c = count($Token) + 1;
> $Token[$c] = $x;
> if ($spaced) return "~~$c*~~";
> return "~~$c~~";
>
> }
>
> Cheers,
> Dan
>
> On Tue, Sep 20, 2011 at 8:14 PM, DrunkenMonk <[email protected]> wrote:
> > I was using 3.4.15, I only changed declarations:
> >   static $Token = array();
> >  static $c = 0;
> > and the usage of $c:
> >  $Token[++$c] = $x;
>
> > On my live server I patched in the following version (using global
> > instead of static variables for a tiny speed difference):
>
> > function BOLTescape($x, $reverse=true, $spaced=false) {
> > ## USED IN VARIOUS PLACES TO PRESERVE SOME OUTPUT AND BLOCK ANY
> > FURTHER MARKUP PROCESSING. SET REVERSE TO FALSE TO UNESCAPE A STRING
> > OF TEXT.
> >   global $Token, $TokenCount;
> >  //static $Token = array();
> >  //static $c = 0;
> >   if ($x == '') return;
> > //  if ($x == "\n") return '<>';
> >  if($reverse == false) {
> >    while (preg_match('/~~([0-9]+)\*?~~/', $x) != 0) {
> >      $x = preg_replace('/~~([0-9]+)\*?~~/e', '$Token[$1]', $x);
> >    }
> >    return $x;
> >    }
> >   $Token[++$TokenCount] = $x;
> >  if ($spaced) return "~~$TokenCount*~~";
> >  return "~~$TokenCount~~";
> >   }
>
> > On Sep 21, 1:14 am, Kevin <[email protected]> wrote:
> > > your new function has
>
> > > function BOLTescape($x, $reverse=true, $spaced=false) {
>
> > > with $spaced=false at the end.
>
> > > My existing one has:
>
> > > function xBOLTescape($x, $reverse=true) {
>
> > > Did you make other changes that inserted that.
>
> > > I am using 3.4.14
>
> > > On Tue, Sep 20, 2011 at 2:49 PM, DrunkenMonk <[email protected]> wrote:
> > > > I have certain pages on my site that take a long time to load, in the
> > > > order of 10 seconds.
> > > > Changing BOLTescape to the following code reduced that by roughly a
> > > > third:
>
> > > > function BOLTescape($x, $reverse=true, $spaced=false) {
> > > > ## USED IN VARIOUS PLACES TO PRESERVE SOME OUTPUT AND BLOCK ANY
> > > > FURTHER MARKUP PROCESSING. SET REVERSE TO FALSE TO UNESCAPE A STRING
> > > > OF TEXT.
> > > >  static $Token = array();
> > > >  static $c = 0;
> > > >  if ($x == '') return;
> > > > //  if ($x == "\n") return '<>';
> > > >  if($reverse == false) {
> > > >    while (preg_match('/~~([0-9]+)\*?~~/', $x) != 0) $x =
> > > > preg_replace('/~~([0-9]+)\*?~~/e', '$Token[$1]', $x);
> > > >    return $x;
> > > >    }
> > > >  $Token[++$c] = $x;
> > > >  if ($spaced) return "~~$c*~~";
> > > >  return "~~$c~~";
> > > >  }
>
> > > > $Token is defined as a global variable in other places but never used
> > > > as far as I can see.
> > > > It is, however, the count($Token) line that causes the majority of the
> > > > slowdown, so $Token can remain global if this is preferred.
>
> > > > Background:
> > > > On a page that took 7 seconds to load, I added a BOLTstopwatch line to
> > > > the BOLTdoMarkupTable function, and found that over 3 seconds was
> > > > being spent on the pre - escape markup rule. Using my suggestion, this
> > > > was reduced to 0.12 seconds.
>
> > > > I'm going to go find the remaining 2 seconds of loading time and
> > > > optimize them away too.
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "BoltWire" group.
> > > > To post to this group, send email to [email protected].
> > > > To unsubscribe from this group, send email to
> > > > [email protected].
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/boltwire?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "BoltWire" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> >http://groups.google.com/group/boltwire?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"BoltWire" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/boltwire?hl=en.

Reply via email to