Skip to content

Commit e570c3e

Browse files
committed
Use structs for graph lines
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
1 parent 6aa2894 commit e570c3e

7 files changed

+54
-42
lines changed

GitX.xcodeproj/project.pbxproj

-6
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
F565262B0E03D89B00F03B52 /* PBWebHistoryController.m in Sources */ = {isa = PBXBuildFile; fileRef = F565262A0E03D89B00F03B52 /* PBWebHistoryController.m */; };
4747
F56ADDD90ED19F9E002AC78F /* AddBranchTemplate.png in Resources */ = {isa = PBXBuildFile; fileRef = F56ADDD70ED19F9E002AC78F /* AddBranchTemplate.png */; };
4848
F56ADDDA0ED19F9E002AC78F /* AddLabelTemplate.png in Resources */ = {isa = PBXBuildFile; fileRef = F56ADDD80ED19F9E002AC78F /* AddLabelTemplate.png */; };
49-
F56CC7290E65E0AD004307B4 /* PBGitGraphLine.m in Sources */ = {isa = PBXBuildFile; fileRef = F56CC7280E65E0AD004307B4 /* PBGitGraphLine.m */; };
5049
F56CC7320E65E0E5004307B4 /* PBGraphCellInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = F56CC7310E65E0E5004307B4 /* PBGraphCellInfo.m */; };
5150
F57240BB0E9678EA00D8EE66 /* deleted_file.png in Resources */ = {isa = PBXBuildFile; fileRef = F57240BA0E9678EA00D8EE66 /* deleted_file.png */; };
5251
F574A2850EAE2EAC003F2CB1 /* PBRefController.m in Sources */ = {isa = PBXBuildFile; fileRef = F574A2840EAE2EAC003F2CB1 /* PBRefController.m */; };
@@ -65,7 +64,6 @@
6564
F5886A1E0ED5D3880066E74C /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; };
6665
F5886A260ED5D4870066E74C /* PBGitGrapher.mm in Sources */ = {isa = PBXBuildFile; fileRef = F5FF4E790E082E440006317A /* PBGitGrapher.mm */; };
6766
F5886A270ED5D4870066E74C /* PBGitRevisionCell.m in Sources */ = {isa = PBXBuildFile; fileRef = F50FE0E20E07BE9600854FCD /* PBGitRevisionCell.m */; };
68-
F5886A280ED5D4870066E74C /* PBGitGraphLine.m in Sources */ = {isa = PBXBuildFile; fileRef = F56CC7280E65E0AD004307B4 /* PBGitGraphLine.m */; };
6967
F5886A290ED5D4870066E74C /* PBGraphCellInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = F56CC7310E65E0E5004307B4 /* PBGraphCellInfo.m */; };
7068
F5886A2A0ED5D4870066E74C /* PBGitLane.mm in Sources */ = {isa = PBXBuildFile; fileRef = F5C6F68C0E65FF9300478D97 /* PBGitLane.mm */; };
7169
F5886A310ED5D54C0066E74C /* PBGitConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 93FCCBA80EA8AF450061B02B /* PBGitConfig.m */; };
@@ -190,7 +188,6 @@
190188
F56ADDD70ED19F9E002AC78F /* AddBranchTemplate.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = AddBranchTemplate.png; path = Images/AddBranchTemplate.png; sourceTree = "<group>"; };
191189
F56ADDD80ED19F9E002AC78F /* AddLabelTemplate.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = AddLabelTemplate.png; path = Images/AddLabelTemplate.png; sourceTree = "<group>"; };
192190
F56CC7270E65E0AD004307B4 /* PBGitGraphLine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitGraphLine.h; sourceTree = "<group>"; };
193-
F56CC7280E65E0AD004307B4 /* PBGitGraphLine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitGraphLine.m; sourceTree = "<group>"; };
194191
F56CC7300E65E0E5004307B4 /* PBGraphCellInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGraphCellInfo.h; sourceTree = "<group>"; };
195192
F56CC7310E65E0E5004307B4 /* PBGraphCellInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGraphCellInfo.m; sourceTree = "<group>"; };
196193
F57240BA0E9678EA00D8EE66 /* deleted_file.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = deleted_file.png; path = Images/deleted_file.png; sourceTree = "<group>"; };
@@ -506,7 +503,6 @@
506503
F50FE0E10E07BE9600854FCD /* PBGitRevisionCell.h */,
507504
F50FE0E20E07BE9600854FCD /* PBGitRevisionCell.m */,
508505
F56CC7270E65E0AD004307B4 /* PBGitGraphLine.h */,
509-
F56CC7280E65E0AD004307B4 /* PBGitGraphLine.m */,
510506
F56CC7300E65E0E5004307B4 /* PBGraphCellInfo.h */,
511507
F56CC7310E65E0E5004307B4 /* PBGraphCellInfo.m */,
512508
F5C6F68B0E65FF9300478D97 /* PBGitLane.h */,
@@ -745,7 +741,6 @@
745741
F5FF4E7A0E082E440006317A /* PBGitGrapher.mm in Sources */,
746742
911111F80E594F3F00BF76B4 /* PBRepositoryDocumentController.m in Sources */,
747743
913D5E5F0E556A9300CECEA2 /* PBCLIProxy.mm in Sources */,
748-
F56CC7290E65E0AD004307B4 /* PBGitGraphLine.m in Sources */,
749744
F56CC7320E65E0E5004307B4 /* PBGraphCellInfo.m in Sources */,
750745
F5C6F68D0E65FF9300478D97 /* PBGitLane.mm in Sources */,
751746
F5C007750E731B48007B84B2 /* PBGitRef.m in Sources */,
@@ -795,7 +790,6 @@
795790
F5886A270ED5D4870066E74C /* PBGitRevisionCell.m in Sources */,
796791
F5886A320ED5D5510066E74C /* PBGitCommit.m in Sources */,
797792
F5886A340ED5D55D0066E74C /* PBGitBinary.m in Sources */,
798-
F5886A280ED5D4870066E74C /* PBGitGraphLine.m in Sources */,
799793
F5886A290ED5D4870066E74C /* PBGraphCellInfo.m in Sources */,
800794
F5886A2A0ED5D4870066E74C /* PBGitLane.mm in Sources */,
801795
F5886A1B0ED5D37C0066E74C /* NSFileHandleExt.m in Sources */,

