Gitaly N+1 queries with submodules in Projects::CommitController

As seen in #mech_symp_alerts (https://log.gitlab.net/goto/b0fd47e705f98b56458db83700bf6a9a), if a commit has lots of version bumps to submodules, Projects::CommitController can produce thousands of N+1 queries to TreeEntry:

image

[
  "lib/gitlab/gitaly_client.rb:145:in `call'",
  "lib/gitlab/gitaly_client/commit_service.rb:95:in `tree_entry'",
  "lib/gitlab/git/repository.rb:1095:in `gitaly_submodule_url_for'",
  "lib/gitlab/git/repository.rb:488:in `block in submodule_url_for'",
  "lib/gitlab/git/wraps_gitaly_errors.rb:7:in `wrapped_gitaly_errors'",
  "lib/gitlab/git/repository.rb:487:in `submodule_url_for'",
  "app/models/repository.rb:489:in `method_missing'",
  "app/helpers/submodule_helper.rb:10:in `submodule_links'",
  "app/helpers/diff_helper.rb:101:in `submodule_link'",
  "app/views/projects/diffs/_file_header.html.haml:12:in `_app_views_projects_diffs__file_header_html_haml___1690935054858579510_70011605157740'",
  "app/views/projects/diffs/_file.html.haml:10:in `_app_views_projects_diffs__file_html_haml__567988202267863892_70011601886380'",
  "app/views/projects/diffs/_diffs.html.haml:28:in `_app_views_projects_diffs__diffs_html_haml__3046961158008839607_70011595881020'",
  "app/views/projects/commit/show.html.haml:14:in `_app_views_projects_commit_show_html_haml___232866092404321608_70011581975080'",
  "app/controllers/application_controller.rb:121:in `render'",
  "app/controllers/projects/commit_controller.rb:29:in `block (2 levels) in show'",
  "app/controllers/projects/commit_controller.rb:27:in `show'",
  "ee/lib/gitlab/ip_address_state.rb:10:in `with'",
  "ee/app/controllers/ee/application_controller.rb:43:in `set_current_ip_address'",
  "lib/gitlab/session.rb:11:in `with_session'",
  "app/controllers/application_controller.rb:467:in `set_session_storage'",
  "app/controllers/application_controller.rb:461:in `set_locale'",
  "lib/gitlab/application_context.rb:10:in `with_context'",
  "app/controllers/application_controller.rb:453:in `set_current_context'",
  "lib/gitlab/error_tracking.rb:34:in `with_context'",
  "app/controllers/application_controller.rb:545:in `sentry_context'",
  "ee/lib/omni_auth/strategies/group_saml.rb:41:in `other_phase'",
  "ee/lib/gitlab/jira/middleware.rb:19:in `call'"
]

image