# Determining what gets memoized

**URL:** <https://discourse.hanakai.org/t/determining-what-gets-memoized/1566>\
**Category:** questions\
**Created:** [October 15, 2026, 2:54pm UTC](https://discourse.hanakai.org/t/determining-what-gets-memoized/1566 "2026-10-15T14:54:27Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/pat/32/691_2.png) [@pat](https://discourse.hanakai.org/u/pat)\
**Post date:** [October 15, 2026, 2:54pm UTC](https://discourse.hanakai.org/t/determining-what-gets-memoized/1566/1 "2026-10-15T14:54:27Z")

</div>

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:

```ruby
$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?

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/alassek/32/532_2.png) [@alassek](https://discourse.hanakai.org/u/alassek)\
**Post date:** [October 15, 2026, 3:48pm UTC](https://discourse.hanakai.org/t/determining-what-gets-memoized/1566/2 "2026-10-15T15:48:30Z")

</div>

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:

```ruby
> 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.

---

<div class="post-metadata">

**Author:** ![pat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/pat/32/691_2.png) [@pat](https://discourse.hanakai.org/u/pat)\
**Post date:** [October 15, 2026, 10:16pm UTC](https://discourse.hanakai.org/t/determining-what-gets-memoized/1566/3 "2026-10-15T22:16:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![timriley](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/timriley/32/621_2.png) [@timriley](https://discourse.hanakai.org/u/timriley)\
**Post date:** [October 15, 2026, 11:11pm UTC](https://discourse.hanakai.org/t/determining-what-gets-memoized/1566/4 "2026-10-15T23:11:08Z")

</div>

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](https://hanakai.org/learn/hanami/v3.0/app/container-and-components#opting-out-of-the-container) 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:

```ruby
# 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?

---

<div class="post-metadata">

**Author:** ![pat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/pat/32/691_2.png) [@pat](https://discourse.hanakai.org/u/pat)\
**Post date:** [October 16, 2026, 12:01am UTC](https://discourse.hanakai.org/t/determining-what-gets-memoized/1566/5 "2026-10-16T00:01:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![timriley](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/timriley/32/621_2.png) [@timriley](https://discourse.hanakai.org/u/timriley)\
**Post date:** [October 16, 2026, 12:02am UTC](https://discourse.hanakai.org/t/determining-what-gets-memoized/1566/6 "2026-10-16T00:02:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/pat/32/691_2.png) [@pat](https://discourse.hanakai.org/u/pat)\
**Post date:** [October 16, 2026, 3:16am UTC](https://discourse.hanakai.org/t/determining-what-gets-memoized/1566/7 "2026-10-16T03:16:21Z")

</div>

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. 🙂

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!

---

<div class="post-metadata">

**Author:** ![pat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/pat/32/691_2.png) [@pat](https://discourse.hanakai.org/u/pat)\
**Post date:** [October 16, 2026, 3:30am UTC](https://discourse.hanakai.org/t/determining-what-gets-memoized/1566/8 "2026-10-16T03:30:10Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/alassek/32/532_2.png) [@alassek](https://discourse.hanakai.org/u/alassek)\
**Post date:** [October 16, 2026, 4:20am UTC](https://discourse.hanakai.org/t/determining-what-gets-memoized/1566/9 "2026-10-16T04:20:36Z")

</div>

Broadly, yes. The concept of component dirs [comes from dry-system](https://hanakai.org/learn/dry/dry-system/v1.2/component-dirs) 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](https://hanakai.org/learn/dry/dry-system/v1.2/providers) or the manifest registry (which I see does not have documentation).

---

<div class="post-metadata">

**Author:** ![pat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/pat/32/691_2.png) [@pat](https://discourse.hanakai.org/u/pat)\
**Post date:** [October 16, 2026, 4:26am UTC](https://discourse.hanakai.org/t/determining-what-gets-memoized/1566/10 "2026-10-16T04:26:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![colorfulcms](https://avatars.discourse-cdn.com/v4/letter/c/8491ac/32.png) [@colorfulcms](https://discourse.hanakai.org/u/colorfulcms)\
**Post date:** [October 27, 2026, 2:44pm UTC](https://discourse.hanakai.org/t/determining-what-gets-memoized/1566/11 "2026-10-27T14:44:13Z")

</div>

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.
