lanl-ansi / Alpine.jl

A Julia/JuMP-based Global Optimization Solver for Non-convex Programs
https://lanl-ansi.github.io/Alpine.jl/latest/
Other
244 stars 39 forks source link

Unnecessary use of eval in source code #153

Closed julbinb closed 3 years ago

julbinb commented 4 years ago

This use of eval does not do anything useful:

if isa(m.disc_var_pick, Function)
      eval(m.disc_var_pick)(m)

It's similar to writing eval(3) instead of 3. Because m.disc_var_pick is already a function, it would be faster to call it directly, m.disc_var_pick(m). For comparison, here is an example with rand:

julia> using BenchmarkTools

julia> @btime rand()
  4.558 ns (0 allocations: 0 bytes)

julia> @btime eval(rand)()
  67.705 ns (1 allocation: 16 bytes)
harshangrjn commented 3 years ago

@julbinb Thanks for your helpful suggestions. I have updated the code and eliminated the usage of evals as much as possible. If you get a chance to review the recent release (v0.2.0) and see if this needs any more changes, let me know and I shall get them fixed. If not, will close the issue.

julbinb commented 3 years ago

@harshangrjn you are welcome! I will try to take a look over the next week.

julbinb commented 3 years ago

I see a couple of uses of eval that might or might not have a similar issue, e.g. here or here. If what convertor[..] contains is already a function (i.e. a value for which typeof returns Function), it would be unnecessary to call eval. But I didn't look at the code close enough to figure that out.

harshangrjn commented 3 years ago

@julbinb Thanks! Will look into it and make changes where necessary.

harshangrjn commented 3 years ago

Most of the unnecessary eval() functions have been removed in this update.