hanami-controller/benchmarks
Tim Riley 2ca9af9bbb
Reduce memory usage in Hanami::Action via dry-configurable and custom config_class (#392)
Take advantage of the recent memory usage improvements in dry-configurable (see https://github.com/dry-rb/dry-configurable/pull/138) by converting the previously custom `Configuration` class into settings defined on the `Hanam::Action` class.

To preserve some of the existing custom configuration methods, provide a custom `Hanami::Action::Config` config class to `Dry::Configurable` and keep those methods there.

This custom config class also comes in handy as the place to keep the documentation for our settings, which I've converted into yard `@!attribute` macros for concision.

We also move two existing bits of state into new dry-configurable settings:

- Convert `before_callbacks` and `after_callbacks` from class attributes, since Hanami-utils' class attributes implementation is very inefficient with memory.
- Move `accepted_formats` from being an class-level ivar, since this was the only remaining class-level state and it made sense to keep all this together via the class-level config object.

As a consequence of the last change, we're also able to change `Hanami::Action` so it has a conventional `#initialize` method again, rather than having a custom `.new` method that has to `allocate` the object and assign some ivars directly, which is a big win for ease of understanding the code :)

Lastly, while we're here, we get rid of the @name ivar for actions, since it's no longer used anywhere.

Using the newly added `benchmarks/memory_profile_action.rb` (which creates 100 `Action` subclasses by default) we can see this branch allocates 0.338 MB of memory, versus 1.859 MB for the main branch.
2022-10-07 12:39:04 +11:00
..
memory_profile_action.rb Reduce memory usage in Hanami::Action via dry-configurable and custom config_class (#392) 2022-10-07 12:39:04 +11:00