Symbolics.jl: How to preevaluate a computationally expensive expression in build_function()

I like the build_function() method of Symbolics.jl which basically bakes symbolic expressions into compiled code.
However, it does so by compiling the full expression which might be numerically highly inefficient.
Suppose I have a symbolic expression that depends on some computationally expensive function f(x). In the symbolic expression, denoted by g(x, f(x)), f(x) is called multiple times.
Here is my question: Is there a way to preevaluate the value of f(x) once and then only reference to it by using build_function()?
Right now a workaround is to define

@variables x y
g_compiled = build_function(g(x, y), x, y)[1]

and then create a function that does the substitution:
g_final(x) = g_compiled(x, f(x))
This way, f(x) is only computed once and then accessed multiple times as desired. I however find this way of writing this a bit cumbersome. Is there a better way (e.g by registering a function to avoid the reduction to primitives?)

You can try setting cse=true in build_function. I’m not sure if that ever was completed though? @shashi @YingboMa

This is unfortunately not well documented. As far as I can see cse=true just unwraps the expression.

Indeed it’s underdocumented. @shashi let’s chat about this on Thursday.

I think it works with setting cse=true. It would be super nice to add that in the documentation. Is there a reason why this isn’t the default option? I think, this could speed things up a lot if the function is properly declared.

I don’t know, hence the discussion to be had.

Yeah, cse is mostly broken atm, I have opened a few issues with examples that could use some improvement