Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jgit-dev] Making RevCommit object representation more compact in memory

On 8 Oct 2010, at 18:19, Shawn Pearce <spearce@xxxxxxxxxxx> wrote:

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 I
haven't is that it may be more expensive to do the instanceof test
when checking parent count, or accessing a parent, and there are many
places where we avoided using the accessor methods and just directly
accessed 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

Back to the top