symforce icon indicating copy to clipboard operation
symforce copied to clipboard

Add `output_symbols` arg to codegen as performance mitigation

Open gareth-cross opened this issue 2 years ago • 0 comments

For complex expressions, the calculation of Codegen.output_symbols can grow quite large (encroaching on a minute or more). In some cases, the value of output_symbols can be specified externally. For example:

  • The user may have a list of output_symbols readily available at the callsite (frequently it matches the input symbol set exactly).
  • The output_symbols can be determined from another Codegen object, for example in most (though not all) cases the following can work:
cg = Codegen(...)
cg_factor = cg.with_linearization(output_symbols=cg.output_symbols) # avoid recomputation

Ideally free_symbols would be faster, but this is a mitigation/workaround that can offer worthwhile savings in some cases.

This change adds an optional argument to the various constructors of Codegen so that the user can optionally provide the symbols themselves, and avoid an expensive computation.

gareth-cross avatar Aug 17 '23 04:08 gareth-cross