Skip to content

Commit

Permalink
Use structs for graph lines
Browse files Browse the repository at this point in the history
We used to use NSObjects, which was really expensive, because for each commit
as many as 100 graphlines can be created. By preallocating them and not
using NSObjects in general, we gain a massive speedup
  • Loading branch information
pieter committed Nov 24, 2008
1 parent 6aa2894 commit e570c3e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 42 deletions.
6 changes: 0 additions & 6 deletions GitX.xcodeproj/project.pbxproj
Expand Up @@ -46,7 +46,6 @@
F565262B0E03D89B00F03B52 /* PBWebHistoryController.m in Sources */ = {isa = PBXBuildFile; fileRef = F565262A0E03D89B00F03B52 /* PBWebHistoryController.m */; };
F56ADDD90ED19F9E002AC78F /* AddBranchTemplate.png in Resources */ = {isa = PBXBuildFile; fileRef = F56ADDD70ED19F9E002AC78F /* AddBranchTemplate.png */; };
F56ADDDA0ED19F9E002AC78F /* AddLabelTemplate.png in Resources */ = {isa = PBXBuildFile; fileRef = F56ADDD80ED19F9E002AC78F /* AddLabelTemplate.png */; };
F56CC7290E65E0AD004307B4 /* PBGitGraphLine.m in Sources */ = {isa = PBXBuildFile; fileRef = F56CC7280E65E0AD004307B4 /* PBGitGraphLine.m */; };
F56CC7320E65E0E5004307B4 /* PBGraphCellInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = F56CC7310E65E0E5004307B4 /* PBGraphCellInfo.m */; };
F57240BB0E9678EA00D8EE66 /* deleted_file.png in Resources */ = {isa = PBXBuildFile; fileRef = F57240BA0E9678EA00D8EE66 /* deleted_file.png */; };
F574A2850EAE2EAC003F2CB1 /* PBRefController.m in Sources */ = {isa = PBXBuildFile; fileRef = F574A2840EAE2EAC003F2CB1 /* PBRefController.m */; };
Expand All @@ -65,7 +64,6 @@
F5886A1E0ED5D3880066E74C /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; };
F5886A260ED5D4870066E74C /* PBGitGrapher.mm in Sources */ = {isa = PBXBuildFile; fileRef = F5FF4E790E082E440006317A /* PBGitGrapher.mm */; };
F5886A270ED5D4870066E74C /* PBGitRevisionCell.m in Sources */ = {isa = PBXBuildFile; fileRef = F50FE0E20E07BE9600854FCD /* PBGitRevisionCell.m */; };
F5886A280ED5D4870066E74C /* PBGitGraphLine.m in Sources */ = {isa = PBXBuildFile; fileRef = F56CC7280E65E0AD004307B4 /* PBGitGraphLine.m */; };
F5886A290ED5D4870066E74C /* PBGraphCellInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = F56CC7310E65E0E5004307B4 /* PBGraphCellInfo.m */; };
F5886A2A0ED5D4870066E74C /* PBGitLane.mm in Sources */ = {isa = PBXBuildFile; fileRef = F5C6F68C0E65FF9300478D97 /* PBGitLane.mm */; };
F5886A310ED5D54C0066E74C /* PBGitConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 93FCCBA80EA8AF450061B02B /* PBGitConfig.m */; };
Expand Down Expand Up @@ -190,7 +188,6 @@
F56ADDD70ED19F9E002AC78F /* AddBranchTemplate.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = AddBranchTemplate.png; path = Images/AddBranchTemplate.png; sourceTree = "<group>"; };
F56ADDD80ED19F9E002AC78F /* AddLabelTemplate.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = AddLabelTemplate.png; path = Images/AddLabelTemplate.png; sourceTree = "<group>"; };
F56CC7270E65E0AD004307B4 /* PBGitGraphLine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitGraphLine.h; sourceTree = "<group>"; };
F56CC7280E65E0AD004307B4 /* PBGitGraphLine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitGraphLine.m; sourceTree = "<group>"; };
F56CC7300E65E0E5004307B4 /* PBGraphCellInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGraphCellInfo.h; sourceTree = "<group>"; };
F56CC7310E65E0E5004307B4 /* PBGraphCellInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGraphCellInfo.m; sourceTree = "<group>"; };
F57240BA0E9678EA00D8EE66 /* deleted_file.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = deleted_file.png; path = Images/deleted_file.png; sourceTree = "<group>"; };
Expand Down Expand Up @@ -506,7 +503,6 @@
F50FE0E10E07BE9600854FCD /* PBGitRevisionCell.h */,
F50FE0E20E07BE9600854FCD /* PBGitRevisionCell.m */,
F56CC7270E65E0AD004307B4 /* PBGitGraphLine.h */,
F56CC7280E65E0AD004307B4 /* PBGitGraphLine.m */,
F56CC7300E65E0E5004307B4 /* PBGraphCellInfo.h */,
F56CC7310E65E0E5004307B4 /* PBGraphCellInfo.m */,
F5C6F68B0E65FF9300478D97 /* PBGitLane.h */,
Expand Down Expand Up @@ -745,7 +741,6 @@
F5FF4E7A0E082E440006317A /* PBGitGrapher.mm in Sources */,
911111F80E594F3F00BF76B4 /* PBRepositoryDocumentController.m in Sources */,
913D5E5F0E556A9300CECEA2 /* PBCLIProxy.mm in Sources */,
F56CC7290E65E0AD004307B4 /* PBGitGraphLine.m in Sources */,
F56CC7320E65E0E5004307B4 /* PBGraphCellInfo.m in Sources */,
F5C6F68D0E65FF9300478D97 /* PBGitLane.mm in Sources */,
F5C007750E731B48007B84B2 /* PBGitRef.m in Sources */,
Expand Down Expand Up @@ -795,7 +790,6 @@
F5886A270ED5D4870066E74C /* PBGitRevisionCell.m in Sources */,
F5886A320ED5D5510066E74C /* PBGitCommit.m in Sources */,
F5886A340ED5D55D0066E74C /* PBGitBinary.m in Sources */,
F5886A280ED5D4870066E74C /* PBGitGraphLine.m in Sources */,
F5886A290ED5D4870066E74C /* PBGraphCellInfo.m in Sources */,
F5886A2A0ED5D4870066E74C /* PBGitLane.mm in Sources */,
F5886A1B0ED5D37C0066E74C /* NSFileHandleExt.m in Sources */,
Expand Down
21 changes: 6 additions & 15 deletions PBGitGraphLine.h
Expand Up @@ -6,19 +6,10 @@
// Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>


