Determining what gets memoized

Hullo Hanakai folks,

I’m in the process of upgrading a large Hanami app to v3 - which means that all components are now memoized by default. I’m certainly keen to keep that behaviour for the performance benefits, so now I’m thinking through the best ways to verify my code is state-safe.

If there are general automated processes that anyone can suggest, that’d be great - mostly, I’m trying to focus on any class that has instance variables, and then get a sense of whether it’s code that gets kept between requests, and whether that state should be kept. But there’s a lot of files in this app, so this is slow and feels fragile.

I’ve also built up a list of components using this temporary code:

$components = []

config.no_memoize = ->(component) {
  $components << component
  false
}

… and there are classes in there which I wouldn’t expect to be kept between requests. For example: entities for database records, view contexts, view parts.

So this leads me to my main question: how can I better determine the subset of components that are kept between requests? Would it only be those referenced via Deps[]? And if so, how can I generate that subset?

And separately: I know there’s some usefulness to not memoizing in test environments - but being able to turn that memoization on when running tests is also useful for peace of mind. Would there be any interest in a PR to allow this?

The memoization in question is happening in the Dry::System container, you will have one container per slice (including app).

So, from a console run:

> app.boot
> app.container.keys

And that will be the list of memoized components. You can look up each key and inspect what it is based on the name.

Thanks @alassek - I’d forgotten about app.container.keys, that’s certainly nicer than my global variable hack.

However, it still contains structs and view parts and such - classes that I wouldn’t expect to be memoized across requests. I’m dealing with hundreds of classes here, hence my desire to figure out the appropriate subset of all components where memoization matters.

Looks like you’re on the right path here, @pat!

Looks like you’re at the point where you should be keeping certain things out of the container entirely. Structs and View Parts etc. shouldn’t even be registered in the first place. You have a couple of options for this at the Hanami level:

  • Adding # auto_register: false to specific files
  • Opting whole directories via config.no_auto_register_paths

By default, no_auto_register_paths contains ["db", "entities", "relations", "structs"]. These paths are always match from the start of the file path when the auto-registrar runs.

The default values should exclude many files in their conventional locations, but it looks like we should add "views/parts" to the defaults. In the meantime, you can do it in app code:

# in app or slice
config.no_auto_register_paths << "views/parts"

If there are other whole groups of files that are being auto-registered when they shouldn’t be, you could exclude them via this setting too. I’m also keen to hear if there are any other places we’ve missed in our defaults. Let us know how you go?

Thanks @timriley, that’s helpful context too.

I’m finding that in my slices, components are getting autoloaded nonetheless, because they’re within the lib directory, and that directory doesn’t get an auto_register proc assigned to it (looking at the prepare_app_component_dirs method for Hanami::Slice where lib is set up first, then root is set up afterwards - only root gets the auto_register proc logic.)

As I know you’re aware, I’m dealing with a pre-Hanami app nudged into Hanami proper, where almost all logic for a slice is within the slice’s lib directory. I suspect that’s not the way things are expected to be - is my suspicion correct? And if so: is there any guidance on what should live in lib versus what should live elsewhere?

In slices, I’d suggest just moving everything from [slice_path]/lib/ just out into [slice_path]/ directly. Slice autoloaders are configured to load from there and expect the matching Ruby namespace. Is that going to be too big a change? If it is, we can think about other options.

Thanks @timriley - made that shift of moving all slice files out of lib, pretty painless, and now those default no_auto_register_paths kick in. :slight_smile:

I’ll work through the remaining components a little more mindfully, see if there are other directories I can exclude, or single files I can add the pragma comment to.

I’ve also managed to run the test suite with memoization turned on (via some metaprogramming hacks), and beyond the handful of tests which are stubbing some components, everything else passes, so I’m taking that as a signal that things should be pretty safe. Still a little nervewracking to make that jump in production though!

Oh, another question around semantics:

This concept of a ‘component’ in Hanami - should that be anything that is available via Deps[] and (ideally) can be memoized? And so all other classes, however they’re used, are then not considered components?

Flowing on from that - should the concepts of auto-registration and memoization really only be considered as being for components?

(Just wanting to make sure I’m organising code and configuring things in ways that match the Hanami mindset)

Broadly, yes. The concept of component dirs comes from dry-system and it essentially refers to things that are auto-registered into the container and thus made available to Deps.

That’s not the totality of what you will find in the container. There are also possibly things that are manually registered via providers or the manifest registry (which I see does not have documentation).

Thanks @alassek, great to know :slight_smile: Appreciate both of your help with this!

Thanks for writing this thread up, it is a really useful checklist for anyone moving to v3 (moving everything out of slice lib/ was the bit I would have missed too).

One cheap trick I have used for the “is this object state-safe” question in other Ruby apps: in the test environment, freeze each component right after it is resolved from the container. Any code that later sets an instance variable (memoizing a value with ||=, caching the current user, etc.) then raises FrozenError the first time the test suite hits that path, so you get a concrete list of offenders instead of having to read through hundreds of classes. It will not catch mutation of nested objects like a hash held in an ivar, but it finds the common cases quickl

y.