Skip to content

Commit

Permalink
reduce allocations by hinting size
Browse files Browse the repository at this point in the history
  • Loading branch information
kalmarek committed Apr 17, 2024
1 parent 5c20325 commit 91cec1b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/mtables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,13 @@ function MA.operate!(
v::AbstractVector,
w::AbstractVector,
)
l1 = issparse(v) ? nnz(v) : 2^8
l2 = issparse(w) ? nnz(w) : 2^8
l = l1 * l2
idcs = Vector{key_type(res)}()
vals = Vector{eltype(res)}()
sizehint!(idcs, l)
sizehint!(vals, l)

for (kv, a) in nonzero_pairs(v)
for (kw, b) in nonzero_pairs(w)
Expand Down
16 changes: 10 additions & 6 deletions test/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -303,31 +303,35 @@ end
@static if VERSION v"1.9"
@test (@allocations Y * Y) > k^2 - 2 * k
@test Y * Y isa AlgebraElement
@test (@allocations Y * Y) < 50
@test (@allocations Y * Y) 26
else
k1 = @allocated Y * Y
@test Y * Y isa AlgebraElement
Y * Y
k2 = @allocated Y * Y
@test k2 / k1 < 0.2
@test k2 / k1 < 0.5
end

@test all(!iszero, SA.mstructure(fRG).table)

@static if VERSION v"1.9"
YY = deepcopy(Y)
# MA.operate_to!(YY, +, Y, YY)
# YY = deepcopy(Y)
@test_broken @allocations(MA.operate_to!(YY, +, Y, YY)) == 0
@test_broken YY == Y + Y

YY = deepcopy(Y)
# MA.operate_to!(YY, +, YY, Y)
# YY = deepcopy(Y)
@test_broken @allocations(MA.operate_to!(YY, +, YY, Y)) == 0
@test_broken YY == Y + Y

@test_broken @allocations(MA.operate_to!(YY, +, Y, deepcopy(Y))) ==
0
# MA.operate_to!(YY, +, Y, Y)
@test_broken @allocations(MA.operate_to!(YY, +, Y, Y)) == 0
@test_broken YY == Y + Y

@test @allocations(MA.operate_to!(YY, *, Y, Y)) 40
MA.operate_to!(YY, *, Y, Y)
@test @allocations(MA.operate_to!(YY, *, Y, Y)) 25
@test YY == Y * Y
end
end
Expand Down

0 comments on commit 91cec1b

Please sign in to comment.