Skip to the stories
AI & ML

TreeWalker Speeds Up Tree Ensembles by Sharing Work Across Related Rows

  • Christopher Miller
  • 730 reads

Shopify's open-source Rust engine, TreeWalker, shares tree decisions across related rows and scored its delivery model 3.96× faster in internal tests.

TreeWalker Speeds Up Tree Ensembles by Sharing Work Across Related Rows

Delivery estimates do more than inform. They decide whether a shopper finishes the order and which shipping option they pick. Shopify predicts those dates with machine learning, and the estimates appear in agentic search results, on product pages, at checkout, and in the Shop app. That spread puts pressure on latency. The model has to be accurate, and it has to be fast enough that checkout stays quick.

Shop Promise showing an estimated arrival date, trusted delivery, and free returns

TreeWalker is Shopify's answer to one specific cost in that pipeline: scoring many closely related rows with the same tree ensemble. It's a Rust inference engine for tree ensembles, now open source, and it works with models trained in LightGBM and XGBoost without changing training. In internal model-scoring measurements, Shopify reports TreeWalker scored its delivery model 3.96× faster than its current delivery evaluator, with the same training, trees, and results. My read is that the interesting part isn't the headline number. It's the observation behind it: a lot of inference work is repeated, and repetition is something an engine can see and remove.

Why the same questions get asked again and again

Shopify's main delivery system is a discrete-time hazard model. It estimates the conditional probability that a package arrives on each future date, phrased as a sequence of questions: given that it didn't arrive on Monday, what's the chance it arrives on Tuesday? Those conditional probabilities form a distribution, and the distribution yields a delivery date or a date range.

The structure creates repetition. One forecast scores several related rows, one per time horizon, through the same tree ensemble. The shipment facts, like weight and carrier, stay fixed while the horizon and calendar features change. Checkout adds a second layer. It scores several shipping methods for each order, and each method needs its own delivery-date forecast. Those forecasts must fit inside a small latency budget, and repeating identical tree decisions for every date consumes that budget.

Shared inputs and repeated tests show up in many applications, not only delivery. When that happens, a standard evaluator treats each row as an island and walks the forest from the root every time.

A quick primer on what a tree actually does

A decision tree takes a row of features, such as carrier, package weight, or recipient location, and walks a sequence of tests. Each test sends the row down a branch. The walk ends at a leaf, and the leaf holds a value. Training picks the tests, the thresholds, and the leaf values from examples. Prediction just follows those learned choices.

A simple case makes this concrete. Imagine a tree that only looks at a feature called horizon. A horizon of two days goes down the "yes" branch to a leaf worth −0.25. A horizon of eight goes "no" to a leaf worth +0.25. Deeper tests could refine either branch, but the logic stays the same.

Gradient-boosted systems such as LightGBM and XGBoost combine many such trees into an ensemble. Each tree adds its leaf score to a running total for the row. The model then applies a base score and an output transformation. For binary-logistic models, that transformation is a sigmoid, which maps the total to a probability. A single inference can involve hundreds of short tree walks, and a batch of related rows multiplies that count.

Following one forecast through three trees

The clearest way to see the saving is a small worked example. Shopify's article uses a teaching model with three trees, rather than the production delivery model, which has far more trees. The example asks a question the delivery system asks: for one fictional package, what's the chance it arrives on each of the next twelve dates, given it hasn't arrived earlier? Those twelve dates run from July 4 through July 15, and each date becomes one row.

The horizon gives each row its position in the forecast, from one through twelve. The day of month places it on the calendar, from 4 through 15. Two things stay constant across all twelve rows. The package is Carrier A and weighs 2 kg. The article calls a set of rows like this a group: constant features share identical values across it, and varying features can differ. The producer of the rows guarantees that equality, which is the property that makes sharing possible.

The three trees start with the same check: is the carrier A, and does the package weigh more than 1 kg? Trees A and B then test whether the horizon is at most six days. Tree A continues with questions about weight and day of month. Tree B reaches its leaves at that point. Tree C tests whether the horizon is at most nine days.

