Upgrading Discourse to Zeitwerk

Rails 6 ships with two autoloading modes: zeitwerk and classic. In that pull request https://github.com/discourse/discourse/pull/8083 I upgraded Rails to version 6.0.0 with classic autoloader as a transitional phase. It would be interesting to try to switch to Zeitwerk.

Zeitwerk is an efficient and thread-safe code loader for Ruby. As long as the project is following naming conventions, Zeitwerk can find correct files and load them on demand or upfront without the need for any require or require_dependency. Also, it may give a small performance boost to the application according to that article Zeitwerk integration in Rails 6 (Beta 2) | Riding Rails

There are a few steps I need to do to make it work

  1. Change the name of few classes to follow Rails naming convention. For example file, canonical_url.rb should define CanonicalUrl class instead of CanonicalURL. Similarly file ondiff.rb should define Onpdiff class instead of ONPDiff. An alternative approach would be to hook custom inflector to the project, however, I think that following convention might be a better choice - https://github.com/fxn/zeitwerk#custom-inflector

  2. Similar to the previous point, by convention custom validations living in validations directory should be wrapped with Validations module. Besides, some validations are inheriting from EachValidator and should be accessible without namespace. I am planning to move them to separate directory and add to autoload paths.

  3. Remove all require_dependency and ensure that the project is working

  4. Remove all require and ensure that Discourse is working

  5. Ensure that all plugins can access the required dependencies. I don’t know how to achieve it yet. I want to firstly make Discourse running without any plugins.

Of course, there are still plenty of unknowns to solve. I will keep you updated about progress however if you are keen to look, I started playing with that here https://github.com/lis2/discourse/commits/zeitwerk

Please let me know if you see any drawbacks of implementing Zeitwerk or you think I missed something.

14 Likes

I made some progress around Zeitwerk, however, I changed my approach. My original plan was to change every place where Discourse is not following the Zeitwerk filename convention. After a few fixes, I realised that this is just the tip of the iceberg and noticed that if I am going to follow that path, then pull request will be difficult to read and confidently merge to master. For example, all job classes under regular directory should have Regular namespace, same with Onceoff and Scheduled.

I decided to step back a little bit and think about a more evolutionary approach than revolutionary.

I decided that it would be better to introduce custom Inflector, which will cover all files which are not following Zeitwerk convention. The biggest benefit will be that we would be able to deploy that small change and once we are happy with Zeitwerk and we don’t have any performance downgrades, we can start fixing convention file by file in reasonable small pull requests.

I found some problems which could not be sorted by custom Inflector, so I made additional fixes to make it work.

Pull request is still in progress, however, at this stage, I can run Discourse with Zeitwerk and default plugins, run all specs and run benchmark without a problem.

I wanted first to be at that stable state when all specs are passing. Now I can confidently start removing all require_dependency one by one and also test official plugins. Once everything is ready, I will share with you benchmark results in this post.

For now, if you are interested in progress you may take a look on that draft PR - https://github.com/discourse/discourse/pull/8098

The most important file is that custom Zeitwerk Inflector - https://github.com/discourse/discourse/pull/8098/files#diff-1d11f8dea7245fa4921be68dbc40a1d1

10 Likes

To make plugins work I needed to create a few more small pull requests. Once they are merged I think that specs on Discourse should pass.

I also checked performance on Rails 6.0.0 with Classic autoloader and Rails 6.0.0 with Zeitwerk.

Test Classic Zeitwerk Percent
categories-50 32 26 81.25
categories-75 37 29 78.38
categories-90 47 35 74.47
categories-99 67 49 73.13
home-50 30 29 96.67
home-75 37 31 83.78
home-90 44 40 90.91
home-99 67 52 77.61
topic-50 35 35 100.00
topic-75 36 36 100.00
topic-90 48 36 75.00
topic-99 57 58 101.75
categories_admin-50 51 48 94.12
categories_admin-75 62 50 80.65
categories_admin-90 89 66 74.16
categories_admin-99 135 101 74.81
home_admin-50 48 47 97.92
home_admin-75 58 49 84.48
home_admin-90 67 64 95.52
home_admin-99 101 81 80.20
topic_admin-50 48 48 100.00
topic_admin-75 55 49 89.09
topic_admin-90 63 65 103.17
topic_admin-99 92 69 75.00
load_rails 2617 2165 82.73
rss_kb 282428 315684 111.78
pss_kb 270491 303504 112.20

