Branch Prediction Analysis of Morris-Pratt and Knuth-Morris-Pratt Algorithms
Cyril Nicaud, Carine Pivoteau, Stéphane Vialette
TL;DR
The paper analyzes Morris–Pratt and Knuth–Morris–Pratt pattern matching under a simple local branch-prediction model with 2‑bit saturating counters. It employs automata and transducers to encode letter comparisons and failure functions, and uses Markov chains to characterize both the average number of letter comparisons and the asymptotic misprediction counts. The authors derive explicit expressions for mispredictions due to the counter update, per-letter comparisons, and the $i\ge 0$ test, including closed forms for small patterns and general formulations through stationary distributions. They provide numerical illustrations for small patterns and alphabets, reveal that certain branches (notably $i\ge 0$) can be highly mispredicted at larger alphabets, and discuss extensions to hybrid predictors and Markov-text models. Overall, the work offers a foundational theoretical framework for branch-prediction-aware analysis of classic text-search algorithms and suggests directions for more realistic architectures and probabilistic sources.
Abstract
We analyze the classical Morris-Pratt and Knuth-Morris-Pratt pattern matching algorithms through the lens of computer architecture, investigating the impact of incorporating a simple branch prediction mechanism into the model of computation. Assuming a fixed pattern and a random text, we derive precise estimates of the number of mispredictions these algorithms produce using local predictors. Our approach is based on automata theory and Markov chains, providing a foundation for the theoretical analysis of other text algorithms and more advanced branch prediction strategies.
