| Andrew Cooke | Contents | Latest | RSS | Twitter | Previous | Next

C[omp]ute

Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

Personal Projects

Lepl parser for Python.

Colorless Green.

Photography around Santiago.

SVG experiment.

Professional Portfolio

Calibration of seismometers.

Data access via web services.

Cache rewrite.

Extending OpenSSH.

Last 100 entries

SQL Performance Explained; The Little Manual of API Design; Multiple Word Sizes; CRC - Next Steps; FizzBuzz; Update on CRCs; Decent Links / Discussion Community; Automated Reasoning About LLVM Optimizations and Undefined Behavior; A Painless Guide To CRC Error Detection Algorithms; Tests in Julia; Dave Eggers: what's so funny about peace, love and Starship?; Cello - High Level C Programming; autoreconf needs tar; Will Self Goes To Heathrow; Top 5 BioInformatics Papers; Vasovagal Response; Good Food in Vina; Chilean Drug Criminals Use Subsitution Cipher; Adrenaline; Stiglitz on the Impact of Technology; Why Not; How I Am 5; Lenovo X240 OpenSuse 13.1; NSA and GCHQ - Psychological Trolls; Finite Fields in Julia (Defining Your Own Number Type); Julian Assange; Starting Qemu on OpenSuse; Noisy GAs/TMs; Venezuela; Reinstalling GRUB with EFI; Instructions For Disabling KDE Indexing; Evolving Speakers; Changing Salt Size in Simple Crypt 3.0.0; Logarithmic Map (Moved); More Info; Words Found in Voynich Manuscript; An Inventory Of 3D Space-Filling Curves; Foxes Using Magnetic Fields To Hunt; 5 Rounds RC5 No Rotation; JP Morgan and Madoff; Ori - Secure, Distributed File System; Physical Unclonable Functions (PUFs); Prejudice on Reddit; Recursion OK; Optimizing Julia Code; Cash Handouts in Brazil; Couple Nice Music Videos; It Also Works!; Adaptive Plaintext; It Works!; RC5 Without Rotation (2); 8 Years...; Attack Against Encrypted Linux Disks; Pushing Back On NSA At IETF; Summary of Experimental Ethics; Very Good Talk On Security, Snowden; Locusts are Grasshoppers!; Vagrant (OpenSuse and IDEs); Interesting Take On Mandela's Context; Haskell Cabal O(n^2) / O(n) Fix; How I Am 4; Chilean Charity Supporting Women; Doing SSH right; Festival of Urban Intervention; Neat Idea - Wormholes Provide Entanglement; And a Link....; Simple Encryption for Python 2.7; OpenSuse 13.1 Is Better!; Little Gain...; More Details on Technofull Damage; Palmrest Cracked Too....; Tecnofull (Lenovo Support) Is Fucking Useless; The Neuroscientist Who Discovered He Was a Psychopath; Interpolating Polynomials; Bottlehead Crack as Pre-amp; Ooops K702!; Bottlehead Crack, AKG K701; Breaking RC5 Without Rotation; Great post thank you; Big Balls of Mud; Phabricator - Tools for working together; Amazing Julia RC5 Code Parameterized By Word Size; Chi-Square Can Be Two-Sided; Why Do Brits Accept Surveillance?; Statistics Done Wrong; Mesas Trape from Bravo; European Report on Crypto Primitives and Protocols; Interesting Omissions; Oryx And Crake (Margaret Atwood); Music and Theory; My Arduino Programs; Elliptic Curve Crypto; Re: Licensing Interpreted Code; Licensing Interpreted Code; ASUS 1015E-DS03 OpenSuse 12.3 SSD; translating lettuce feature files into stub steps files; Re: translating lettuce feature files into stub steps files; A Tale of Two Psychiatrists; The Real Reason the Poor Go Without Bank Accounts; The Rational Choices of Crack Addicts; Forgot grouped

© 2006-2013 Andrew Cooke (site) / post authors (content).

RXPY Benchmarks

From: andrew cooke <andrew@...>

