On Fri, Oct 8, 2010 at 3:06 AM, Aliaksandr Radzivanovich<aradzivanovich@xxxxxxxxx> wrote:I was browsing through JGit source code and noticed that it strives to
be fast and efficient, preserving every byte where possible. I
appreciate this approach and I think it could be optimized even
further. So I want to share and discuss my idea with the community.
I've thought about doing this myself over the years. The reason Ihaven't is that it may be more expensive to do the instanceof testwhen checking parent count, or accessing a parent, and there are manyplaces where we avoided using the accessor methods and just directlyaccessed the field.
Using a visitor pattern for processing the parents coupled with using a different subclass (SingleParentRevCommit vs MultiParentRevCommit) would avoid the instanceof check.
// Single visit(RevCommitVisitor v) { v.process(parent); // nice and JIT able }
// Multi visit(RevCommitVisitor v) { for(int p=0;p<parents.length;p++) v.process(parents[p]) }
These classes are probably preparing a new array (or maybe even edit it in place if its the same size). We'd need a package level setParents() method on RevCommit to permit the parents to be modified and have them be stored correctly (as a single RevCommit if there is only one, or as an array if there is more than one).
Could you also create a new instance instead of mutating an existing one? In most cases you may not need to mutate and could simply return it unmolested.
Alex |