Andrew Cooke | Contents | Latest | RSS | 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

Choochoo Training Diary

Last 100 entries

Surprise Paradox; [Books] Good Author List; [Computing] Efficient queries with grouping in Postgres; [Computing] Automatic Wake (Linux); [Computing] AWS CDK Aspects in Go; [Bike] Adidas Gravel Shoes; [Computing, Horror] Biological Chips; [Books] Weird Lit Recs; [Covid] Extended SIR Models; [Art] York-based Printmaker; [Physics] Quantum Transitions are not Instantaneous; [Computing] AI and Drum Machines; [Computing] Probabilities, Stopping Times, Martingales; bpftrace Intro Article; [Computing] Starlab Systems - Linux Laptops; [Computing] Extended Berkeley Packet Filter; [Green] Mainspring Linear Generator; Better Approach; Rummikub Solver; Chilean Poetry; Felicitations - Empowerment Grant; [Bike] Fixing Spyre Brakes (That Need Constant Adjustment); [Computing, Music] Raspberry Pi Media (Audio) Streamer; [Computing] Amazing Hack To Embed DSL In Python; [Bike] Ruta Del Condor (El Alfalfal); [Bike] Estimating Power On Climbs; [Computing] Applying Azure B2C Authentication To Function Apps; [Bike] Gearing On The Back Of An Envelope; [Computing] Okular and Postscript in OpenSuse; There's a fix!; [Computing] Fail2Ban on OpenSuse Leap 15.3 (NFTables); [Cycling, Computing] Power Calculation and Brakes; [Hardware, Computing] Amazing Pockit Computer; Bullying; How I Am - 3 Years Post Accident, 8+ Years With MS; [USA Politics] In America's Uncivil War Republicans Are The Aggressors; [Programming] Selenium and Python; Better Walking Data; [Bike] How Fast Before Walking More Efficient Than Cycling?; [COVID] Coronavirus And Cycling; [Programming] Docker on OpenSuse; Cadence v Speed; [Bike] Gearing For Real Cyclists; [Programming] React plotting - visx; [Programming] React Leaflet; AliExpress Independent Sellers; Applebaum - Twilight of Democracy; [Politics] Back + US Elections; [Programming,Exercise] Simple Timer Script; [News] 2019: The year revolt went global; [Politics] The world's most-surveilled cities; [Bike] Hope Freehub; [Restaurant] Mama Chau's (Chinese, Providencia); [Politics] Brexit Podcast; [Diary] Pneumonia; [Politics] Britain's Reichstag Fire moment; install cairo; [Programming] GCC Sanitizer Flags; [GPU, Programming] Per-Thread Program Counters; My Bike Accident - Looking Back One Year; [Python] Geographic heights are incredibly easy!; [Cooking] Cookie Recipe; Efficient, Simple, Directed Maximisation of Noisy Function; And for argparse; Bash Completion in Python; [Computing] Configuring Github Jekyll Locally; [Maths, Link] The Napkin Project; You can Masquerade in Firewalld; [Bike] Servicing Budget (Spring) Forks; [Crypto] CIA Internet Comms Failure; [Python] Cute Rate Limiting API; [Causality] Judea Pearl Lecture; [Security, Computing] Chinese Hardware Hack Of Supermicro Boards; SQLAlchemy Joined Table Inheritance and Delete Cascade; [Translation] The Club; [Computing] Super Potato Bruh; [Computing] Extending Jupyter; Further HRM Details; [Computing, Bike] Activities in ch2; [Books, Link] Modern Japanese Lit; What ended up there; [Link, Book] Logic Book; Update - Garmin Express / Connect; Garmin Forerunner 35 v 230; [Link, Politics, Internet] Government Trolls; [Link, Politics] Why identity politics benefits the right more than the left; SSH Forwarding; A Specification For Repeating Events; A Fight for the Soul of Science; [Science, Book, Link] Lost In Math; OpenSuse Leap 15 Network Fixes; Update; [Book] Galileo's Middle Finger; [Bike] Chinese Carbon Rims; [Bike] Servicing Shimano XT Front Hub HB-M8010; [Bike] Aliexpress Cycling Tops; [Computing] Change to ssh handling of multiple identities?; [Bike] Endura Hummvee Lite II; [Computing] Marble Based Logic; [Link, Politics] Sanity Check For Nuclear Launch; [Link, Science] Entropy and Life

© 2006-2017 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