PBGitGraphLine.h

+6-15
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,10 @@
66
// Copyright 2008 __MyCompanyName__. All rights reserved.
77
//
88

9-
#import <Cocoa/Cocoa.h>
10-
11-
12-
@interface PBGitGraphLine : NSObject
9+
struct PBGitGraphLine
1310
{
14-
char upper;
15-
char from;
16-
char to;
17-
char colorIndex;
18-
}
19-
@property(readonly) char upper, from, to, colorIndex;
20-
- (id)initWithUpper: (char) u From: (char) f to: (char) t color: (char) c;
21-
+ (PBGitGraphLine*) lowerLineFrom:(char) f to: (char) t color: (char) c;
22-
+ (PBGitGraphLine*) upperLineFrom:(char) f to: (char) t color: (char) c;
23-
24-
@end
11+
int upper : 1;
12+
int from : 8;
13+
int to : 8;
14+
int colorIndex : 8;
15+
};

PBGitGraphLine.m

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#import "PBGitGraphLine.h"
1010

11-
1211
@implementation PBGitGraphLine
1312
@synthesize upper, from, to, colorIndex;
1413
- (id)initWithUpper: (char) u From: (char) f to: (char) t color: (char) c;

PBGitGrapher.mm

+25-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#import "PBGitGrapher.h"
1414
#import "PBGitCommit.h"
1515
#import "PBGitLane.h"
16+
#import "PBGitGraphLine.h"
1617
#import <vector>
1718

