Download base package sources from posit-dev/oak-r-sources#1328
Conversation
lionel-
left a comment
There was a problem hiding this comment.
Nice!
One minor comment: I know that ty has ty_vendored as well, but I'd have included these in oak_sources rather than in a separate oak_vendored crate. Fine either way.
Larger concern: Including the sources in a single binary blob of size ~N means that the repo will increase by N for every patch/minor/major release we add support for. By contrast, storing a root source of size N then separate diffs of sizes ~D means we're only increasing the repo size by D when a new release comes out.
That's not a deal breaker though. I do like when a git clone goes fast, but I leave it up to you to decide which approach to go for.
And test that `VERSIONS` is sorted in case we ever bork `versions.txt`
6b55884 to
9369b4e
Compare
.tar.zstposit-dev/oak-r-sources
…he oak-r-sources release version If we ever add more R package folders in a future oak-r-sources release, we want to have a cache miss when the release version changes
|
Due to to licensing concerns (which we aren't fully sure of the ramifications of), we didn't want to embed R's GPL-2 licensed files directly in the MIT licensed ark, even though we effectively are just an "installer" of those files and don't run them ourselves, which should be fine under GPL. Regardless, we have now set up posit-dev/oak-r-sources to hold the infrastructure for making the compressed At each new R release, we will go over and do a new release of I have purposefully chosen a versioning scheme of On the Ark side, after a new In Ark, the cache insert for base R packages works in 2 stages:
When a user changes R versions, we already have There's no possible way to have an "outdated" version of |
Here are some pointers on the matter: |
This is probably one of the more technically impressive PRs I've ever done 😛
Previously in
oak_source::get_r("4.5.0")we would download the 100mb R 4.5.0 tarball from CRAN and store the whole thing on disk in the cache so we could have access to base package source files. We would repeat this for any other R version the user uses.We don't really use much of that downloaded tarball right now, just
src/library/{package}/R/{files}.Rto have the original source files for the base R package of interest.@lionel- suggested that maybe we could vendor the base packages' R files, starting with the full files for R 4.2.0 (our last supported version of R) and shipping git diffs for all subsequent patch versions that could be applied on the fly. Claude and I have come up with something even better 😄.
Zstd magic
Instead we inline a single 1.5 MB
base-sources.tar.zstwithinclude_bytes!()into the ark binary that we decompress and untar on the fly. This contains the completeR/sources for every base package for every patch release since R 4.2.0, all ~20 of them. This is an incredible technical achievement, as you are about to see.base-sources.tar.zstbase-sources.tardecompressed (by ✨ magic ✨ )base-sources/folder fully untarredSo how the hell did we pack 142.7 MB into 1.5 MB? Good question!
You see, a single R version's worth of
src/library/{package}/R/{files}.Rfiles weigh in at about 8 MB. Tarred and compressed, this comes in at about 1.3 MB for just that one R version (very close to our 1.5 MB!).Between R versions, the
{files}.Rdon't actually change all that much! We take advantage of this by leveraging a zstd feature called the compression window. Zstd maintains an adjustable "lookback window" of bytes that it uses to look for repeating patterns in the blob you are compressing. If it finds patterns, it can highly compress them.We leverage this by placing the same file across R versions adjacent to each other in the
.tar. For example, rather than tarring in this naive order:we instead sort the files like this before tarring:
This places
4.5.0/base/R/a.Rright next to4.6.0/base/R/a.Rin the tar, and they are basically the same file between R versions, so when zstd sees this it basically removes all of the duplication in the compressed result.This means that we can store a whopping 20 versions of R sources for nearly the same price as 1, and the size is small enough that we can just vendor it in to ark itself.
Binary size
What I don't understand is what
cargo buildis doing. With a debug build, you can "see" the difference in the binary size:It got...smaller?? I have no idea how that could happen, but I tried this multiple times and got the same result? I even did a
cargo cleanbetween branch switches. I'm surely doing something wrong.But it's definitely working!
Screen.Recording.2026-07-09.at.8.59.21.AM.mov
Updating
In terms of updating, we will need to update the bundled
base-sources.tar.zstafter every patch R release. All we have to do is:versions.txtwith the new versionjust vendor-r-sources, which will re-download all the R sources from CRAN and recreate an updatedbase-sources.tar.zstthat we'd commitSome other things
Cache, so once a particular R version's sources are unpacked from the compressed tarball and stored in the cache, they are never unpacked again. That said, those sources will be subject to the same "eviction" rules as any otherCache, so if you don't use an R version's sources for 4 months, it will be evicted (freeing up around 8 MB of space).