membrane_core icon indicating copy to clipboard operation
membrane_core copied to clipboard

Optimise access to element's state

Open daniel-jodlos opened this issue 4 years ago • 1 comments

Currently, a lot of frequently used parameters are very deeply nested. The goal is to optimise accessing them by flattening the physical structure of the state, while still maintaining the logical.

Having performed performance tests o read and write operations on various representations of both generated, deeply nested maps and the actual state, the following can be concluded:

  • Flattening of the structure brings the greatest improvement in both read and update operations, being about 10x faster than accessing current, deeply nested structure. However, that regards only accessing a single element. Rebuilding even parts of nested structure from it's flat representation will be costly.
  • Using Process dictionary allows for 25x - 30x faster updates over standard Elixir map
  • Using Map allows for about 10x faster reads than Process dictionary in the actual state and 5x faster on generated maps

Issue of large performance overheads of rebuilding parts of the structure could be partially resolved by using compile-time formulas eg. [pads: [:direction, :demand_unit]]. That strategy could potentially bring the time <total number of keys in rebuilt structure> x <time of accessing single value> which is still not great. Caching most commonly rebuilt parts might need to be revisited in the future.

daniel-jodlos avatar Jul 01 '21 10:07 daniel-jodlos

Using records instead of structs for keeping elements' state did not bring any performance gains (more info). Map flattening seems promising when you want to access single elements, but makes it costly to retrieve sub-maps from the top-level map - you need to iterate over all of its keys and take only those with specific prefix, which involves atom-to-string conversions and length_of_the_map comparisons. Process dictionaries were not tested yet.

balins avatar Aug 23 '21 13:08 balins