Date: Sat, 17 Jul 2010 16:48:30 -0400

Much of this is just duplicating the re2 work, and the rest is RXPY-specific,
but even so, these are quite interesting (for me, at least).

Note that the times are relative to the Python re package (log10 in brackets).
Also, the plots are both linear ### and logarithmic [ ].


First, some basic matching.  This is just showing how slow my code is
(hundreds of times slower than the re package).

Match a(.)c against abc
             Backtracking [##########                   ]              95 [2.0]
            Parallel wide [#######################            ]       206 [2.3]
    Parallel wide, hashed [#################################### ]     315 [2.5]
             Parallel seq [#######################            ]       205 [2.3]
     Parallel seq, hashed [###################################  ]     304 [2.5]
            Parallel beam [############################        ]      245 [2.4]
    Parallel beam, hashed [######################################]    339 [2.5]

Match (a)b(?<=(?(1)b|x))(c) against abc
             Backtracking [############                   ]           210 [2.3]
            Parallel wide [#########################          ]       398 [2.6]
    Parallel wide, hashed [#################################    ]     531 [2.7]
             Parallel seq [#########################          ]       400 [2.6]
     Parallel seq, hashed [##################################   ]     546 [2.7]
            Parallel beam [##############################      ]      480 [2.7]
    Parallel beam, hashed [######################################]    622 [2.8]

Match a*b against a^100b
             Backtracking [####################################  ]   1902 [3.3]
            Parallel wide [#################################    ]    1737 [3.2]
    Parallel wide, hashed [####################################  ]   1919 [3.3]
             Parallel seq [################################     ]    1733 [3.2]
     Parallel seq, hashed [####################################  ]   1919 [3.3]
            Parallel beam [###################################  ]    1884 [3.3]
    Parallel beam, hashed [######################################]   2073 [3.3]

Match .*b against a^100b
             Backtracking [#################                 ]       1902 [3.3]
            Parallel wide [###############                   ]       1769 [3.2]
    Parallel wide, hashed [#################                 ]       1927 [3.3]
             Parallel seq [###############                   ]       1757 [3.2]
     Parallel seq, hashed [##################                ]       2015 [3.3]
            Parallel beam [###################################  ]    3825 [3.6]
    Parallel beam, hashed [######################################]   4300 [3.6]

Above is quite interesting because the beam search is suddenly twice as slow,
just by changing from "a*b" to ".*b".  That's because the second expression
has a failure ("." matchs the final "b") and so needs to backtrack.  Because
the initial beam width is 1 that means that the entire search must be repeated
with doubled with.


The next test is a search.

Search .*b against a^100b
             Backtracking [                         ]                1172 [3.1]
            Parallel wide [######################################]  41841 [4.6]
    Parallel wide, hashed [                         ]                1459 [3.2]
             Parallel seq [                         ]                1212 [3.1]
     Parallel seq, hashed [                         ]                1338 [3.1]
            Parallel beam [###                          ]            3764 [3.6]
    Parallel beam, hashed [###                          ]            3688 [3.6]

Above shows the problem with a naive wide parallel approach - there are an
awful lot of different states to store.  Of course, most are duplicates so the
hashing removes the problem (as does sequential or beam search).


The next tests show the exponential explosion problem in Python's matcher.
This is a little more difficult to trigger than in the re2 paper because
Python has a "shortcut" (hack!) that avoids the problem in very simple cases
(but, as we will see below, does nothing to address the underlying issues).

Match (?:a|b)?^2n(?:ab)^n against (ab)^n for n=4
             Backtracking [#############                   ]          573 [2.8]
            Parallel wide [#########                     ]            392 [2.6]
    Parallel wide, hashed [##                      ]                  125 [2.1]
             Parallel seq [#########                     ]            398 [2.6]
     Parallel seq, hashed [##                      ]                  125 [2.1]
            Parallel beam [######################################]   1606 [3.2]
    Parallel beam, hashed [########                      ]            360 [2.6]

Match (?:a|b)?^2n(?:ab)^n against (ab)^n for n=6
             Backtracking [######################################]    637 [2.8]
            Parallel wide [#############################       ]      476 [2.7]
    Parallel wide, hashed [#                 ]                         27 [1.4]
             Parallel seq [#############################       ]      475 [2.7]
     Parallel seq, hashed [#                 ]                         27 [1.4]
    Parallel beam, hashed [##                    ]                     51 [1.7]

Match (?:a|b)?^2n(?:ab)^n against (ab)^n for n=8
    Parallel wide, hashed [##############       ]                       4 [0.6]
     Parallel seq, hashed [##############       ]                       4 [0.6]
    Parallel beam, hashed [######################################]     10 [1.0]

As the number of terms increase, only the hashed approaches are efficient (due
to the explosion in the number of states).  This is as expected.  What is nice
to see is that by n=8 my Python code is oly 4x slower than the re package!
This is because the re backtracker is "exploding", while my code is linear
(but slow).


This is only possible, though, when we can discard duplicate state.  Which
isn't so easy when matching groups:

Match (a|b)?^2n(?:ab)^n against (ab)^n for n=4
             Backtracking [#######                       ]            481 [2.7]
            Parallel wide [#####                       ]              378 [2.6]
    Parallel wide, hashed [#########                      ]           627 [2.8]
             Parallel seq [######                       ]             414 [2.6]
     Parallel seq, hashed [##########                     ]           667 [2.8]
            Parallel beam [##########################          ]     1612 [3.2]
    Parallel beam, hashed [######################################]   2415 [3.4]

Match (a|b)?^2n(?:ab)^n against (ab)^n for n=6
             Backtracking [###########################         ]      512 [2.7]
            Parallel wide [######################             ]       415 [2.6]
    Parallel wide, hashed [######################################]    723 [2.9]
             Parallel seq [######################             ]       418 [2.6]
     Parallel seq, hashed [######################################]    714 [2.9]


And here we do no better than normal backtracking.


The main conclusions, then, are:

1 - Python's re package does suffer from the exponential "explosion" issue,
    while my code (with duplicate state elimination) does not.

2 - My code can also handle groups, but the degrades in performance as
    expected.

So RXPY is both general and, when possible, efficient.  Even if it is terribly
slow.

A secondary conclusion is that the beam search approach does not seem to be
worth the trouble.

Andrew

Future Work

From: andrew cooke <andrew@...>

Date: Sun, 18 Jul 2010 08:53:28 -0400

The following optimisations are needed at some point:

- Branches should test whether there is sufficient remaining text.  This can
  be precomputed by nodes.

- Search should identify (at least) initial sequences and optimise on those.
  See the existing re library code.  RXPY could go better and use non-startign
  sequences with a known lookback to start.

But for now I think I am going to produce a simplified version of the hashed,
wide engine and see how that can work on PyPy.

Andrew

More Benchmarks

From: andrew cooke <andrew@...>

Date: Sun, 25 Jul 2010 11:42:01 -0400

There are some new engines here.  "Simple" is a stripped down engine that does
not support loops or groups.  "Complex" is similar to "Wide hashed", but has
much more care taken in the implementation to reduce overheads.  Finally,
"Quick" uses "Simple" until failure, then changes to "Complex" - this is the
"final" engine.

Also, the "primality" tests are based on '^1?$|^(11+?)\1+$', which repeats
an increasingly large group until match.

Note that for "simple" tests, the overhead of "Quick" is significant.


Match . against a
             Backtracking [#############                 ]             49 [1.7]
            Parallel wide [#######################           ]         82 [1.9]
    Parallel wide, hashed [############################       ]        97 [2.0]
             Parallel seq [########################          ]         84 [1.9]
     Parallel seq, hashed [##########################         ]        92 [2.0]
            Parallel beam [#################################    ]     114 [2.1]
    Parallel beam, hashed [###################################  ]     122 [2.1]
          Parallel simple [##################################   ]     117 [2.1]
         Parallel complex [######################################]    135 [2.1]
                    Quick [##################################   ]     120 [2.1]

Match a(.)c against abc
             Backtracking [####                     ]                  60 [1.8]
            Parallel wide [#########                    ]             124 [2.1]
    Parallel wide, hashed [###############                 ]          193 [2.3]
             Parallel seq [##########                   ]             126 [2.1]
     Parallel seq, hashed [################                ]          201 [2.3]
            Parallel beam [############                   ]           159 [2.2]
    Parallel beam, hashed [#################                ]         214 [2.3]
         Parallel complex [###################               ]        240 [2.4]
                    Quick [######################################]    473 [2.7]

Match a*b against a^100b
             Backtracking [######################################]   1605 [3.2]
            Parallel wide [################################     ]    1384 [3.1]
    Parallel wide, hashed [####################################  ]   1519 [3.2]
             Parallel seq [################################     ]    1385 [3.1]
     Parallel seq, hashed [####################################  ]   1517 [3.2]
            Parallel beam [###################################  ]    1513 [3.2]
    Parallel beam, hashed [######################################]   1662 [3.2]
          Parallel simple [##############                   ]         630 [2.8]
         Parallel complex [############################        ]     1202 [3.1]
                    Quick [##############                   ]         624 [2.8]

Match .*b against a^100b
             Backtracking [##################                ]       1461 [3.2]
            Parallel wide [###############                   ]       1273 [3.1]
    Parallel wide, hashed [##################                ]       1444 [3.2]
             Parallel seq [################                  ]       1295 [3.1]
     Parallel seq, hashed [#################                 ]       1401 [3.1]
            Parallel beam [####################################  ]   2896 [3.5]
    Parallel beam, hashed [######################################]   3108 [3.5]
          Parallel simple [######                        ]            581 [2.8]
         Parallel complex [#############                    ]        1100 [3.0]
                    Quick [#######                       ]            585 [2.8]

Search .*b against a^100b
             Backtracking [                        ]                  846 [2.9]
            Parallel wide [######################################]  29364 [4.5]
    Parallel wide, hashed [                         ]                1011 [3.0]
             Parallel seq [                        ]                  730 [2.9]
     Parallel seq, hashed [                        ]                  818 [2.9]
            Parallel beam [##                          ]             2462 [3.4]
    Parallel beam, hashed [##                          ]             2110 [3.3]
          Parallel simple [                     ]                     335 [2.5]
         Parallel complex [                       ]                   690 [2.8]
                    Quick [                     ]                     335 [2.5]

Match (.*) (.*) (.*)  against abc abc abc
             Backtracking [#################                 ]        601 [2.8]
            Parallel wide [#############                   ]          465 [2.7]
    Parallel wide, hashed [#################                 ]        592 [2.8]
             Parallel seq [#############                   ]          450 [2.7]
     Parallel seq, hashed [################                 ]         568 [2.8]
            Parallel beam [################################     ]    1087 [3.0]
    Parallel beam, hashed [######################################]   1328 [3.1]
         Parallel complex [#############                   ]          471 [2.7]
                    Quick [####################              ]        706 [2.8]


Search .*?a*b against a^10b
             Backtracking [#                      ]                   109 [2.0]
            Parallel wide [######################################]   1991 [3.3]
    Parallel wide, hashed [####                       ]               242 [2.4]
             Parallel seq [########                      ]            441 [2.6]
     Parallel seq, hashed [###                       ]                222 [2.3]
            Parallel beam [#                      ]                   118 [2.1]
    Parallel beam, hashed [##                      ]                  129 [2.1]
          Parallel simple [#                     ]                    101 [2.0]
         Parallel complex [###                       ]                200 [2.3]
                    Quick [#                     ]                    101 [2.0]

Search .*?a*b against a^100b
             Backtracking [                         ]                1026 [3.0]
    Parallel wide, hashed [##                          ]             2542 [3.4]
             Parallel seq [######################################]  33381 [4.5]
     Parallel seq, hashed [##                          ]             2264 [3.4]
            Parallel beam [                         ]                1055 [3.0]
    Parallel beam, hashed [                         ]                1162 [3.1]
          Parallel simple [                        ]                  781 [2.9]
         Parallel complex [#                          ]              1922 [3.3]
                    Quick [                        ]                  780 [2.9]

Search .*?a*b against a^1000b
             Backtracking [##############                    ]       8137 [3.9]
    Parallel wide, hashed [######################################]  21470 [4.3]
     Parallel seq, hashed [###################################   ]  19550 [4.3]
            Parallel beam [################                   ]      9044 [4.0]
    Parallel beam, hashed [#################                  ]      9792 [4.0]
          Parallel simple [###########                      ]        6512 [3.8]
         Parallel complex [#############################        ]   16108 [4.2]
                    Quick [###########                      ]        6679 [3.8]


Match (a|b)?^2n(?:ab)^n against (ab)^n for n=4
             Backtracking [#######                       ]            492 [2.7]
            Parallel wide [#####                       ]              358 [2.6]
    Parallel wide, hashed [##########                     ]           615 [2.8]
             Parallel seq [#####                       ]              354 [2.5]
     Parallel seq, hashed [#########                      ]           597 [2.8]
            Parallel beam [########################            ]     1485 [3.2]
    Parallel beam, hashed [######################################]   2343 [3.4]
         Parallel complex [###                        ]               261 [2.4]
                    Quick [####                        ]              310 [2.5]

Match (a|b)?^2n(?:ab)^n against (ab)^n for n=6
             Backtracking [############################        ]      514 [2.7]
            Parallel wide [#####################             ]        401 [2.6]
    Parallel wide, hashed [######################################]    712 [2.9]
             Parallel seq [#####################             ]        396 [2.6]
     Parallel seq, hashed [######################################]    718 [2.9]
         Parallel complex [#################                ]         331 [2.5]
                    Quick [##################               ]         338 [2.5]


Match (?:a|b)?^2n(?:ab)^n against (ab)^n for n=4
             Backtracking [##############                   ]         597 [2.8]
            Parallel wide [#########                     ]            394 [2.6]
    Parallel wide, hashed [##                      ]                  127 [2.1]
             Parallel seq [#########                     ]            386 [2.6]
     Parallel seq, hashed [##                       ]                 141 [2.2]
            Parallel beam [######################################]   1619 [3.2]
    Parallel beam, hashed [########                      ]            366 [2.6]
          Parallel simple [                    ]                       58 [1.8]
         Parallel complex [##                      ]                  120 [2.1]
                    Quick [                    ]                       55 [1.7]

Match (?:a|b)?^2n(?:ab)^n against (ab)^n for n=6
             Backtracking [######################################]    628 [2.8]
            Parallel wide [############################        ]      457 [2.7]
    Parallel wide, hashed [#                 ]                         26 [1.4]
             Parallel seq [############################        ]      453 [2.7]
     Parallel seq, hashed [#                 ]                         26 [1.4]
    Parallel beam, hashed [##                    ]                     50 [1.7]
          Parallel simple [            ]                               10 [1.0]
         Parallel complex [                 ]                          23 [1.4]
                    Quick [            ]                               10 [1.0]

Match (?:a|b)?^2n(?:ab)^n against (ab)^n for n=8
    Parallel wide, hashed [##############       ]                       4 [0.6]
     Parallel seq, hashed [##############       ]                       4 [0.6]
    Parallel beam, hashed [######################################]     10 [1.0]
          Parallel simple [##]#                                         1 [0.1]
         Parallel complex [############      ]                          3 [0.5]
                    Quick [#]##                                         1 [0.1]

Match a?^na^n against a^n for n=8
    Parallel wide, hashed [##############                 ]           136 [2.1]
     Parallel seq, hashed [##############                 ]           132 [2.1]
    Parallel beam, hashed [######################################]    361 [2.6]
          Parallel simple [####                    ]                   49 [1.7]
         Parallel complex [###########                   ]            105 [2.0]
                    Quick [#####                    ]                  51 [1.7]


Primality 32
             Backtracking [#                      ]                   146 [2.2]
            Parallel wide [###########################         ]     2157 [3.3]
    Parallel wide, hashed [######################################]   3115 [3.5]
             Parallel seq [###########################         ]     2143 [3.3]
     Parallel seq, hashed [##################################### ]   2994 [3.5]
            Parallel beam [######                       ]             516 [2.7]
    Parallel beam, hashed [########                      ]            672 [2.8]
         Parallel complex [########                      ]            683 [2.8]
                    Quick [#########                      ]           754 [2.9]

Primality 37
             Backtracking [##                        ]                444 [2.6]
            Parallel wide [#######                        ]          1305 [3.1]
    Parallel wide, hashed [##########                      ]         1852 [3.3]
             Parallel seq [#######                        ]          1302 [3.1]
     Parallel seq, hashed [##########                      ]         1811 [3.3]
            Parallel beam [#############################        ]    4956 [3.7]
    Parallel beam, hashed [######################################]   6709 [3.8]
         Parallel complex [#                        ]                 378 [2.6]
                    Quick [#                        ]                 409 [2.6]

Primality 128
             Backtracking [                     ]                     401 [2.6]
            Parallel wide [#############################        ]   21910 [4.3]
    Parallel wide, hashed [######################################]  28511 [4.5]
             Parallel seq [############################         ]   21262 [4.3]
     Parallel seq, hashed [######################################]  29158 [4.5]
         Parallel complex [####                          ]           3876 [3.6]
                    Quick [####                          ]           3910 [3.6]

Primality 131
             Backtracking [####                        ]              563 [2.8]
            Parallel wide [#############################        ]    3298 [3.5]
    Parallel wide, hashed [######################################]   4397 [3.6]
             Parallel seq [############################        ]     3220 [3.5]
     Parallel seq, hashed [######################################]   4466 [3.6]
         Parallel complex [####                        ]              585 [2.8]
                    Quick [####                        ]              586 [2.8]

Primality (eager) 32
             Backtracking [                       ]                   313 [2.5]
            Parallel wide [########                        ]         2224 [3.3]
    Parallel wide, hashed [############                     ]        3068 [3.5]
             Parallel seq [########                        ]         2198 [3.3]
     Parallel seq, hashed [###########                      ]        2965 [3.5]
            Parallel beam [##############################       ]    7542 [3.9]
    Parallel beam, hashed [######################################]   9658 [4.0]
         Parallel complex [##                        ]                677 [2.8]
                    Quick [##                         ]               749 [2.9]

Primality (eager) 37
             Backtracking [##                        ]                534 [2.7]
            Parallel wide [######                        ]           1387 [3.1]
    Parallel wide, hashed [#########                       ]         1882 [3.3]
             Parallel seq [######                        ]           1370 [3.1]
     Parallel seq, hashed [#########                       ]         1854 [3.3]
            Parallel beam [##############################       ]    5855 [3.8]
    Parallel beam, hashed [######################################]   7586 [3.9]
         Parallel complex [#                        ]                 396 [2.6]
                    Quick [#                        ]                 436 [2.6]

Primality (eager) 128
             Backtracking [                       ]                   351 [2.5]
            Parallel wide [#############################        ]    8833 [3.9]
    Parallel wide, hashed [######################################]  11640 [4.1]
             Parallel seq [#############################        ]    8685 [3.9]
     Parallel seq, hashed [######################################]  11709 [4.1]
         Parallel complex [####                         ]            1590 [3.2]
                    Quick [####                          ]           1600 [3.2]

Primality (eager) 131
             Backtracking [####                         ]             631 [2.8]
            Parallel wide [###########################         ]     3310 [3.5]
    Parallel wide, hashed [##################################### ]   4419 [3.6]
             Parallel seq [###########################         ]     3244 [3.5]
     Parallel seq, hashed [######################################]   4692 [3.7]
         Parallel complex [####                        ]              586 [2.8]
                    Quick [####                        ]              596 [2.8]

Comment on this post