Results are not always consistent so I would take them with a grain of salt.

5 Likes

The amount of inconsistency on the median here is a bit odd, I wonder why results are fluctuating so much

Surprised loader would have any impact

4 Likes

I tried it once again, here are results

Test Classic Zeitwerk Percent
categories-50 25 25 100.00
categories-75 26 26 100.00
categories-90 37 33 89.19
categories-99 57 48 84.21
home-50 26 26 100.00
home-75 27 28 103.70
home-90 38 35 92.11
home-99 60 50 83.33
topic-50 27 26 96.30
topic-75 35 27 77.14
topic-90 41 33 80.49
topic-99 54 50 92.59
categories_admin-50 48 50 104.17
categories_admin-75 60 61 101.67
categories_admin-90 76 71 93.42
categories_admin-99 122 122 100.00
home_admin-50 47 46 97.87
home_admin-75 58 55 94.83
home_admin-90 66 63 95.45
home_admin-99 99 121 122.22
topic_admin-50 50 49 98.00
topic_admin-75 62 50 80.65
topic_admin-90 72 65 90.28
topic_admin-99 103 74 71.84
load_rails 2675 2216 82.84
rss_kb 279924 315240 112.62
pss_kb 267659 303026 113.21

We can try another way to benchmark. What would you say about more iterations, something which would run for an hour? In addition, instead of taking the best result, compare the average from each experiment. That may give more consistent numbers. What do you think?

1 Like

In my pull requests for plugins, you will notice that a lot of fixes are around searching in the global namespace.

I changed code like

module ::Jobs
  class TranslatorMigrateToAzurePortal < Jobs::Onceoff

to

module ::Jobs
  class TranslatorMigrateToAzurePortal < ::Jobs::Onceoff

One thing bothered me about that solution, why was it working before Zeitwerk. That question when something works but shouldn’t is always tricky :slight_smile:

I think I found a potential answer in the description of the classic autoloader (Autoloading and Reloading Constants (Classic Mode) — Ruby on Rails Guides) - “If not found, then the algorithm walks up the ancestor chain of the cref”

Zeitwerk is more strict. Once I tried to load code before fix it was complaining that Jobs::Jobs::Onceoff can not be found.

Sam suggested under pull request https://github.com/discourse/discourse-prometheus-alert-receiver/commit/ef9c238a6fabc3a9ca238664adb3dc594dc709ee, that instead of using < ::Jobs::Onceoff we can just use < Onceoff and he is right. I checked that without namespace it works as well. I am thinking that giving :: is explicitly saying that we are inheriting from Discourse Core class, however, we can go either way.

4 Likes

I think that when the code is this close together, this reads great:

module ::Jobs
  class TranslatorMigrateToAzurePortal < Onceoff

If the code starts floating down being explicit makes more sense … eg:

module ::Jobs
  [ 50 lines omitted]
  class TranslatorMigrateToAzurePortal < ::Jobs::Onceoff

That said, I am on a fence here, so I am fine either way. ::Jobs::Onceoff is short enough and mega explicit so we can just go with that for now.

4 Likes

I would love to start getting this merged. @kris.kotlarek is this now in a merge-able state? Timing is quite good cause we just had a beta

https://github.com/discourse/discourse/pull/8098

5 Likes

Let me rebase recent master into that and ensure that it still works, I will do that today

6 Likes

@sam I think we are good to go, I rebased with the latest master and made a small adjustment to Webauthn.
I checked 3 things:
run the server on local and click a little bit to ensure it works as expected
run specs
downloaded all official plugins and ensured that specs for plugins are passing (we need to merge adjustments for plugins first)

5 Likes

Cool I am merging this. Let’s see what shakes out!

4 Likes

Can we merge fixes for plugins as well? Otherwise, if you try to run Discourse with plugins it will fail

https://github.com/discourse/discourse-translator/pull/25
https://github.com/discourse/discourse-canned-replies/pull/51
https://github.com/discourse/discourse-cakeday/pull/29
https://github.com/discourse/discourse-voting/pull/45
https://github.com/discourse/discourse-perspective-api/pull/4
https://github.com/discourse/discourse-calendar/pull/4
https://github.com/discourse/discourse-prometheus/pull/4

4 Likes

Please go ahead and merge away!

3 Likes

This is now merged! If any plugin authors are struggling here, let us know in a new dedicated topic!

Great work Kris!!!

10 Likes