1819
using namespace std;
@@ -33,13 +34,23 @@ - (id) initWithRepository: (PBGitRepository*) repo
3334
return self;
3435
}
3536

37+
void add_line(struct PBGitGraphLine *lines, int *nLines, int upper, int from, int to, int index)
38+
{
39+
// TODO: put in one thing
40+
struct PBGitGraphLine a = { upper, from, to, index };
41+
lines[(*nLines)++] = a;
42+
}
43+
3644
- (void) decorateCommit: (PBGitCommit *) commit
3745
{
3846
int i = 0, newPos = -1;
3947
std::vector<PBGitLane *> *currentLanes = new std::vector<PBGitLane *>;
4048
std::vector<PBGitLane *> *previousLanes = (std::vector<PBGitLane *> *)pl;
4149

42-
NSMutableArray* lines = [NSMutableArray array];
50+
int maxLines = (previousLanes->size() + [commit.parents count] + 2) * 3;
51+
struct PBGitGraphLine *lines = (struct PBGitGraphLine *)malloc(sizeof(struct PBGitGraphLine) * maxLines);
52+
int currentLine = 0;
53+
4354
PBGitLane *currentLane = NULL;
4455
BOOL didFirst = NO;
4556

@@ -57,26 +68,26 @@ - (void) decorateCommit: (PBGitCommit *) commit
5768
currentLanes->push_back(*it);
5869
currentLane = currentLanes->back();
5970
newPos = currentLanes->size();
60-
[lines addObject: [PBGitGraphLine upperLineFrom: i to: newPos color: (*it)->index()]];
71+
add_line(lines, &currentLine, 1, i, newPos,(*it)->index());
6172
}
6273
else {
63-
[lines addObject: [PBGitGraphLine upperLineFrom: i to: newPos color: (*it)->index()]];
74+
add_line(lines, &currentLine, 1, i, newPos,(*it)->index());
6475
delete *it;
6576
}
6677
}
6778
else {
6879
// We are not this commit.
6980
currentLanes->push_back(*it);
70-
[lines addObject: [PBGitGraphLine upperLineFrom: i to: currentLanes->size() color: (*it)->index()]];
71-
[lines addObject: [PBGitGraphLine lowerLineFrom: currentLanes->size() to: currentLanes->size() color: (*it)->index()]];
81+
add_line(lines, &currentLine, 1, i, currentLanes->size(),(*it)->index());
82+
add_line(lines, &currentLine, 0, currentLanes->size(), currentLanes->size(), (*it)->index());
7283
}
7384
// For existing columns, we always just continue straight down
7485
// ^^ I don't know what that means anymore :(
7586

7687
if (currentLane)
77-
[lines addObject:[PBGitGraphLine lowerLineFrom:newPos to:newPos color: currentLane->index()]];
88+
add_line(lines, &currentLine, 0, newPos, newPos,(*it)->index());
7889
else
79-
[lines addObject:[PBGitGraphLine lowerLineFrom:newPos to:newPos color: 0]];
90+
add_line(lines, &currentLine, 0, newPos, newPos, 0);
8091
}
8192
}
8293
//Add your own parents
@@ -86,7 +97,7 @@ - (void) decorateCommit: (PBGitCommit *) commit
8697
PBGitLane *newLane = new PBGitLane([commit.parents objectAtIndex:0]);
8798
currentLanes->push_back(newLane);
8899
newPos = currentLanes->size();
89-
[lines addObject:[PBGitGraphLine lowerLineFrom: newPos to: newPos color: newLane->index()]];
100+
add_line(lines, &currentLine, 0, newPos, newPos, newLane->index());
90101
}
91102

