Rails 6 默认提供两种自动加载模式:zeitwerk 和 classic。在该拉取请求 DEV: Upgrading Discourse to Rails 6 by KrisKotlarek · Pull Request #8083 · discourse/discourse · GitHub 中,我将 Rails 升级到了 6.0.0 版本,并暂时使用 classic 自动加载器作为过渡方案。尝试切换到 Zeitwerk 会很有意思。
Zeitwerk 是一个高效且线程安全的 Ruby 代码加载器。只要项目遵循命名规范,Zeitwerk 就能找到正确的文件并按需或预先加载它们,而无需任何 require 或 require_dependency。此外,根据这篇文章 https://weblog.rubyonrails.org/2019/2/22/zeitwerk-integration-in-rails-6-beta-2/,它还可能为应用程序带来轻微的性能提升。
为了实现这一目标,我需要完成以下几个步骤:
修改部分类的名称以符合 Rails 命名规范。例如,文件 canonical_url.rb 应定义 CanonicalUrl 类,而不是 CanonicalURL。同样,文件 ondiff.rb 应定义 Onpdiff 类,而不是 ONPDiff。另一种方法是为项目挂载自定义推断器(inflector),但我认为遵循规范可能是更好的选择 - GitHub - fxn/zeitwerk: Efficient and thread-safe code loader for Ruby · GitHub
与上一点类似,按照规范,位于 validations 目录中的自定义验证应包裹在 Validations 模块中。此外,某些验证继承自 EachValidator,应无需命名空间即可访问。我计划将它们移至单独的目录,并添加到自动加载路径中。
移除所有 require_dependency,并确保项目正常运行。
移除所有 require,并确保 Discourse 正常运行。
确保所有插件都能访问所需的依赖项。目前我还不知道如何实现这一点。我打算先让 Discourse 在不加载任何插件的情况下运行。
当然,仍有许多未知问题需要解决。我会持续向您汇报进展。如果您有兴趣查看,我在此处开始尝试:Commits · KrisKotlarek/discourse · GitHub
如果您发现实施 Zeitwerk 存在任何弊端,或认为我遗漏了某些内容,请随时告知。
14 个赞
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 - DEV: Upgrading Discourse to Zeitwerk by KrisKotlarek · Pull Request #8098 · discourse/discourse · GitHub
The most important file is that custom Zeitwerk Inflector - DEV: Upgrading Discourse to Zeitwerk by KrisKotlarek · Pull Request #8098 · discourse/discourse · GitHub
10 个赞
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 个赞
sam
(Sam Saffron)
2019 年9 月 15 日 04:54
4
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 个赞
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 个赞
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
I think I found a potential answer in the description of the classic autoloader (https://guides.rubyonrails.org/autoloading_and_reloading_constants_classic_mode.html#resolution-algorithms ) - “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 FIX: Use top-level namespace for base classes · discourse/discourse-prometheus-alert-receiver@ef9c238 · GitHub , 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 个赞
sam
(Sam Saffron)
2019 年9 月 17 日 06:15
7
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 个赞
sam
(Sam Saffron)
2019 年10 月 2 日 00:59
8
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
master ← KrisKotlarek:zeitwerk-inflector
merged 04:01AM - 02 Oct 19 UTC
Update from Classic to Zeitwerk autoloader.
**This pull request is a little … bit risky, so please don't merge in a super busy period.**
Could you review if that looks fine?
Before merging that, I think that it would be good to merge fixes for plugins:
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
What can go wrong?
- If I missed any `::` in front `Jobs::Onceoff`, `Jobs::Base` or `Jobs::Scheduled` it may crash
- I changed Demon::Base to Demon::DemonBase, so any third party plugin relaying on that class needs to be updated
- If I miss any typos like here: https://github.com/discourse/discourse/pull/8098/commits/1050ab0440dbad1f1f0d77e8ea8558ab9b552040
Details about approach are documented here: https://meta.discourse.org/t/upgrading-discourse-to-zeitwerk/128337
5 个赞
Let me rebase recent master into that and ensure that it still works, I will do that today
6 个赞
@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 个赞
sam
(Sam Saffron)
2019 年10 月 2 日 04:00
12
Cool I am merging this. Let’s see what shakes out!
4 个赞
Can we merge fixes for plugins as well? Otherwise, if you try to run Discourse with plugins it will fail
master ← KrisKotlarek:zeitwerk
merged 04:31AM - 02 Oct 19 UTC
Zeitwerk is failing when we are searching for Jobs::Onceoff and Jobs::Base witho… ut going back to the top-level namespace (this is correlated to the fact that we got Onceoff base class and module with the same name).
I noticed that in plugins we are using sometimes :: and sometimes not and that gives me comfort that this change will not break anything.
Travis error: https://travis-ci.org/discourse/discourse/jobs/585072364
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 个赞
sam
(Sam Saffron)
2019 年10 月 2 日 04:29
14
Please go ahead and merge away!
3 个赞
sam
(Sam Saffron)
2019 年10 月 2 日 05:19
16
This is now merged! If any plugin authors are struggling here, let us know in a new dedicated topic!
Great work Kris!!!
10 个赞