Vendor base package sources in a compressed .tar.zst#1328
Conversation
| } | ||
|
|
||
| /// Get cached CRAN package source tree if already present | ||
| pub fn get_cran(&self, name: &str, version: &str) -> Option<PathBuf> { |
There was a problem hiding this comment.
I've left this as get_cran() even though there are no other getters right now in case we can figure out how to do Bioconductor in the future, so we'd have get_bioc() too
There was a problem hiding this comment.
I was going to setup the xtask idea for this but apparently you can just create an examples/ folder and put arbitrary scripts with a main entrypoint here? They run with all of your dev-dependencies, and you run it like cargo run -p oak_vendored --example vendor, which just vendor-r-sources does
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
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).