I’m using OnlineStats to calculate a likelihood function, so I’m creating and destroying a LogSumExp for each row. LogSumExps, like all online stats, are mutable structs, so heap-allocated in general, but I thought that since the LogSumExp is not visible outside the function the optimizer would get rid of those allocations as discussed here. However, the code in the MWE below prints 0.000000 seconds (1 allocation: 112 bytes)
. Since this happens once per row, it’s a significant performance penalty, any suggestions?
MWE:
using OnlineStats
function logsumexp(vals)
result = LogSumExp()
for val in vals
fit!(result, val)
end
x = value(result)
return x
end
function main()
logsumexp([1, 2, 3, 4, 5, 6])
@time logsumexp([1, 2, 3, 4, 5, 6])
end
main()
Actual example code: DiscreteChoiceModels.jl/src/mnl.jl at main · mattwigway/DiscreteChoiceModels.jl · GitHub