lib/api/services.rb retains a lot of memory and objects
After reading https://samsaffron.com/archive/2018/02/16/reducing-string-duplication-in-ruby, I tried to profile GitLab and found out that lib/api/services.rb
retained some memory and objects:
retained memory by file
-----------------------------------
470252 /Users/remy/Code/GitLab/gdk/gitlab/lib/api/services.rb
retained memory by location
-----------------------------------
363776 /Users/remy/Code/GitLab/gdk/gitlab/lib/api/services.rb:771
retained objects by file
-----------------------------------
3107 /Users/remy/Code/GitLab/gdk/gitlab/lib/api/services.rb
19634 "-"
1568 /Users/remy/Code/GitLab/gdk/gitlab/lib/api/services.rb:766
17456 "_"
1568 /Users/remy/Code/GitLab/gdk/gitlab/lib/api/services.rb:766
By adding # frozen_string_literal: true
we can get rid of the last two issues.
Then I discovered that we loop through 28 services to define their individual endpoints in https://gitlab.com/gitlab-org/gitlab-ce/blob/e0195f3369880d3a0b3c5c7e7e5bdee26b785c4a/lib/api/services.rb#L759, and in that loop we're looping again through all 28 services classes in https://gitlab.com/gitlab-org/gitlab-ce/blob/e0195f3369880d3a0b3c5c7e7e5bdee26b785c4a/lib/api/services.rb#L762. That result in a 28x28 = 784 iterations where all we need is 28 iterations to add the even_names
for each service, and then 28 iterations to create individual endpoints.
After these improvements, lib/api/services.rb
doesn't come up in the memory profiling summary anymore.