Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve time complexity of IndexOf. #5800

Merged
merged 1 commit into from Nov 23, 2018

Conversation

jbialobr
Copy link
Member

The complexity was O(n), which is not acceptable for a method
that is used to compare elements in a sorting algorithm.
The new time complexity is O(1).

@ghost ghost assigned jbialobr Nov 21, 2018
@ghost ghost added the status: ready label Nov 21, 2018
@codecov
Copy link

codecov bot commented Nov 21, 2018

Codecov Report

Merging #5800 into master will increase coverage by 0.01%.
The diff coverage is 0%.

@@            Coverage Diff             @@
##           master    #5800      +/-   ##
==========================================
+ Coverage   36.94%   36.95%   +0.01%     
==========================================
  Files         620      620              
  Lines       47416    47416              
  Branches     6323     6321       -2     
==========================================
+ Hits        17518    17524       +6     
+ Misses      29115    29111       -4     
+ Partials      783      781       -2

GitUI/CommitInfo/CommitInfo.cs Show resolved Hide resolved
@@ -78,7 +78,7 @@ public partial class CommitInfo : GitModuleControl
private List<string> _branches;
private string _branchInfo;
private string _gitDescribeInfo;
[CanBeNull] private IList<string> _sortedRefs;
[CanBeNull] private IDictionary<string, int> _sortedRefs;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is no longer technically "sorted" maybe _indexByRefName is a better name for the field.

GitUI/CommitInfo/CommitInfo.cs Show resolved Hide resolved
var length = headLength + s.Length;

for (var i = 0; i < _otherList.Count; i++)
if (s.StartsWith("remotes/"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the user of this comparer persist the data in sanitised form so we don't have to allocate temporary strings here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. Could you try to convince me that it is worth to care about this? These are short-lived strings and AFAIK the GC deals pretty well with them. The gain in performance caused by avoiding string allocations is almost none if any at all (The old version of this method with string allocations seems to be faster - didn't do a benchmark, just compared freezes times when navigating through blame lines). The gain achieved by replacing List with Dictionary is significant, noticeable with a bare eye. There are many other places where optimization could be done and I would rather optimize them before doing so low level optimizations like avoiding allocations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbialobr I don't doubt there's already a gain, the implementation looks great. I'm just wary of comparers that allocate. I don't even know how this comparer is used, but if it was impacting perf due to algorithmic complexity, then presumably there were quite a few calls. Right now it's allocating temp strings for each comparison. If the inputs were all canonical (ref/ form or whatever) then you could eliminate the allocations and the StartsWith call.

The complexity was O(n), which is not acceptable for a method
that is used to compare elements in a sorting algorithm.
The new time complexity is O(1).
@jbialobr jbialobr merged commit f43f53e into gitextensions:master Nov 23, 2018
@ghost ghost removed the status: ready label Nov 23, 2018
@jbialobr jbialobr deleted the jb/sortTime branch November 23, 2018 04:48
@RussKie RussKie added this to the 3.0.1 milestone Nov 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants