changeset 1004:7fd37aab84fe refactor/lazy_tensors

Simplify bounds handling for LazyElementwiseOperation
author Jonatan Werpers <jonatan@werpers.com>
date Sun, 20 Mar 2022 21:35:20 +0100
parents 7ef605b8f132
children becd95ba0fce
files src/LazyTensors/lazy_array.jl test/LazyTensors/lazy_tensor_operations_test.jl
diffstat 2 files changed, 7 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/LazyTensors/lazy_array.jl	Sun Mar 20 21:19:00 2022 +0100
+++ b/src/LazyTensors/lazy_array.jl	Sun Mar 20 21:35:20 2022 +0100
@@ -64,15 +64,13 @@
 
 Base.size(v::LazyElementwiseOperation) = size(v.a)
 
-evaluate(leo::LazyElementwiseOperation{T,D,:+}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] + leo.b[I...]
-evaluate(leo::LazyElementwiseOperation{T,D,:-}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] - leo.b[I...]
-evaluate(leo::LazyElementwiseOperation{T,D,:*}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] * leo.b[I...]
-evaluate(leo::LazyElementwiseOperation{T,D,:/}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] / leo.b[I...]
+evaluate(leo::LazyElementwiseOperation{T,D,:+}, I::Vararg{Int,D}) where {T,D} = @inbounds leo.a[I...] + leo.b[I...]
+evaluate(leo::LazyElementwiseOperation{T,D,:-}, I::Vararg{Int,D}) where {T,D} = @inbounds leo.a[I...] - leo.b[I...]
+evaluate(leo::LazyElementwiseOperation{T,D,:*}, I::Vararg{Int,D}) where {T,D} = @inbounds leo.a[I...] * leo.b[I...]
+evaluate(leo::LazyElementwiseOperation{T,D,:/}, I::Vararg{Int,D}) where {T,D} = @inbounds leo.a[I...] / leo.b[I...]
 
-Base.@propagate_inbounds @inline function Base.getindex(leo::LazyElementwiseOperation{T,D}, I::Vararg{Int,D}) where {T,D}
-    @boundscheck if !checkbounds(Bool, leo.a, I...)
-        throw(BoundsError([leo], I...))
-    end
+function Base.getindex(leo::LazyElementwiseOperation{T,D}, I::Vararg{Int,D}) where {T,D}
+    @boundscheck checkbounds(leo.a, I...)
     return evaluate(leo, I...)
 end
 
--- a/test/LazyTensors/lazy_tensor_operations_test.jl	Sun Mar 20 21:19:00 2022 +0100
+++ b/test/LazyTensors/lazy_tensor_operations_test.jl	Sun Mar 20 21:35:20 2022 +0100
@@ -25,7 +25,7 @@
 end
 
 
-@testset "TensorApplication" begin
+@testset "LazyTensorApplication" begin
     struct SizeDoublingMapping{T,R,D} <: LazyTensor{T,R,D}
         domain_size::NTuple{D,Int}
     end