Hi Toby and all, The list rejected my last message because of the images (again, sorry), so here it is again in text for everyone else. Toby, sorry for the duplicate.
I don't think the error comes from the patch. Stock R 4.5.2 and the unpatched trunk sources stop with the same message, and the same number, at N = 39810: workspace required (2377642345) is too large probably because of setting 'se = TRUE'. With se = TRUE, loess needs a workspace of roughly (D + 1) x nf x N doubles, and with the default span = 0.75 that passes INT_MAX at about N = 37,800. Without the patch your atime run reaches the 1 second limit long before that, so it never gets there. That's also why I used span = 0.5 for my N = 40,000 timings. The patch itself only adds an integer array of nv x N, and nv is 9 here, so about 1.3 MB at N = 37,000. On the scaling: unpatched predict took 0.08, 1.1, 5.2 and 24.2 s for N = 5,000, 10,000, 20,000 and 40,000 at span = 0.5. So below 10,000 it is much steeper than quadratic for me too, then close to quadratic up to 40,000. I'm not sure yet where the extra at small N comes from. Thanks, Don ________________________________ From: Dong Yin <[email protected]> Sent: Thursday, 05 November 2026 20:58:16 To: [email protected] <[email protected]> Cc: [email protected] <[email protected]> Subject: RE: loess: repeated neighbour list search in ehg191 makes predict(se = TRUE) slow Hi Toby, Thanks for trying it! I don't think the error comes from the patch. Stock R 4.5.2 stops with the same message, and the same number, at N = 39810: With se = TRUE, loess needs a workspace of roughly (D + 1) x nf x N doubles, and with the default span = 0.75 that passes INT_MAX at about N = 37,800 (panel B below). Without the patch your atime run reaches the 1 second limit long before that, so it never gets there. That's also why I used span = 0.5 for my N = 40,000 timings. The patch itself only adds an integer array of nv x N, and nv is 9 here, so about 1.3 MB at N = 37,000. well� i wonder Your plot made me look at the scaling again (panel A). Below N = 10,000 it is much steeper than quadratic for me too, then close to quadratic from 10,000 to 40,000. I'm not sure yet where the extra at small N comes from. Thanks, Don ________________________________ From: Dong Yin <[email protected]> Sent: Thursday, 05 November 2026 15:22:48 To: [email protected] <[email protected]> Subject: RE: loess: repeated neighbour list search in ehg191 makes predict(se = TRUE) slow Sorry, it looks like the list stripped the text of my previous message, so here it is again without the images. I am a student working on neuroimaging. I found a repeated linear search in ehg191 that makes predict.loess(se = TRUE) much slower than it needs to be. It is still present in R-devel r90840 (src/library/stats/src/loessf.f, lines 799 to 809). For every training observation and every k-d tree vertex, the routine scans that vertex's neighbour list backwards, so with a fixed span the search work grows with n squared. This is the path ggplot2's geom_smooth(method = "loess") takes by default, since it draws a confidence band, so I suspect quite a few people wait on it without knowing why. The proof of concept (ehg191-lookup.diff.txt, attached to my previous message: https://stat.ethz.ch/pipermail/r-devel/attachments/20261105/f1a77502/attachment.txt) builds the inverse neighbour positions once and replaces the scan with a lookup. On an Apple M4 chip, predicting at 80 points with n = 40,000 and span = 0.5 took 24.2 s before and 0.22 s after (medians of three paired runs). L and the full prediction result were bit for bit identical in every timing configuration and in 1,000 random configurations with one to three predictors, and 24 further fits, including statistics = "exact", were identical too. I think the slow part can be reproduced with: set.seed(20261105) n <- 20000L x <- runif(n, -3, 3) y <- sin(x) + rnorm(n, sd = 0.35) f <- loess(y ~ x) nd <- data.frame(x = seq(min(x), max(x), length.out = 80L)) system.time(predict(f, nd, se = TRUE)) I rebuilt the trunk loess.R, loessc.c and loessf.f, unchanged and patched, under R 4.5.2 on macOS arm64. Would this be worth turning into a proper patch with a regression test? I would be glad of advice on how you would prefer the workspace to be allocated. Thanks, Don ________________________________ From: Dong Yin Sent: Thursday, 05 November 2026 14:44:33 To: [email protected] <[email protected]> Cc: Y. Fang <[email protected]> Subject: loess: repeated neighbour list search in ehg191 makes predict(se = TRUE) slow Hi all, I am a student working on NeuroImaging. I found a repeated linear search in ehg191 that makes predict.loess(se = TRUE) much slower than it needs to be. It is still present in R-devel r90840 (src/library/stats/src/loessf.f, lines 799 to 809). so, for every training observation and every k-d tree vertex, the routine scans that vertex's neighbour list backwards, so with a fixed span the search work grows with n squared. This is the path ggplot2's geom_smooth(method = "loess") takes by default, since it draws a confidence band, so I suspect quite a few people wait on it without knowing why The attached POC builds the inverse neighbour positions once and replaces the scan with a lookup. The change in loessf.f is: On an apple m4 chip, predicting at 80 points with n = 40,000 and span = 0.5 took 24.2 s before and 0.22 s after (medians of three paired runs). L and the full prediction result were bit for bit identical in every timing configuration and in 1,000 random configurations with one to three predictors, and 24 further fits, including statistics = "exact", were identical too I think the slow part can be reproduced with: set.seed(20261105) n <- 20000L x <- runif(n, -3, 3) y <- sin(x) + rnorm(n, sd = 0.35) f <- loess(y ~ x) nd <- data.frame(x = seq(min(x), max(x), length.out = 80L)) system.time(predict(f, nd, se = TRUE)) I rebuilt the trunk loess.R, loessc.c and loessf.f, unchanged and patched, under R 4.5.2 on macOS arm64 Would this be worth turning into a proper patch with a regression test or some Pls? I would be glad of advice on how you would prefer the workspace to be allocated. Thanks, Don [[alternative HTML version deleted]]
______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