In a standard evaluator, each of the twelve rows walks all three trees on its own. That's 36 leaf additions, and many of the intermediate tests repeat the same answer. The carrier and weight checks give the same result for every row. The horizon-at-most-six test appears in two trees and gets asked again for each row.

Prepare shared answers once, then follow only the occupied branches

TreeWalker resolves the shared prefix once for the group. Because every row has Carrier A and 2 kg, the carrier and weight answers can be computed a single time and reused across all three trees. It also notices that Trees A and B both ask about the horizon threshold. It prepares the set of matching rows for that test once, covering horizons one through six, and both trees use it.

The traversal then divides the group as it goes. At Tree A's horizon test, rows one through six take one branch and rows seven through twelve take the other. Rows one through six reach a weight check. The package weighs 2 kg, which is under the 3 kg threshold, so all six rows go to a −0.5 leaf together. The engine keeps them together even though other rows went elsewhere at the earlier split.

Rows seven through twelve reach a day-of-month test. Their dates are July 10 through 15, so every row answers "yes." The sibling branch is empty for this group, and TreeWalker skips it without a recursive descent. The article calls this the unsplit shortcut, and it's the piece I found most useful. A node only costs work when the current rows actually diverge there.

That makes the behavior depend on the group, not only the tree. If the first forecast date moves to July 23, the later horizons cover July 29 through August 3. Rows for July 29 through 31 answer "no" and reach a +0.25 leaf. August 1 through 3 answer "yes" and reach a +0.5 leaf. The model and the twelve-row horizon range are the same. The day-of-month test splits execution only because the dates changed.

Tree B reuses the prepared six-day horizon answer and adds −0.25 to the early rows and +0.25 to the later ones. Tree C uses its own prepared answer for "horizon at most nine." For the July 4 forecast, the running totals work out as follows. Horizons one through six finish at −0.5. Horizons seven through nine finish at +1. Horizons ten through twelve finish at +0.5. Each date keeps a separate total, and that total is final only after all three trees have contributed.

The article's demo stops at the leaf sums so every addition is visible. Grouped traversal performs the same 36 leaf additions and produces the same separate total for every date. The base score and output transformation apply afterward, as usual. The saving comes from skipping repeated decisions on the way to those leaves. It doesn't come from skipping the outputs.

Shopify describes this as an application of partial evaluation, a technique in which you specialize a computation on inputs known to be shared, then continue with the parts that depend on the varying inputs. The learned tests and leaf contributions don't change. The model is the same. Only the execution order is reorganized around the group.

Why the implementation matters as much as the idea

The idea would be easy to lose in a naïve implementation. Shopify says that copying lists of rows at each decision point would run slower than plain LightGBM. The engine had to respect how modern CPUs behave: compact data representations, good use of L2 cache, and as little branching as possible.

The key structure is a row mask, one bit per row that records whether the row is still active. Intersecting the active mask with a predicate's prepared mask selects the matching rows, and the remaining active rows take the other branch. Original row positions stay fixed, so each result lands in the right output slot.

Prepared masks describe the whole group, while each node works on its current subset. In the July 4 example, the "day of month at most 15" mask includes all twelve rows. Tree A reaches that node with only rows seven through twelve, so the engine intersects the prepared mask with the current subset. That keeps the earlier rows from re-entering the branch.

At model load time, TreeWalker identifies repeated predicates and matching prefixes. For each input group, it sorts the values of each varying numeric feature once and sweeps across the distinct thresholds to build reusable masks. The sorting, sweeping, and mask storage all cost something, and small groups may not earn that cost back. The article is candid about this, and it's a fair point to weigh before adopting the engine.

The rest of the traversal benefits from compact nodes and a memory layout that places each node's preferred child next to it. That improves locality and lets execution run in a loop along the preferred path. Wider groups need more mask words, and every active row still needs its leaf contribution added. Sharing removes repeated decisions, but it can't remove the outputs the caller asked for.