@interface PBGitGraphLine : NSObject
struct PBGitGraphLine
{
char upper;
char from;
char to;
char colorIndex;
}
@property(readonly) char upper, from, to, colorIndex;
- (id)initWithUpper: (char) u From: (char) f to: (char) t color: (char) c;
+ (PBGitGraphLine*) lowerLineFrom:(char) f to: (char) t color: (char) c;
+ (PBGitGraphLine*) upperLineFrom:(char) f to: (char) t color: (char) c;

@end
int upper : 1;
int from : 8;
int to : 8;
int colorIndex : 8;
};
1 change: 0 additions & 1 deletion PBGitGraphLine.m
Expand Up @@ -8,7 +8,6 @@

#import "PBGitGraphLine.h"


@implementation PBGitGraphLine
@synthesize upper, from, to, colorIndex;
- (id)initWithUpper: (char) u From: (char) f to: (char) t color: (char) c;
Expand Down
35 changes: 25 additions & 10 deletions PBGitGrapher.mm
Expand Up @@ -13,6 +13,7 @@
#import "PBGitGrapher.h"
#import "PBGitCommit.h"
#import "PBGitLane.h"
#import "PBGitGraphLine.h"
#import <vector>

using namespace std;
Expand All @@ -33,13 +34,23 @@ - (id) initWithRepository: (PBGitRepository*) repo
return self;
}

void add_line(struct PBGitGraphLine *lines, int *nLines, int upper, int from, int to, int index)
{
// TODO: put in one thing
struct PBGitGraphLine a = { upper, from, to, index };
lines[(*nLines)++] = a;
}

- (void) decorateCommit: (PBGitCommit *) commit
{
int i = 0, newPos = -1;
std::vector<PBGitLane *> *currentLanes = new std::vector<PBGitLane *>;
std::vector<PBGitLane *> *previousLanes = (std::vector<PBGitLane *> *)pl;

NSMutableArray* lines = [NSMutableArray array];
int maxLines = (previousLanes->size() + [commit.parents count] + 2) * 3;
struct PBGitGraphLine *lines = (struct PBGitGraphLine *)malloc(sizeof(struct PBGitGraphLine) * maxLines);
int currentLine = 0;

PBGitLane *currentLane = NULL;
BOOL didFirst = NO;

Expand All @@ -57,26 +68,26 @@ - (void) decorateCommit: (PBGitCommit *) commit
currentLanes->push_back(*it);
currentLane = currentLanes->back();
newPos = currentLanes->size();
[lines addObject: [PBGitGraphLine upperLineFrom: i to: newPos color: (*it)->index()]];
add_line(lines, &currentLine, 1, i, newPos,(*it)->index());
}
else {
[lines addObject: [PBGitGraphLine upperLineFrom: i to: newPos color: (*it)->index()]];
add_line(lines, &currentLine, 1, i, newPos,(*it)->index());
delete *it;
}
}
else {
// We are not this commit.
currentLanes->push_back(*it);
[lines addObject: [PBGitGraphLine upperLineFrom: i to: currentLanes->size() color: (*it)->index()]];
[lines addObject: [PBGitGraphLine lowerLineFrom: currentLanes->size() to: currentLanes->size() color: (*it)->index()]];
add_line(lines, &currentLine, 1, i, currentLanes->size(),(*it)->index());
add_line(lines, &currentLine, 0, currentLanes->size(), currentLanes->size(), (*it)->index());
}
// For existing columns, we always just continue straight down
// ^^ I don't know what that means anymore :(

