[pmwiki-devel] MarkupToHTML optimisation

Stefan Schimanski sts at 1stein.org
Sat Nov 4 03:28:11 CST 2006


Hi!

I am experimenting with some changes to speedup page rendering in
PmWiki. One of those is the following, which groups Markup rules without
the "e" flag into rule blocks. Per block there is only one preg_replace
call. The result is up to 25% speedup, with a quite small change.

Stefan

--- ../http/pmwiki.php	2006-11-02 17:17:47.000000000 +0100
+++ pmwiki.php	2006-11-04 10:23:23.000000000 +0100
@@ -1244,14 +1244,31 @@
   global $MarkupTable,$MarkupRules,$LinkPattern;
   if (!$MarkupRules) {
     uasort($MarkupTable,'mpcmp');
+    $i = 0;
     foreach($MarkupTable as $id=>$m) 
-      if (@$m['pat']) 
-        $MarkupRules[str_replace('\\L',$LinkPattern,$m['pat'])]=$m['rep'];
+     if (@$m['pat']) {
+        $p = str_replace('\\L',$LinkPattern,$m['pat']);
+        if( $p{0}=='/' ) {
+          $MarkupRules[$i][0]="rexp";
+          $MarkupRules[$i][1][] = $p;
+	  $MarkupRules[$i][2][] = $m['rep'];
+          if( preg_match( "/^\\/.*\\/[^\/]*e[^\/]*$/s", $p ) && preg_match('/[()]/', $m['rep']) ) $i++;
+      } else {
+          if( @count($MarkupRules[$i])>0 ) $i++;
+          $MarkupRules[$i][0]="str";
+          $MarkupRules[$i][1] = $p;
+          $MarkupRules[$i][2] = $m['rep'];
+          $i++;
+        }
+     }
+
   }
+
   return $MarkupRules;
 }
 
 
 function MarkupToHTML($pagename, $text, $opt = NULL) {
   # convert wiki markup text to HTML output
   global $MarkupRules, $MarkupFrame, $MarkupFrameBase, $WikiWordCount,
@@ -1269,12 +1286,16 @@
   while (count($lines)>0) {
     $x = array_shift($lines);
     $RedoMarkupLine=0;
-    foreach($markrules as $p=>$r) {
-      if ($p{0} == '/') $x=preg_replace($p,$r,$x); 
-      elseif (strstr($x,$p)!==false) $x=eval($r);
+    foreach($markrules as $ruleblock) {
+      if( $ruleblock[0]=="rexp" )
+        $x=preg_replace($ruleblock[1],$ruleblock[2],$x); 
+        elseif (strstr($x,$ruleblock[1])!==false) $x=eval($ruleblock[2]);
       if (isset($php_errormsg)) { echo "pat=$p"; unset($php_errormsg); }
       if ($RedoMarkupLine) { $lines=array_merge((array)$x,$lines); continue 2; }
     }
     if ($x>'') $out .= "$x\n";
   }
   foreach((array)(@$MarkupFrame[0]['posteval']) as $v) eval($v);



More information about the pmwiki-devel mailing list