Skip to content

Commit

Permalink
Optimize array access (#128)
Browse files Browse the repository at this point in the history
* Manually optimize array access so it is fast even without LTO

* Format code

* Use sub on pointer, not NonNull
  • Loading branch information
Taaitaaiger authored May 18, 2024
1 parent fff4842 commit 2121360
Show file tree
Hide file tree
Showing 12 changed files with 500 additions and 121 deletions.
268 changes: 256 additions & 12 deletions benches/benches/array_access.rs

Large diffs are not rendered by default.

16 changes: 0 additions & 16 deletions jl_sys/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,22 +1149,6 @@ extern "C" {

pub fn jlrs_array_len(a: *mut crate::types::jl_array_t) -> usize;

#[cfg(any(
feature = "julia-1-6",
feature = "julia-1-7",
feature = "julia-1-8",
feature = "julia-1-9",
feature = "julia-1-10",
))]
pub fn jlrs_array_data(a: *mut crate::types::jl_array_t) -> *mut std::ffi::c_void;

#[cfg(not(any(
feature = "julia-1-6",
feature = "julia-1-7",
feature = "julia-1-8",
feature = "julia-1-9",
feature = "julia-1-10",
)))]
pub fn jlrs_array_data(a: *mut crate::types::jl_array_t) -> *mut std::ffi::c_void;

pub fn jlrs_array_ndims(a: *mut crate::types::jl_array_t) -> usize;
Expand Down
5 changes: 0 additions & 5 deletions jl_sys/src/jlrs_cc_reexport.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ extern "C"
jl_value_t *jlrs_svecref(void *t, size_t i); // X
jl_value_t *jlrs_svecset(void *t, size_t i, void *x); // X
size_t jlrs_array_len(jl_array_t *a);
#if JULIA_VERSION_MINOR <= 10
void *jlrs_array_data(jl_array_t *a);
#endif
#if JULIA_VERSION_MINOR >= 11
void *jlrs_array_data(jl_array_t *a);
#endif
size_t jlrs_array_ndims(jl_array_t *a);
jl_value_t *jlrs_exprarg(jl_expr_t *e, size_t n); // X
void jlrs_exprargset(jl_expr_t *e, size_t n, jl_value_t *v); // X
Expand Down
15 changes: 9 additions & 6 deletions jlrs/src/convert/into_julia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ use crate::{
/// behavior. The type in Rust must always be `#[repr(C)]`. The `DataType` must be an isbits-type.
///
/// [`Value::new`]: crate::data::managed::value::Value::new
#[cfg_attr(feature = "diagnostics", diagnostic::on_unimplemented(
message = "the trait bound `{Self}: IntoJulia` is not satisfied",
label = "the trait `IntoJulia` is not implemented for `{Self}`",
note = "Custom types that implement `IntoJulia` should be generated with JlrsCore.reflect",
note = "Do not implement `ForeignType`, `OpaqueType`, or `ParametricVariant` unless this type is exported to Julia with `julia_module!`"
))]
#[cfg_attr(
feature = "diagnostics",
diagnostic::on_unimplemented(
message = "the trait bound `{Self}: IntoJulia` is not satisfied",
label = "the trait `IntoJulia` is not implemented for `{Self}`",
note = "Custom types that implement `IntoJulia` should be generated with JlrsCore.reflect",
note = "Do not implement `ForeignType`, `OpaqueType`, or `ParametricVariant` unless this type is exported to Julia with `julia_module!`"
)
)]
pub unsafe trait IntoJulia: Sized + 'static {
/// Returns the associated Julia type of the implementor.
///
Expand Down
15 changes: 9 additions & 6 deletions jlrs/src/convert/unbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ use crate::data::managed::value::Value;
///
/// [`Value::unbox`]: crate::data::managed::value::Value::unbox
/// [`ValidLayout`]: crate::data::layout::valid_layout::ValidLayout
#[cfg_attr(feature = "diagnostics", diagnostic::on_unimplemented(
message = "the trait bound `{Self}: Unbox` is not satisfied",
label = "the trait `Unbox` is not implemented for `{Self}`",
note = "Custom types that implement `Unbox` should be generated with JlrsCore.reflect",
note = "Do not implement `ForeignType`, `OpaqueType`, or `ParametricVariant` unless this type is exported to Julia with `julia_module!`"
))]
#[cfg_attr(
feature = "diagnostics",
diagnostic::on_unimplemented(
message = "the trait bound `{Self}: Unbox` is not satisfied",
label = "the trait `Unbox` is not implemented for `{Self}`",
note = "Custom types that implement `Unbox` should be generated with JlrsCore.reflect",
note = "Do not implement `ForeignType`, `OpaqueType`, or `ParametricVariant` unless this type is exported to Julia with `julia_module!`"
)
)]
pub unsafe trait Unbox {
/// The type of the unboxed data. Must be `#[repr(C)]`.
type Output: Sized + Clone;
Expand Down
15 changes: 9 additions & 6 deletions jlrs/src/data/layout/valid_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ use crate::{
/// Safety: implementations of [`ValidLayout::valid_layout`] must not trigger the GC. This means
/// no function can be called that allocates Julia data, calls Julia functions, or can trigger the
/// GC some other way.
#[cfg_attr(feature = "diagnostics", diagnostic::on_unimplemented(
message = "the trait bound `{Self}: ValidLayout` is not satisfied",
label = "the trait `ValidLayout` is not implemented for `{Self}`",
note = "Custom types that implement `ValidLayout` should be generated with JlrsCore.reflect",
note = "Do not implement `ForeignType`, `OpaqueType`, or `ParametricVariant` unless this type is exported to Julia with `julia_module!`"
))]
#[cfg_attr(
feature = "diagnostics",
diagnostic::on_unimplemented(
message = "the trait bound `{Self}: ValidLayout` is not satisfied",
label = "the trait `ValidLayout` is not implemented for `{Self}`",
note = "Custom types that implement `ValidLayout` should be generated with JlrsCore.reflect",
note = "Do not implement `ForeignType`, `OpaqueType`, or `ParametricVariant` unless this type is exported to Julia with `julia_module!`"
)
)]
pub unsafe trait ValidLayout {
/// Must be `true` if the Rust type is a managed type.
const IS_REF: bool = false;
Expand Down
Loading

0 comments on commit 2121360

Please sign in to comment.