if (currentLane)
[lines addObject:[PBGitGraphLine lowerLineFrom:newPos to:newPos color: currentLane->index()]];
add_line(lines, &currentLine, 0, newPos, newPos,(*it)->index());
else
[lines addObject:[PBGitGraphLine lowerLineFrom:newPos to:newPos color: 0]];
add_line(lines, &currentLine, 0, newPos, newPos, 0);
}
}
//Add your own parents
Expand All @@ -86,7 +97,7 @@ - (void) decorateCommit: (PBGitCommit *) commit
PBGitLane *newLane = new PBGitLane([commit.parents objectAtIndex:0]);
currentLanes->push_back(newLane);
newPos = currentLanes->size();
[lines addObject:[PBGitGraphLine lowerLineFrom: newPos to: newPos color: newLane->index()]];
add_line(lines, &currentLine, 0, newPos, newPos, newLane->index());
}

// Add all other parents
Expand All @@ -102,7 +113,7 @@ - (void) decorateCommit: (PBGitCommit *) commit
for (; it < currentLanes->end(); ++it) {
i++;
if ((*it)->isCommit(parent)) {
[lines addObject:[PBGitGraphLine lowerLineFrom: i to: newPos color: (*it)->index()]];
add_line(lines, &currentLine, 0, i, newPos,(*it)->index());
was_displayed = YES;
break;
}
Expand All @@ -117,10 +128,14 @@ - (void) decorateCommit: (PBGitCommit *) commit
addedParent = YES;
PBGitLane *newLane = new PBGitLane(parent);
currentLanes->push_back(newLane);
[lines addObject:[PBGitGraphLine lowerLineFrom: currentLanes->size() to: newPos color: newLane->index()]];
add_line(lines, &currentLine, 0, currentLanes->size(), newPos, newLane->index());
}

previous = [[PBGraphCellInfo alloc] initWithPosition:newPos andLines:lines];
if (currentLine > maxLines)
NSLog(@"Number of lines: %i vs allocated: %i", currentLine, maxLines);

previous.nLines = currentLine;
previous.sign = commit.sign;

// If a parent was added, we have room to not indent.
Expand Down
10 changes: 6 additions & 4 deletions PBGitRevisionCell.m
Expand Up @@ -214,11 +214,13 @@ - (void) drawWithFrame: (NSRect) rect inView:(NSView *)view
NSRect ownRect;
NSDivideRect(rect, &ownRect, &rect, pathWidth, NSMinXEdge);

for (PBGitGraphLine* line in cellInfo.lines) {
if (line.upper == 0)
[self drawLineFromColumn: line.from toColumn: line.to inRect:ownRect offset: ownRect.size.height color: line.colorIndex];
int i;
struct PBGitGraphLine *lines = cellInfo.lines;
for (i = 0; i < cellInfo.nLines; i++) {
if (lines[i].upper == 0)
[self drawLineFromColumn: lines[i].from toColumn: lines[i].to inRect:ownRect offset: ownRect.size.height color: lines[i].colorIndex];
else
[self drawLineFromColumn: line.from toColumn: line.to inRect:ownRect offset: 0 color:line.colorIndex];
[self drawLineFromColumn: lines[i].from toColumn: lines[i].to inRect:ownRect offset: 0 color:lines[i].colorIndex];
}

if (cellInfo.sign == '<' || cellInfo.sign == '>')
Expand Down
12 changes: 8 additions & 4 deletions PBGraphCellInfo.h
Expand Up @@ -7,20 +7,24 @@
//

#import <Cocoa/Cocoa.h>

#import "PBGitGraphLine.h"

@interface PBGraphCellInfo : NSObject
{
int position;
NSArray* lines;
struct PBGitGraphLine *lines;
int nLines;
int numColumns;
char sign;
NSArray* refs;
}
@property(readonly) NSArray* lines;
@property(readonly) struct PBGitGraphLine *lines;
@property(assign) int nLines;

@property(assign) int position, numColumns;
@property(assign) char sign;

- (id)initWithPosition: (int) p andLines: (NSArray*) l;

- (id)initWithPosition: (int) p andLines: (struct PBGitGraphLine *) l;

@end
11 changes: 9 additions & 2 deletions PBGraphCellInfo.m
Expand Up @@ -10,12 +10,19 @@


@implementation PBGraphCellInfo
@synthesize lines, position, numColumns, sign;
- (id)initWithPosition: (int) p andLines: (NSArray*) l
@synthesize lines, position, numColumns, sign, nLines;
- (id)initWithPosition:(int)p andLines:(struct PBGitGraphLine *)l
{
position = p;
lines = l;

return self;
}

-(void) finalize
{
free(lines);
[super finalize];
}

@end

0 comments on commit e570c3e

Please sign in to comment.