Where TreeWalker sits next to other tree-inference tools

Other projects target the same cost from different angles. lleaves compiles LightGBM models to machine code, and TL2cgen generates compiled prediction code from tree ensembles. Compilation cuts execution overhead for each walk. Group-aware evaluation asks a different question: which work can be shared across inputs? The two approaches aren't mutually exclusive, but they answer different questions, so the right comparison depends on the workload you already run.

What the measurements show, and where they stop

The published TreeWalker paper evaluates grouped execution on two openly available survival datasets, SUPPORT and FLCHAIN, plus Expedia hotel-search sessions for click-through-rate prediction. The main baseline is TreeWalker's own independent-row full walk, using the same model representation and layout. That choice isolates the grouped strategy, including its preparation costs, from differences between engines.

On SUPPORT and FLCHAIN, the speedup grows with group width across eight forecast widths. At one row, the two approaches are roughly at parity. At 104 to 128 rows, the measured speedup reaches 7.7× to 9.1×. The paper also includes a theorem-informed fit to those points. The theorem separates three costs: visited split nodes, per-row leaf writes, and preparing predicate masks. Sharing spreads some of those costs across more rows, but output writes and preparation remain.

To turn that structure into a smooth curve, the authors approximate each evaluator's time as a fixed cost plus a per-row cost. The resulting speedup takes the form S(G) = (G + c) / (a + bG), where G is group width. The three positive coefficients are calibrated per series against all eight measurements, which weights each point equally in log speedup. The largest pointwise discrepancy is about 6.5%. That figure describes fit error on these observations. It says nothing about accuracy on a new model or machine, and the authors frame the fit as descriptive rather than predictive.

The timing conditions matter for reading these numbers. They are single-core model-scoring measurements on an Intel Xeon Platinum 8581C, with Arm results on Google Axion. Complete groups are timed in microseconds, and repeated blocks are summarized by the median of their medians. Full-walk latency is divided by grouped latency. The timed evaluator uses shared predicate precomputation and ordinary floating-point accumulation, with exact-sum mode disabled. These measurements cover model scoring, not end-to-end service latency. A full experiment archive records the protocol, code, and measurements.

A second workload tests what happens when more inputs vary. The authors built 128 what-if variants of each UCI credit record and scored them with one LightGBM model. As the number of perturbed dimensions rises from one to eight, the speedup over full walk falls from 15.2× to 8.1× on Intel, and from 9.2× to 6.2× on Arm. Sharing still helps, but each extra varying input shrinks the set of answers that can be reused.

The broader campaign, covering 712 configurations, also shows where the approach loses. Grouped traversal is slower than its own full walk in 33 Intel configurations and 78 Arm configurations, mostly at small group sizes. Compiled engines win some subsets too: TL2cgen is faster in 122 of the 712 Intel configurations. The paper's advice, which I'd repeat, is to benchmark against the engine you already use, on your own group sizes.

Spotting groups in your own workload

Fast delivery forecasts motivated TreeWalker, and that's the case Shopify measured most directly. Still, the same shape of work appears elsewhere. Consider search ranking. A relevance or click-through model may score every result for a single query. The user and query context stay fixed while each result brings its own attributes. Walking each result independently repeats every test on the shared context. A grouped engine can reuse those answers and still return a separate score per result.

Scenario analysis is another natural fit. When you vary a few inputs and hold the rest fixed, you've built a group. The what-if workload in the paper is an example, and its results show the trade-off: the more inputs you vary, the less sharing you get back.

TreeWalker is open source, and the code is on GitHub. The paper is on arXiv. If your application already produces related predictions, whether delivery dates, ranked search results, or scenario variants, check the group sizes and the number of varying features first. My read is that the payoff is largest when the shared context is wide and the varying part is narrow. Measure that on your own models before reorganizing anything, and keep an eye on how the group-size curve shifts in future releases, since that's where the next gains are likely to show up.

Source: Christopher Miller · shopify.engineering

From the same set