Skip to content

Commit f674859

Browse files
committed
refactor: move SparseArrays into an extension
1 parent 8992aaa commit f674859

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

Project.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
name = "MaybeInplace"
22
uuid = "bb5d69b7-63fc-4a16-80bd-7e42200c7bdb"
33
authors = ["Avik Pal <avikpal@mit.edu> and contributors"]
4-
version = "0.1.3"
4+
version = "0.1.4"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
88
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
99
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
10+
11+
[extensions]
12+
MaybeInplaceSparseArraysExt = "SparseArrays"
13+
14+
[weakdeps]
1015
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1116

1217
[compat]
1318
ArrayInterface = "7.6"
14-
LinearAlgebra = "<0.0.1, 1"
19+
LinearAlgebra = "1.9"
1520
MacroTools = "0.5"
16-
SparseArrays = "<0.0.1, 1"
21+
SparseArrays = "1.9"
1722
julia = "1.9"

ext/MaybeInplaceSparseArraysExt.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module MaybeInplaceSparseArraysExt
2+
3+
using MaybeInplace: MaybeInplace
4+
using SparseArrays: AbstractSparseArray
5+
6+
# Sparse Arrays `mul!` has really bad performance
7+
# This works around it, and also potentially allows dispatching for other array types
8+
MaybeInplace.__mul!(C::AbstractSparseArray, A, B) = (C .= A * B)
9+
MaybeInplace.__mul!(C::AbstractSparseArray, A, B, α, β) = (C .= α * A * B .+ β * C)
10+
11+
end

src/MaybeInplace.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module MaybeInplace
22

3-
using LinearAlgebra, MacroTools, SparseArrays
4-
import ArrayInterface: can_setindex, restructure
5-
import SparseArrays: AbstractSparseArray
3+
using LinearAlgebra: LinearAlgebra, axpy!, mul!
4+
using MacroTools: MacroTools, @capture
5+
using ArrayInterface: can_setindex, restructure
66

77
## Documentation
88
__bangbang__docs = """
@@ -304,12 +304,8 @@ const OP_MAPPING = Dict{Symbol, Function}(:copyto! => __copyto!!, :.-= => __sub!
304304
return :(@. y += α * x)
305305
end
306306

307-
# Sparse Arrays `mul!` has really bad performance
308-
# This works around it, and also potentially allows dispatching for other array types
309307
__mul!(C, A, B) = mul!(C, A, B)
310-
__mul!(C::AbstractSparseArray, A, B) = (C .= A * B)
311308
__mul!(C, A, B, α, β) = mul!(C, A, B, α, β)
312-
__mul!(C::AbstractSparseArray, A, B, α, β) = (C .= α * A * B .+ β * C)
313309

314310
## Macros
315311
for m in (:bangbang, :bb, :❗)

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
33
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
44
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
5+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
56
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
67
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

test/basictests.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using MaybeInplace, StaticArrays, Test
1+
using MaybeInplace, StaticArrays, Test, SparseArrays
22

33
function copyto!!(y, x)
44
@bb copyto!(y, x)
@@ -92,6 +92,11 @@ end
9292
z = @SVector[1.0, 1.0]
9393
@test matmul!!(y, x, z) == @SVector[2.0, 2.0]
9494
@test y == @SVector[0.0, 0.0]
95+
96+
x = sprand(100, 100, 0.01)
97+
z = sprand(100, 100, 0.01)
98+
y = x * z
99+
@test matmul!!(y, x, z) == x * z
95100
end
96101

97102
@testset "similar" begin

0 commit comments

Comments
 (0)