Skip to contents

Introduction

This article shows where time and memory go in a cuplyr pipeline, when to use eager or lazy execution, and where the GPU loses to plain dplyr. The numbers come from one machine. vignette("query-optimization", package = "cuplyr") explains the optimizer passes. This article measures what they buy.

In short, use lazy (the default) for pipelines. It is not faster. On clean GPU memory it ties with eager up to 1e6 rows and is 0.67 to 1.0 ms slower at 1e7 and 5e7 rows. Its advantage is memory. Lazy frees every intermediate column when the pipeline finishes. Eager keeps them until R’s garbage collector runs, so in a loop they pile up until the card is full and cuplyr’s out-of-memory handler has to step in. In the measurements below that made eager 3 to 7 times slower than lazy.

How a cuplyr call runs

Each verb does some work in R, such as checking arguments and working out the new schema. In eager mode it then runs on the GPU. At 1e3 rows the R part dominates. A profile of one eager verb at 1e3 rows put about 80% of the time in R for filter(), mutate() and arrange(), 98% for select() and 61 to 65% for a grouped summarise(). That fixed cost is the per-call floor.

The two modes differ in when the GPU work happens.

  • Eager runs each verb at once and returns a GPU table. Every intermediate table stays allocated until R’s garbage collector drops it.
  • Lazy records each verb as a node in a plan without touching the GPU. compute() optimizes and runs the plan and keeps the result on the GPU. collect() does the same and copies the result to R. Intermediates are freed when the call returns.

To use eager, pass tbl_gpu(df, lazy = FALSE) for one table, or set options(cuplyr.exec_mode = "eager") or CUPLYR_EXEC_MODE=eager for every table created afterwards (see ?"cuplyr-options"). Printing a lazy table runs the plan to show its first rows and keeps nothing, so it costs about as much as a collect().

explain() prints the plan of a lazy table as R code: the plan the optimizer will run. show_query(optimized = FALSE) prints the steps as you wrote them.

lazy <- tbl_gpu(mtcars) |>
  filter(mpg > 20) |>
  mutate(kpl = mpg * 0.425, hp2 = hp * 2) |>
  select(cyl, kpl)

explain(lazy)
#> <cuplyr plan: optimized>
#> table on the GPU
#>   columns read: 2 of 11 (mpg, cyl)
#> filter(mpg > 20)
#> mutate(kpl = mpg * 0.425)
#> select(cyl, kpl)

In the optimized plan the mutate has one expression instead of two, because nothing uses hp2, and the source line shows that the plan reads only 2 of the 11 columns: mpg for the filter and the mutate, cyl for the select. Nothing has run on the GPU yet. collect(lazy) runs the plan.

compute() and collect() run six optimizer passes.

  • Projection pruning keeps only the columns later steps need. This is the columns read: 2 of 11 line above.
  • Mutate fusion merges consecutive mutate() calls into one step, keeping the order you wrote. It skips the merge if the result would have more than 8 expressions, or if the later mutate() reads more than 4 columns the earlier one creates.
  • Dead-column pruning drops mutate outputs nothing uses, like hp2 above.
  • Filter pushdown moves a filter below a mutate when the filter doesn’t read a column the mutate computes, so the mutate sees fewer rows. It also moves filters below a select() that keeps their columns, and into the inputs of some joins. That depends on the join type and its relationship, multiple and unmatched arguments. explain() shows where each filter ended up.
  • Filter reordering merges consecutive filter() calls into one node that builds one row mask, and puts cheaper predicates first, judged by expression size. Eager runs each filter() separately and allocates a table between them.
  • Scan pushdown hands filters on a lazy read_parquet_gpu() table to the parquet reader, so it skips data it doesn’t need to read.

A single filter() with several predicates is already one node in both modes, and the optimizer leaves its predicate order alone unless it merges it with another filter. Verbs such as arrange() and summarise() are barriers. The passes run on each side of them but not across.

Measurements

The tables below are static. Nothing is timed when this vignette builds. The code is at the end.

  • Date and version: 2026-10-26, cuplyr 0.1.1.9000 (a development version before 0.2.0). Some figures from earlier versions are quoted for comparison.
  • Hardware and software: NVIDIA GeForce RTX 5070 (12 GB), driver 615.78.08, R 4.6.1, dplyr 1.2.1, the default async memory resource.
  • Method: data already on the GPU, so uploads are not in the pipeline timings. Warm runs, median of 15 samples (9 at 5e7 rows). Each sample times a batch of calls fenced with gpu_sync(), aiming at 30 ms per batch. Calls slower than 30 ms ran one per batch. gc() ran untimed before every batch. Each number is the median of three fresh R processes, which differed by up to 4% for the cuplyr pipelines and up to 10% elsewhere. The timer has 1 ms resolution, so gaps of 0.1 ms between eager and lazy at 1e3 and 1e6 rows are noise.
  • Some figures under “Reading the results” come from separate profiling runs. The text says where.

