Skip to main content

Shared, private and separate libraries

Source: vignettes/libraries.qmd

By default, install_library() compiles every installed client into one shared library. All clients in a library share one Julia environment, so when their [compat] bounds conflict, the build leaves the later ones out (see When clients conflict). Such a client can instead get a private library of its own, or go into a separate library that you select for the R sessions that use it. This vignette compares the three.

Shared Private Separate
Set up with install_library() install_library(private = "pkg") build_library() and JLBIND_LIBRARY
Clients in one session Those in the library All Those in the selected library
Disk space overhead 2 MB 29 MB or more per private client 2 MB per library
Julia runtimes One One per library One
Rebuilt by install_library() install_library() You
Systems All Linux, macOS All

The overhead comes on top of each client’s compiled code, which every layout holds (about 9 MB for CausalStructures.jl; see Bundle sizes). A private library’s copy of the runtime grows with the standard libraries its dependencies use, to 76 MB for CausalStructures.jl, whose dependencies need OpenBLAS. With private or separate libraries, a dependency that several clients share is compiled into each of their libraries. Thus, we recommend you to use the default with shared libraries, unless impossible due to conflicts.

The shared library

install_library() stores the shared library under tools::R_user_dir("jlbind", "cache") and rebuilds it whenever a client or the Julia version changes. It uses the runtime of the Julia that built it, so it is small, and all clients share one Julia heap, one set of threads and each other’s Julia objects. Each build compiles every client, and a client that conflicts with those before it is left out. To choose which clients to build it from, and in which order to resolve them, name them:

jlbind::install_library(packages = c("pkgA", "pkgC"))

Private libraries

On Linux and macOS1, a client can get a library of its own:

jlbind::install_library(private = "pkgB")

This builds the shared library from the other clients, and a library for pkgB with its dependencies resolved for it alone. The private library has its own copy of the Julia runtime, with its symbols renamed (JuliaC’s --privatize), so it loads alongside the shared library and any other private ones, and pkgB works in the same R session as the clients it conflicts with. Later builds keep it, so install_library() rebuilds both. To put every client back into the shared library, pass private = character().

The runtime copy makes the library a bundle, so it runs without Julia installed. It takes disk space, and in each session its own memory and JULIA_NUM_THREADS threads. Julia objects that pkgB returns work with the functions of pkgB only.

Separate libraries

Clients used in different R sessions can each have a library, built with build_library():

jlbind::build_library("pkgA", "~/jlbind/pkgA")
jlbind::build_library(c("pkgB", "pkgC"), "~/jlbind/pkgBC")

Set JLBIND_LIBRARY before the first client loads to select the library for a session, or in a project’s .Renviron for every session in that project:

# jlbind.dll on Windows, jlbind.dylib on macOS
Sys.setenv(JLBIND_LIBRARY = "~/jlbind/pkgA/jlbind.so")
library(pkgA)

Every client in that session then binds to this library, in place of the shared and private ones. A separate library is as small as the shared one, but you rebuild it yourself: after updating one of its clients, run build_library() again (check_bound() reports when it’s needed).

Footnotes

  1. JuliaC gives a library a private Julia runtime on Linux and macOS. Windows support is in progress in JuliaC.jl#151.↩︎