Merge request not loading due to Gitaly calls and highlighting

@dimitrieh noticed that https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/14609 did not load. It looks like it times out after 60 s. We added it to the profile list in gitlab-com/gitlab-profiler!38 (merged). The profiler results:

Under the profiler, you can see the problem really is not really due to SQL, although the 1 SQL load times could probably be optimized:

I, [2018-03-06T23:52:58.784265 #23621]  INFO -- : Completed 200 OK in 193620ms (Views: 179060.6ms | ActiveRecord: 841.4ms | Elasticsearch: 0.0ms)

At least 20% of the load times can be attributed to Gitaly. For example, one issue here is that I see that Repository#empty? is not being memoized:

image

Another issue is that we call find_merge_base repeatedly:

image

However, 25% of the load time is due to highlighting diffs:

image

/cc: @jacobvosmaer-gitlab, @smcgivern

Edited by Stan Hu