A five-verb pipeline

filter(x > 0.1) |> mutate(z = x * y + w) |> group_by(g) |>
  summarise(m = mean(z), n = n()) |> arrange(desc(m)) |> collect()

The table has an integer g with 100 groups and double columns x, y and w. The dplyr column runs the same pipeline on a tibble. “Held” is the GPU memory one call leaves allocated until R’s next gc(), measured as the growth of gpu_memory_state()$pool_used_bytes.

rows eager ms lazy ms dplyr ms eager held MB lazy held MB
1e3 1.87 1.75 3.50 0.04 0
1e6 2.00 2.00 22.0 32 0
1e7 4.33 5.00 213 324 0
5e7 17.5 18.5 not run 1620 0
5e7, 5 calls back to back, no gc() 68.4 18.0 not run

The last row is what a loop does. It runs the pipeline five times without a gc() and reports the mean time per call. The eager figure depends on what the memory pool already holds, and 68.4 ms is the low end. In two earlier runs of the same loop (commit b39a8f1) eager took 145 and 155 ms per call, against 22 and 24 ms for lazy.

One eager verb at 1e3 rows

No collect(). The result stays on the GPU.

verb cuplyr ms dplyr ms
filter 0.36 0.35
mutate 0.37 0.39
select 0.10 0.42
arrange 0.14 0.68
grouped summarise 0.40 0.77

Transfers (4 columns)

rows tbl_gpu() upload ms collect() of all rows ms
1e3 0.13 0.10
1e6 6.00 6.00
1e7 63.0 54.0

A case built for the optimizer

A 21-column table (g plus x1 to x20) and a query that computes columns it later drops and filters after the mutate:

mutate(a = x1 * 2, b = x2 * x3, c = x4 + x5) |> filter(x1 > 0.9) |>
  select(g, a) |> collect()

explain() shows the optimizer reading 2 of the 21 columns, moving the filter below the mutate, and dropping b and c.

rows eager ms lazy ms dplyr ms eager held MB lazy held MB
1e6 1.73 1.73 22.5 43 0
1e7 6.20 4.86 183 428 0

Reading the results

  • Up to 1e6 rows lazy ties with eager or is a little faster. With longer batches (0.15 s each, 11 interleaved samples, three fresh processes) lazy took 1.75 to 1.76 ms against 1.87 to 1.88 at 1e3 rows, and 1.95 to 1.98 ms against 2.05 to 2.08 at 1e6, 4 to 6% less. At 1e7 rows lazy is 0.67 ms behind (5.00 against 4.33 ms, 15%) and at 5e7 rows 1.0 ms behind (18.5 against 17.5 ms, 6%). Earlier in the 0.2.0 development cycle those two gaps were 1.2 and 1.5 ms.
  • At 1e3 rows, in two profiling runs, recording the five-verb lazy plan took 1.0 ms, all of it in R. collect() took 0.66 ms: 0.04 ms to optimize, 0.43 ms to lower and run, 0.09 ms to copy the result to R, and the rest bookkeeping. Recording time doesn’t depend on row count.
  • Lazy is slower on big tables because it loses overlap. At 1e7 rows, in a profiling run, collect() took 3.8 ms, 3.6 ms of it lowering and running the plan. The optimizer leaves this plan unchanged, so both modes run the same five GPU steps. An eager verb returns while its GPU work is still running, so the next verb’s R work runs alongside it. Lazy does all its R work first while the GPU waits. Forcing eager to wait after every verb removes the overlap. In three runs eager took 4.25 to 4.33 ms normally, 5.14 to 5.17 ms with a gpu_sync() after each verb, and lazy took 5.00 ms. So lost overlap accounts for all of lazy’s 0.67 ms. The overlap grows with the GPU work, which is why the gap shows only on big tables. Freeing lazy’s intermediates adds about 0.05 ms at every size.
  • In the optimizer case lazy wins only on the bigger table, 4.86 against 6.20 ms at 1e7 rows (22% less). At 1e6 rows both take 1.73 ms because there is too little GPU work for the skipped steps to matter.
  • Adding a filter() or mutate() to a lazy plan took 0.16 to 0.19 ms at every plan length from 1 to 100 verbs, in a profiling run. collect() time grows with plan length. A 100-verb plan of mutate() calls took 8.8 ms. Joins are the exception. Recording one on a 120-node plan took about twice as long as on a 3-node plan (0.61 against 0.30 ms).
  • Memory is the reason to prefer lazy. The eager pipeline leaves about 32 bytes per row behind until R’s next gc(), 324 MB at 1e7 rows and 1620 MB at 5e7. Run it again before the gc() and the pool keeps growing. In a traced run of the five-call loop it went from 5.4 to 11.6 GB on a 12 GB card. Then cuplyr’s out-of-memory handler ran gc() and gpu_gc() and retried the step. That is the back-to-back row, 68.4 against 18.0 ms, lazy 3.8 times faster. The two earlier runs gave 6.6 and 6.7 times.
  • Until the card fills, eager stays faster. In a separate run at 2.5e7 rows, five back-to-back eager calls left about 4 GB behind and took 9.2 ms each, against 10 ms lazy.
  • Against dplyr at 1e3 rows both modes are about twice as fast (1.87 ms eager and 1.75 ms lazy against 3.50 ms). At 1e6 rows cuplyr is 11 times faster, and at 1e7 rows 43 to 49 times.

