O Zeitwerk é um carregador de código eficiente e seguro para threads em Ruby. Desde que o projeto siga as convenções de nomenclatura, o Zeitwerk consegue encontrar os arquivos corretos e carregá-los sob demanda ou antecipadamente, sem a necessidade de qualquer require ou require_dependency. Além disso, conforme mencionado naquele artigo https://weblog.rubyonrails.org/2019/2/22/zeitwerk-integration-in-rails-6-beta-2/, ele pode oferecer um pequeno aumento de desempenho para a aplicação.
Existem algumas etapas que preciso realizar para fazê-lo funcionar:
Alterar o nome de algumas classes para seguir a convenção de nomenclatura do Rails. Por exemplo, o arquivo canonical_url.rb deve definir a classe CanonicalUrl em vez de CanonicalURL. Da mesma forma, o arquivo ondiff.rb deve definir a classe Onpdiff em vez de ONPDiff. Uma abordagem alternativa seria conectar um inflector personalizado ao projeto; no entanto, acredito que seguir a convenção seja uma escolha melhor — GitHub - fxn/zeitwerk: Efficient and thread-safe code loader for Ruby · GitHub
Similar ao ponto anterior, por convenção, validações personalizadas localizadas no diretório validations devem ser envoltas pelo módulo Validations. Além disso, algumas validações herdam de EachValidator e devem ser acessíveis sem namespace. Planejo movê-las para um diretório separado e adicioná-las aos caminhos de carregamento automático.
Remover todos os require_dependency e garantir que o projeto esteja funcionando.
Remover todos os require e garantir que o Discourse esteja funcionando.
Garantir que todos os plugins possam acessar as dependências necessárias. Ainda não sei como alcançar isso. Quero primeiro fazer o Discourse funcionar sem nenhum plugin.
Claro, ainda há muitas incógnitas a resolver. Manterei você atualizado sobre o progresso; no entanto, se você tiver interesse em acompanhar, comecei a experimentar isso aqui: Commits · KrisKotlarek/discourse · GitHub
Por favor, me avise se você identificar alguma desvantagem na implementação do Zeitwerk ou se acha que deixei algo passar.
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.
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?
@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)