# Directly modify compiled method without recompilation

**URL:** <https://discourse.julialang.org/t/directly-modify-compiled-method-without-recompilation/86564>\
**Category:** Performance\
**Tags:** compilation\
**Created:** [August 30, 2022, 5:45pm UTC](https://discourse.julialang.org/t/directly-modify-compiled-method-without-recompilation/86564 "2022-08-30T17:45:16Z")\
**Posts on this page:** 1\
**Showing post:** 20

<div class="post-metadata">

**Author:** ![FireCrumb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/firecrumb/32/35370_2.png) [@FireCrumb](https://discourse.julialang.org/u/FireCrumb)\
**Post date:** [August 31, 2022, 7:33pm UTC](https://discourse.julialang.org/t/directly-modify-compiled-method-without-recompilation/86564/20 "2022-08-31T19:33:49Z")

</div>

Here’s a longer but hopefully more representative example:

```julia
using RuntimeGeneratedFunctions

RuntimeGeneratedFunctions.init(@ __MODULE__ )

function get_calc(mysetup::Expr, myalgo::Expr)::Expr
    return :(
        function calc(myinput::Float32)::Float32
            # input always in v1
            v1 = myinput
            
            # init variables
            $mysetup

            # run calculation
            $myalgo

            # result always in v5
            res = v5
            return res
        end
    )
end

get_calc_func(calc_body) =
    @RuntimeGeneratedFunction(@ __MODULE__ , calc_body, opaque_closures = false)

function find_calc()
    bestres = typemin(Float32)
    bestalgo::Expr = :()

    mysetup = Vector{Expr}()
    for _ in 1:10
        expr_list = Vector{Expr}()
        # without v1, which is input
        for myvar in [:v2, :v3, :v4]
            initval = rand(Float32)
            ex = :($myvar = $initval)
            push!(expr_list, ex)
        end
        # v5 is output
        push!(expr_list, :(v5 = 0.0))
        initvar_expr = quote end
        initvar_expr.args = expr_list
        push!(mysetup, initvar_expr)
    end

    myalgo = Vector{Expr}()
    for _ in 1:100
        algo_inst_list = Vector{Expr}()

        algo_len = rand(3:10)

        for _ in 1:algo_len
            x = rand([:v1, :v2, :v3, :v4, :v5])
            y = rand([:v1, :v2, :v3, :v4, :v5])
            z = rand([:v1, :v2, :v3, :v4, :v5])
            op = rand([:-, :+, :*, :/])
            push!(algo_inst_list, :($x = $op($y, $z)))
        end
        algo_expr = quote end
        algo_expr.args = algo_inst_list
        push!(myalgo, algo_expr)
    end

    for setup_expr in mysetup
        for algo_expr in myalgo
            calc_body = get_calc(setup_expr, algo_expr)
            mycalc_expr = get_calc_func(calc_body)
            for myinput in range(-10, 10, step=50)
                res = mycalc_expr(myinput)
                if res > bestres
                    bestres = res
                    bestalgo = calc_body
                end
            end
        end
    end

    return bestalgo, bestres
end

@time @info find_calc()

```

---

_[View the full topic](https://discourse.julialang.org/t/directly-modify-compiled-method-without-recompilation/86564)._