Where the GPU loses

  • Tiny tables. At 1e3 rows filter() and mutate() take as long as dplyr’s (0.36 and 0.37 ms against 0.35 and 0.39), because R-side work dominates. select(), arrange() and grouped summarise() are 4.2, 4.8 and 1.9 times faster. A whole pipeline is about twice as fast, but that saves under 2 ms.
  • Transfers. Moving data costs more than computing on it. Uploading 1e7 rows takes 63 ms and collecting them 54 ms, against 4.33 ms for the eager pipeline. If you upload, run one cheap verb and collect, the GPU loses.
  • CPU fallbacks. Some expressions have no GPU translation, so cuplyr evaluates them in R and copies data across. This is silent by default. Set options(cuplyr.fallback = "warn") to be told, or "error" to forbid it. The message names the verb, the expression, the reason and how much data was copied. A plan break is different. It happens when a lazy plan has to run early, for example a summarise() of an integer sum(i + j), and it stays on the GPU. options(cuplyr.plan_break = "warn") reports it, and it never errors. See ?"cuplyr-fallback".

Practical advice

  • Upload once, keep the data on the GPU, and collect once at the end.
  • Use lazy for pipelines, especially on big tables and in loops. Don’t expect it to be faster. Its gain is that intermediates don’t pile up.
  • Use eager for interactive exploration and single calls where you want each result at once.
  • To branch from a shared prefix, compute() it once and build the branches from the result, which stays on the GPU.
  • After dropping a large eager result, gc() returns its memory to cuplyr’s pool for the next calls to reuse. gpu_gc() also hands the pool’s unused memory back to the card for other processes. It takes about a second and the pool has to grow again afterwards, so don’t call it inside a loop. See vignette("memory", package = "cuplyr").
  • Fence GPU timings with gpu_sync(). GPU calls return before the kernels finish, so without it the timer can stop early.
  • Check explain() when a lazy pipeline is slower than you expect.

Reproduce

This is a trimmed copy of the script behind the pipeline timings and the memory column. It doesn’t run when the vignette builds. Run it in a fresh session on an idle GPU. Batch sizes come from one timed call that the 1 ms timer rounds, so batches of sub-millisecond calls can run well under or over the 30 ms target.

library(cuplyr)
library(dplyr)
set.seed(1)

# Median ms per call. A sample is a batch of calls fenced with gpu_sync();
# gc() runs untimed before every batch.
per_call <- function(fn, samples = 15, warmup = 3, target = 0.03) {
  for (i in seq_len(warmup)) fn()
  gpu_sync(); gc(verbose = FALSE)
  t0 <- proc.time()[["elapsed"]]; fn(); gpu_sync()
  one <- max(proc.time()[["elapsed"]] - t0, 1e-4)
  reps <- max(1L, min(500L, as.integer(ceiling(target / one))))
  t <- vapply(seq_len(samples), function(i) {
    gc(verbose = FALSE)
    t0 <- proc.time()[["elapsed"]]
    for (j in seq_len(reps)) fn()
    gpu_sync()
    (proc.time()[["elapsed"]] - t0) / reps
  }, numeric(1))
  1e3 * median(t)
}

# GPU memory one call leaves allocated until R's next gc(), in MB.
held_mb <- function(fn) {
  gc(verbose = FALSE); gc(verbose = FALSE); gpu_sync()
  u0 <- gpu_memory_state()$pool_used_bytes
  r <- fn(); gpu_sync()
  u1 <- gpu_memory_state()$pool_used_bytes
  rm(r)
  (u1 - u0) / 1e6
}

pipeline <- function(d) {
  d |>
    filter(x > 0.1) |>
    mutate(z = x * y + w) |>
    group_by(g) |>
    summarise(m = mean(z), n = n()) |>
    arrange(desc(m))
}

n <- 1e6
df <- data.frame(g = sample.int(100L, n, TRUE), x = runif(n), y = runif(n),
                 w = rnorm(n))
tb <- tibble::as_tibble(df)
eg <- tbl_gpu(df, lazy = FALSE)
lz <- tbl_gpu(df, lazy = TRUE)

c(eager = per_call(function() collect(pipeline(eg))),
  lazy  = per_call(function() collect(pipeline(lz))),
  dplyr = per_call(function() pipeline(tb)))
c(eager = held_mb(function() collect(pipeline(eg))),
  lazy  = held_mb(function() collect(pipeline(lz))))