RC RANDOM CHAOS

How RNA-Folding Algorithms Speed Up Haskell's ApplicativeDo Compilation

· via Hacker News

Original source

Stealing from Biologists to Compile Haskell Faster

Hacker News →

GHC ships an -foptimal-applicative-do flag that’s disabled by default because the underlying algorithm runs in O(n³), making a 1,000-statement block take nearly a minute to compile versus under two seconds for the greedy heuristic. The optimization matters because ApplicativeDo lets developers write plain do-notation while the compiler figures out which statements are independent and can be batched under <*>, which libraries like Haxl exploit to collapse multiple database or network round-trips into one. The greedy algorithm leaves rounds on the table by making locally-myopic cuts, while the optimal version finds schedules that align independent subtrees in parallel.

The author traces why the obvious shortcuts fail. The cost is not simply the longest dependency chain, because the no-reordering constraint means a single long-spanning arrow can force an extra round invisibly. A one-dimensional scan that tracks how far a window can extend within a round budget breaks too: an arrow whose head lies outside the current window misleads the scan into treating the blob as larger than it is, hiding parallel seams. The structure inside any span genuinely depends on both endpoints, so a real two-dimensional minimum search is unavoidable in the tie-breaking case the original paper left open.

The surprising hook is that the recurrence — minimizing cost over spans of a sequence with nested independent groupings — has the same shape as the dynamic program biologists use to predict RNA secondary structure. The post sets up that connection as the path toward beating O(n³) on the cases where the extreme-cut shortcut from the original paper ties and forces a deeper search.

Read the full article

Continue reading at Hacker News →

This is an AI-generated summary. Read the original for the full story.