Hi Don thanks for the update.

I tried your patch and on my machine it is definitely faster, see
figures https://github.com/tdhock/atime/issues/135#issuecomment-6748730446

It is faster for all N so as long as the increased memory usage is
acceptable, I would argue that somebody in R-core should accept the
patch.

You wrote "what still grows with N^2 is the workspace, and that is
what limits N" but the emprical memory in the figure seems to be
linear? I guess the quadratic workspace memory is using fortran not R
memory allocation? Two ideas,

1. can you please modify the code so that quadratic workspace memory
uses R memory allocation, so we can see quadratic memory usage in the
atime figure?

2. Is there a way of reducing that workspace memory from quadratic to linear?

Toby

On Fri, Nov 6, 2026 at 5:30 PM Dong Yin <[email protected]> wrote:
>
> Hi Toby,
>
> Thanks for replying
>
> and good idea!
>
> the attached patch (against R-devel r90842) adds predict(..., cache = TRUE)
>
> on the span = 0.5 error, well, span was passed to predict(), which ignores 
> it, so the fit still used 0.75. It needs to go to loess().
>
> also, with or without the patch, the existing workspace limit stops se = TRUE 
> at about N = 46,300 for span = 0.5 (37,800 for 0.75), so N has to stay below 
> that
>
> ares <- atime::atime(
>   N = unique(as.integer(10^seq(1, log10(45000), by = 0.1))),
>   setup = {
>     set.seed(20261105)
>     x <- runif(N, -3, 3)
>     y <- sin(x) + rnorm(N, sd = 0.35)
>     f <- loess(y ~ x, span = 0.5)
>     nd <- data.frame(x = seq(min(x), max(x), length.out = 80L))
>   },
>   seconds.limit = 1,
>   cache.FALSE = predict(f, nd, se = TRUE, cache = FALSE),
>   cache.TRUE = predict(f, nd, se = TRUE, cache = TRUE))
>
> in my run cache = FALSE hit the 1 s limit at N = 10,000, and both gave bit 
> for bit identical predictions
>
> the gap between the two memory curves is just the new table, which is now 
> allocated with R_alloc as you suggested; the existing workspace still uses 
> R_Calloc, which atime doesn't see
>
> i guess the old search is probably what made predict(se = TRUE) grow with 
> N^2. with cache = TRUE the time roughly doubles when N doubles (0.09 s to 
> 0.17 s CPU from N = 20,000 to 40,000), while cache = FALSE roughly quadruples 
> (1.1 s to 4.4 s from 10,000 to 20,000), which fits the log-linear plot you 
> got. what still grows with N^2 is the workspace, and that is what limits N
>
> Thanks,
> Don
>
>
> ________________________________________
> From: Toby Hocking <[email protected]>
> Sent: Friday, 06 November 2026 19:06:03
> To: Dong Yin
> Cc: [email protected]
> Subject: Re: loess: repeated neighbour list search in ehg191 makes predict(se 
> = TRUE) slow
>
> also even better if the temporary arrays used the R memory allocation
> (not fortran), so then we can compare the memory usage (fortran
> allocations are not accounted for in the atime analysis)… is that
> possible? (I’m not great at hacking fortran but I know that could work
> with C)
>
> On Fri, Nov 6, 2026 at 2:03 PM Toby Hocking <[email protected]> wrote:
> >
> > Thanks for the response Don. That is interesting.
> > I tried the span=0.5 but still error on my end.
> > Decreasing the atime seconds.limit from 1 to 0.1 gives an encouraging
> > result which looks log linear though
> > https://github.com/tdhock/atime/issues/135#issuecomment-6692949103
> >
> > The improvement would be more convincing if we could run the two
> > versions side by side in the same R session.
> > Can you make a patch with a new argument, like predict(cache=TRUE) for
> > your new version and FALSE for the old version? That way we could run
> > both in the same R session to more obviously see the improvements.
> >
> > On Thu, Nov 5, 2026 at 4:00 PM Dong Yin <[email protected]> wrote:
> > >
> > > 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.

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to