Zeitwerk est un chargeur de code efficace et sûr pour les threads en Ruby. Tant que le projet respecte les conventions de nommage, Zeitwerk peut trouver les fichiers corrects et les charger à la demande ou immédiatement, sans avoir besoin d’utiliser require ou require_dependency. De plus, cela peut offrir un léger gain de performance à l’application, selon cet article : https://weblog.rubyonrails.org/2019/2/22/zeitwerk-integration-in-rails-6-beta-2/
Voici quelques étapes que je dois effectuer pour que cela fonctionne :
Changer le nom de quelques classes pour respecter la convention de nommage de Rails. Par exemple, le fichier canonical_url.rb devrait définir la classe CanonicalUrl au lieu de CanonicalURL. De même, le fichier ondiff.rb devrait définir la classe Onpdiff au lieu de ONPDiff. Une approche alternative consisterait à attacher un inflector personnalisé au projet, mais je pense que respecter la convention est un meilleur choix : GitHub - fxn/zeitwerk: Efficient and thread-safe code loader for Ruby · GitHub
De même que pour le point précédent, par convention, les validations personnalisées situées dans le répertoire validations doivent être enveloppées dans le module Validations. Par ailleurs, certaines validations héritent de EachValidator et doivent être accessibles sans espace de noms. Je prévois de les déplacer dans un répertoire séparé et de les ajouter aux chemins d’autoloading.
Supprimer tous les require_dependency et s’assurer que le projet fonctionne.
Supprimer tous les require et s’assurer que Discourse fonctionne.
S’assurer que tous les plugins peuvent accéder aux dépendances requises. Je ne sais pas encore comment y parvenir. Je souhaite d’abord faire fonctionner Discourse sans aucun plugin.
Bien sûr, il reste encore de nombreuses inconnues à résoudre. Je vous tiendrai informé de l’avancement, mais si vous souhaitez jeter un coup d’œil, j’ai commencé à expérimenter cela ici : Commits · KrisKotlarek/discourse · GitHub
Merci de me faire part de tout inconvénient que vous voyez à l’implémentation de Zeitwerk ou si vous pensez que j’ai oublié quelque chose.
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)