92103
// Add all other parents
@@ -102,7 +113,7 @@ - (void) decorateCommit: (PBGitCommit *) commit
102113
for (; it < currentLanes->end(); ++it) {
103114
i++;
104115
if ((*it)->isCommit(parent)) {
105-
[lines addObject:[PBGitGraphLine lowerLineFrom: i to: newPos color: (*it)->index()]];
116+
add_line(lines, &currentLine, 0, i, newPos,(*it)->index());
106117
was_displayed = YES;
107118
break;
108119
}
@@ -117,10 +128,14 @@ - (void) decorateCommit: (PBGitCommit *) commit
117128
addedParent = YES;
118129
PBGitLane *newLane = new PBGitLane(parent);
119130
currentLanes->push_back(newLane);
120-
[lines addObject:[PBGitGraphLine lowerLineFrom: currentLanes->size() to: newPos color: newLane->index()]];
131+
add_line(lines, &currentLine, 0, currentLanes->size(), newPos, newLane->index());
121132
}
122133

123134
previous = [[PBGraphCellInfo alloc] initWithPosition:newPos andLines:lines];
135+
if (currentLine > maxLines)
136+
NSLog(@"Number of lines: %i vs allocated: %i", currentLine, maxLines);
137+
138+
previous.nLines = currentLine;
124139
previous.sign = commit.sign;
125140

126141
// If a parent was added, we have room to not indent.

PBGitRevisionCell.m

+6-4
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,13 @@ - (void) drawWithFrame: (NSRect) rect inView:(NSView *)view
214214
NSRect ownRect;
215215
NSDivideRect(rect, &ownRect, &rect, pathWidth, NSMinXEdge);
216216

217-
for (PBGitGraphLine* line in cellInfo.lines) {
218-
if (line.upper == 0)
219-
[self drawLineFromColumn: line.from toColumn: line.to inRect:ownRect offset: ownRect.size.height color: line.colorIndex];
217+
int i;
218+
struct PBGitGraphLine *lines = cellInfo.lines;
219+
for (i = 0; i < cellInfo.nLines; i++) {
220+
if (lines[i].upper == 0)
221+
[self drawLineFromColumn: lines[i].from toColumn: lines[i].to inRect:ownRect offset: ownRect.size.height color: lines[i].colorIndex];
220222
else
221-
[self drawLineFromColumn: line.from toColumn: line.to inRect:ownRect offset: 0 color:line.colorIndex];
223+
[self drawLineFromColumn: lines[i].from toColumn: lines[i].to inRect:ownRect offset: 0 color:lines[i].colorIndex];
222224
}
223225

224226
if (cellInfo.sign == '<' || cellInfo.sign == '>')

PBGraphCellInfo.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,24 @@
77
//
88

99
#import <Cocoa/Cocoa.h>
10-
10+
#import "PBGitGraphLine.h"
1111

1212
@interface PBGraphCellInfo : NSObject
1313
{
1414
int position;
15-
NSArray* lines;
15+
struct PBGitGraphLine *lines;
16+
int nLines;
1617
int numColumns;
1718
char sign;
1819
NSArray* refs;
1920
}
20-
@property(readonly) NSArray* lines;
21+
@property(readonly) struct PBGitGraphLine *lines;
22+
@property(assign) int nLines;
23+
2124
@property(assign) int position, numColumns;
2225
@property(assign) char sign;
2326

24-
- (id)initWithPosition: (int) p andLines: (NSArray*) l;
27+
28+
- (id)initWithPosition: (int) p andLines: (struct PBGitGraphLine *) l;
2529

2630
@end

PBGraphCellInfo.m

+9-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@
1010

1111

1212
@implementation PBGraphCellInfo
13-
@synthesize lines, position, numColumns, sign;
14-
- (id)initWithPosition: (int) p andLines: (NSArray*) l
13+
@synthesize lines, position, numColumns, sign, nLines;
14+
- (id)initWithPosition:(int)p andLines:(struct PBGitGraphLine *)l
1515
{
1616
position = p;
1717
lines = l;
1818

1919
return self;
2020
}
21+
22+
-(void) finalize
23+
{
24+
free(lines);
25+
[super finalize];
26+
}
27+
2128
@end

0 commit comments

Comments
 (0)