Skip to content

Commit a9d5a75

Browse files
committed
refactor: remove redundant allocation in constant branch
1 parent e44c0f9 commit a9d5a75

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

src/Evaluate.jl

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,9 @@ function _eval_tree_array(
210210
return deg0_eval(tree, cX, eval_options)
211211
elseif is_constant(tree)
212212
# Speed hack for constant trees.
213-
const_result = dispatch_constant_tree(tree, operators)::ResultOk{Vector{T}}
213+
const_result = dispatch_constant_tree(tree, operators)::ResultOk{T}
214214
!const_result.ok && return ResultOk(_similar(cX, eval_options, axes(cX, 2)), false)
215-
return ResultOk(
216-
_fill_similar(const_result.x[], cX, eval_options, axes(cX, 2)), true
217-
)
215+
return ResultOk(_fill_similar(const_result.x, cX, eval_options, axes(cX, 2)), true)
218216
elseif tree.degree == 1
219217
op_idx = tree.op
220218
return dispatch_deg1_eval(tree, cX, op_idx, operators, eval_options)
@@ -634,37 +632,33 @@ over an entire array when the values are all the same.
634632
nbin = get_nbin(operators)
635633
deg1_branch = if nuna > OPERATOR_LIMIT_BEFORE_SLOWDOWN
636634
quote
637-
deg1_eval_constant(tree, operators.unaops[op_idx], operators)::ResultOk{Vector{T}}
635+
deg1_eval_constant(tree, operators.unaops[op_idx], operators)::ResultOk{T}
638636
end
639637
else
640638
quote
641639
Base.Cartesian.@nif(
642640
$nuna,
643641
i -> i == op_idx,
644-
i -> deg1_eval_constant(
645-
tree, operators.unaops[i], operators
646-
)::ResultOk{Vector{T}}
642+
i -> deg1_eval_constant(tree, operators.unaops[i], operators)::ResultOk{T}
647643
)
648644
end
649645
end
650646
deg2_branch = if nbin > OPERATOR_LIMIT_BEFORE_SLOWDOWN
651647
quote
652-
deg2_eval_constant(tree, operators.binops[op_idx], operators)::ResultOk{Vector{T}}
648+
deg2_eval_constant(tree, operators.binops[op_idx], operators)::ResultOk{T}
653649
end
654650
else
655651
quote
656652
Base.Cartesian.@nif(
657653
$nbin,
658654
i -> i == op_idx,
659-
i -> deg2_eval_constant(
660-
tree, operators.binops[i], operators
661-
)::ResultOk{Vector{T}}
655+
i -> deg2_eval_constant(tree, operators.binops[i], operators)::ResultOk{T}
662656
)
663657
end
664658
end
665659
return quote
666660
if tree.degree == 0
667-
return deg0_eval_constant(tree)::ResultOk{Vector{T}}
661+
return deg0_eval_constant(tree)::ResultOk{T}
668662
elseif tree.degree == 1
669663
op_idx = tree.op
670664
return $deg1_branch
@@ -677,16 +671,16 @@ end
677671

678672
@inline function deg0_eval_constant(tree::AbstractExpressionNode{T}) where {T}
679673
output = tree.val
680-
return ResultOk([output], is_valid(output))::ResultOk{Vector{T}}
674+
return ResultOk(output, is_valid(output))::ResultOk{T}
681675
end
682676

683677
function deg1_eval_constant(
684678
tree::AbstractExpressionNode{T}, op::F, operators::OperatorEnum
685679
) where {T,F}
686680
result = dispatch_constant_tree(tree.l, operators)
687681
!result.ok && return result
688-
output = op(result.x[])::T
689-
return ResultOk([output], is_valid(output))::ResultOk{Vector{T}}
682+
output = op(result.x)::T
683+
return ResultOk(output, is_valid(output))::ResultOk{T}
690684
end
691685

692686
function deg2_eval_constant(
@@ -696,8 +690,8 @@ function deg2_eval_constant(
696690
!cumulator.ok && return cumulator
697691
result_r = dispatch_constant_tree(tree.r, operators)
698692
!result_r.ok && return result_r
699-
output = op(cumulator.x[], result_r.x[])::T
700-
return ResultOk([output], is_valid(output))::ResultOk{Vector{T}}
693+
output = op(cumulator.x, result_r.x)::T
694+
return ResultOk(output, is_valid(output))::ResultOk{T}
701695
end
702696

703697
"""

0 commit comments

Comments
 (0)