| Title: | Zero-Copy Julia to R Array Bridge via ALTREP |
|---|---|
| Description: | Provides zero-copy R views of Julia-owned arrays by implementing ALTREP (Alternative Representations) classes that return pointers directly into Julia's memory. The package integrates with 'JuliaCall' and uses C-level finalizers for safe cross-runtime garbage collection. |
| Authors: | Aviezer Lifshitz [aut, cre] (ORCID: <https://orcid.org/0000-0002-8458-9507>), Weizmann Institute of Science [cph] |
| Maintainer: | Aviezer Lifshitz <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-05-23 09:04:17 UTC |
| Source: | https://github.com/tanaylab/jlview |
Check if an object is a jlview ALTREP vector
is_jlview(x)is_jlview(x)
x |
An R object |
TRUE if x is a jlview ALTREP vector, FALSE otherwise
Creates an ALTREP vector (or matrix, if 2D+) backed by Julia memory.
The resulting R object shares the same memory as the Julia array,
avoiding data copying. Modifications to the R object trigger
copy-on-write (unless writeable = TRUE).
jlview(julia_array, writeable = FALSE, names = NULL, dimnames = NULL)jlview(julia_array, writeable = FALSE, names = NULL, dimnames = NULL)
julia_array |
A JuliaObject referencing a Julia array |
writeable |
If |
names |
Optional character vector of names to attach to the result. Attached atomically during construction to avoid ALTREP materialization. |
dimnames |
Optional list of dimnames to attach to the result. Attached atomically during construction to avoid ALTREP materialization. |
An ALTREP vector backed by Julia memory, or a standard R vector if the Julia type is not supported for zero-copy.
## Not run: JuliaCall::julia_setup() # Create a Julia array and view it in R without copying JuliaCall::julia_command("x = randn(1000)") x <- jlview(JuliaCall::julia_eval("x")) sum(x) # operates directly on Julia memory ## End(Not run)## Not run: JuliaCall::julia_setup() # Create a Julia array and view it in R without copying JuliaCall::julia_command("x = randn(1000)") x <- jlview(JuliaCall::julia_eval("x")) sum(x) # operates directly on Julia memory ## End(Not run)
Returns the current amount of Julia memory pinned by jlview objects and the threshold at which forced GC is triggered.
jlview_gc_pressure()jlview_gc_pressure()
A list with pinned_bytes and threshold
Returns metadata about a jlview ALTREP vector including the Julia element type, length, writeability, and release status.
jlview_info(x)jlview_info(x)
x |
A jlview ALTREP vector |
A named list with components:
Julia element type (e.g., "Float64")
Number of elements
Whether the view allows direct writes
Whether the view has been released
Whether COW materialization has occurred
Creates a zero-copy ALTREP view of a Julia NamedArray matrix, preserving row and column names as dimnames.
jlview_named_matrix(julia_named_matrix)jlview_named_matrix(julia_named_matrix)
julia_named_matrix |
A JuliaObject referencing a Julia NamedArray matrix |
An ALTREP matrix with dimnames set from the Julia NamedArray
## Not run: JuliaCall::julia_setup() JuliaCall::julia_command("using NamedArrays") m <- JuliaCall::julia_eval('NamedArray(randn(3,2), (["a","b","c"], ["x","y"]))') x <- jlview_named_matrix(m) rownames(x) # returns c("a", "b", "c") colnames(x) # returns c("x", "y") ## End(Not run)## Not run: JuliaCall::julia_setup() JuliaCall::julia_command("using NamedArrays") m <- JuliaCall::julia_eval('NamedArray(randn(3,2), (["a","b","c"], ["x","y"]))') x <- jlview_named_matrix(m) rownames(x) # returns c("a", "b", "c") colnames(x) # returns c("x", "y") ## End(Not run)
Creates a zero-copy ALTREP view of a Julia NamedArray vector, preserving the axis names.
jlview_named_vector(julia_named_array)jlview_named_vector(julia_named_array)
julia_named_array |
A JuliaObject referencing a Julia NamedArray vector |
An ALTREP vector with names set from the Julia NamedArray
## Not run: JuliaCall::julia_setup() JuliaCall::julia_command("using NamedArrays") v <- JuliaCall::julia_eval('NamedArray([1.0, 2.0, 3.0], (["a", "b", "c"],))') x <- jlview_named_vector(v) names(x) # returns c("a", "b", "c") ## End(Not run)## Not run: JuliaCall::julia_setup() JuliaCall::julia_command("using NamedArrays") v <- JuliaCall::julia_eval('NamedArray([1.0, 2.0, 3.0], (["a", "b", "c"],))') x <- jlview_named_vector(v) names(x) # returns c("a", "b", "c") ## End(Not run)
Unpins the Julia array immediately, freeing memory without waiting for R's garbage collector. After release, accessing the data will error.
jlview_release(x)jlview_release(x)
x |
A jlview ALTREP vector |
Invisible NULL
When total pinned bytes exceeds this threshold, jlview forces an R garbage collection to reclaim stale ALTREP objects. Default is 10GB.
jlview_set_gc_threshold(bytes)jlview_set_gc_threshold(bytes)
bytes |
Threshold in bytes (numeric) |
Invisible NULL
Creates a dgCMatrix-class backed by a zero-copy ALTREP
vector for the nonzero values (x slot). The row indices (i
slot) and column pointers (p slot) are copied and shifted from
1-based (Julia) to 0-based (R) indexing in Julia, then returned as plain
R integer vectors.
jlview_sparse(julia_sparse_matrix, lazy_indices = FALSE)jlview_sparse(julia_sparse_matrix, lazy_indices = FALSE)
julia_sparse_matrix |
A JuliaObject referencing a Julia
|
lazy_indices |
Ignored. Retained for API compatibility only. Previously controlled lazy vs eager materialization of ALTREP index vectors, which have been removed in favor of simple copy+shift in Julia. |
A dgCMatrix-class sparse matrix.
## Not run: JuliaCall::julia_setup() JuliaCall::julia_command("using SparseArrays") m <- JuliaCall::julia_eval("sprand(Float64, 100, 50, 0.1)") s <- jlview_sparse(m) class(s) # "dgCMatrix" ## End(Not run)## Not run: JuliaCall::julia_setup() JuliaCall::julia_command("using SparseArrays") m <- JuliaCall::julia_eval("sprand(Float64, 100, 50, 0.1)") s <- jlview_sparse(m) class(s) # "dgCMatrix" ## End(Not run)
Creates a jlview object and ensures it is released when the scope exits, even if an error occurs. This prevents memory leaks from forgotten releases.
with_jlview( julia_array, expr, writeable = FALSE, names = NULL, dimnames = NULL )with_jlview( julia_array, expr, writeable = FALSE, names = NULL, dimnames = NULL )
julia_array |
A JuliaObject referencing a Julia array |
expr |
An expression to evaluate with the jlview object bound to |
writeable |
Passed to |
names |
Passed to |
dimnames |
Passed to |
The result of evaluating expr
## Not run: JuliaCall::julia_setup() JuliaCall::julia_command("big = randn(100000)") result <- with_jlview(JuliaCall::julia_eval("big"), { c(mean(.x), sd(.x)) }) # .x is automatically released here ## End(Not run)## Not run: JuliaCall::julia_setup() JuliaCall::julia_command("big = randn(100000)") result <- with_jlview(JuliaCall::julia_eval("big"), { c(mean(.x), sd(.x)) }) # .x is automatically released here ## End(Not run)