Vendor base package sources in a compressed .tar.zst#1328
Open
DavisVaughan wants to merge 3 commits into
Open
Conversation
DavisVaughan
commented
Jul 9, 2026
| } | ||
|
|
||
| /// Get cached CRAN package source tree if already present | ||
| pub fn get_cran(&self, name: &str, version: &str) -> Option<PathBuf> { |
Contributor
Author
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
Contributor
Author
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
Comment on lines
+222
to
+230
| let components: Vec<&str> = path | ||
| .components() | ||
| .map(|component| component.as_os_str().to_str()) | ||
| .collect::<Option<Vec<_>>>()?; | ||
|
|
||
| // R-{version} / src / library / {package} / R / <rest...> | ||
| let [_r_version, "src", "library", package, "R", rest @ ..] = components.as_slice() else { | ||
| return None; | ||
| }; |
Contributor
Author
There was a problem hiding this comment.
I thought this was rather cool
And test that `VERSIONS` is sorted in case we ever bork `versions.txt`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cacheto evict when untouched for 4 months #1323 after it is merged firstThis 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).