Skip to content

Commit

Permalink
Merge pull request #536 from DivineDominion/textstorage-speedup
Browse files Browse the repository at this point in the history
use NSTextStorage for speedups
  • Loading branch information
splhack authored Aug 8, 2017
2 parents 03750a9 + 64a5e97 commit a7579ca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/MacVim/MMTextStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ typedef struct {


@interface MMTextStorage : NSTextStorage {
NSMutableAttributedString *attribString;
NSTextStorage *backingStore;
int maxRows, maxColumns;
int actualRows, actualColumns;
NSAttributedString *emptyRowString;
Expand Down
78 changes: 39 additions & 39 deletions src/MacVim/MMTextStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ @implementation MMTextStorage
- (id)init
{
if ((self = [super init])) {
attribString = [[NSMutableAttributedString alloc] initWithString:@""];
backingStore = [[NSTextStorage alloc] init];
// NOTE! It does not matter which font is set here, Vim will set its
// own font on startup anyway. Just set some bogus values.
font = [[NSFont userFixedPitchFontOfSize:0] retain];
Expand Down Expand Up @@ -104,19 +104,19 @@ - (void)dealloc
[font release]; font = nil;
[defaultBackgroundColor release]; defaultBackgroundColor = nil;
[defaultForegroundColor release]; defaultForegroundColor = nil;
[attribString release]; attribString = nil;
[backingStore release]; backingStore = nil;
[super dealloc];
}

- (NSString *)string
{
return [attribString string];
return [backingStore string];
}

- (NSDictionary *)attributesAtIndex:(NSUInteger)index
effectiveRange:(NSRangePointer)range
{
return [attribString attributesAtIndex:index effectiveRange:range];
return [backingStore attributesAtIndex:index effectiveRange:range];
}

- (void)replaceCharactersInRange:(NSRange)range
Expand All @@ -126,15 +126,15 @@ - (void)replaceCharactersInRange:(NSRange)range
ASLogWarn(@"Calling %@ on MMTextStorage is unsupported",
NSStringFromSelector(_cmd));
#endif
//[attribString replaceCharactersInRange:range withString:string];
//[backingStore replaceCharactersInRange:range withString:string];
}

- (void)setAttributes:(NSDictionary *)attributes range:(NSRange)range
{
// NOTE! This method must be implemented since the text system calls it
// constantly to 'fix attributes', apply font substitution, etc.
#if 0
[attribString setAttributes:attributes range:range];
[backingStore setAttributes:attributes range:range];
#elif 1
// HACK! If the font attribute is being modified, then ensure that the new
// font has a fixed advancement which is either the same as the current
Expand All @@ -153,7 +153,7 @@ - (void)setAttributes:(NSDictionary *)attributes range:(NSRange)range
return;

float adv = cellSize.width;
if ([attribString attribute:MMWideCharacterAttributeName
if ([backingStore attribute:MMWideCharacterAttributeName
atIndex:range.location
effectiveRange:NULL])
adv += adv;
Expand All @@ -170,9 +170,9 @@ - (void)setAttributes:(NSDictionary *)attributes range:(NSRange)range
dictionaryWithDictionary:attributes];
[newAttr setObject:newFont forKey:NSFontAttributeName];

[attribString setAttributes:newAttr range:range];
[backingStore setAttributes:newAttr range:range];
} else {
[attribString setAttributes:attributes range:range];
[backingStore setAttributes:attributes range:range];
}
#endif
}
Expand Down Expand Up @@ -329,34 +329,34 @@ - (void)drawString:(NSString *)string atRow:(int)row column:(int)col
}

// Mark these characters as wide. This attribute is subsequently checked
// when translating (row,col) pairs to offsets within 'attribString'.
// when translating (row,col) pairs to offsets within 'backingStore'.
if (flags & DRAW_WIDE)
[attributes setObject:[NSNull null]
forKey:MMWideCharacterAttributeName];

// Replace characters in text storage and apply new attributes.
NSRange r = NSMakeRange(range.location, [string length]);
[attribString replaceCharactersInRange:range withString:string];
[attribString setAttributes:attributes range:r];
[backingStore replaceCharactersInRange:range withString:string];
[backingStore setAttributes:attributes range:r];

NSInteger changeInLength = [string length] - range.length;
if (acells != cells || acol != col) {
if (acells == cells + 1) {
// NOTE: A normal width character replaced a double width
// character. To maintain the invariant that each row covers the
// same amount of cells, we compensate by adding an empty column.
[attribString replaceCharactersInRange:NSMakeRange(NSMaxRange(r),0)
[backingStore replaceCharactersInRange:NSMakeRange(NSMaxRange(r),0)
withAttributedString:[emptyRowString
attributedSubstringFromRange:NSMakeRange(0,1)]];
++changeInLength;
#if 0
} else if (acol == col - 1) {
[attribString replaceCharactersInRange:NSMakeRange(r.location,0)
[backingStore replaceCharactersInRange:NSMakeRange(r.location,0)
withAttributedString:[emptyRowString
attributedSubstringFromRange:NSMakeRange(0,1)]];
++changeInLength;
} else if (acol == col + 1) {
[attribString replaceCharactersInRange:NSMakeRange(r.location-1,1)
[backingStore replaceCharactersInRange:NSMakeRange(r.location-1,1)
withAttributedString:[emptyRowString
attributedSubstringFromRange:NSMakeRange(0,2)]];
++changeInLength;
Expand Down Expand Up @@ -435,10 +435,10 @@ - (void)deleteLinesFromRow:(int)row lineCount:(int)count
return;
}

NSAttributedString *srcString = [attribString
NSAttributedString *srcString = [backingStore
attributedSubstringFromRange:srcRange];

[attribString replaceCharactersInRange:destRange
[backingStore replaceCharactersInRange:destRange
withAttributedString:srcString];
[self edited:(NSTextStorageEditedCharacters
| NSTextStorageEditedAttributes) range:destRange
Expand Down Expand Up @@ -471,9 +471,9 @@ - (void)deleteLinesFromRow:(int)row lineCount:(int)count
return;
}

[attribString replaceCharactersInRange:destRange
[backingStore replaceCharactersInRange:destRange
withAttributedString:emptyString];
[attribString setAttributes:attribs
[backingStore setAttributes:attribs
range:NSMakeRange(destRange.location, width)];

[self edited:(NSTextStorageEditedAttributes
Expand Down Expand Up @@ -531,9 +531,9 @@ - (void)insertLinesAtRow:(int)row lineCount:(int)count
return;
}

NSAttributedString *srcString = [attribString
NSAttributedString *srcString = [backingStore
attributedSubstringFromRange:srcRange];
[attribString replaceCharactersInRange:destRange
[backingStore replaceCharactersInRange:destRange
withAttributedString:srcString];
[self edited:(NSTextStorageEditedCharacters
| NSTextStorageEditedAttributes) range:destRange
Expand Down Expand Up @@ -566,9 +566,9 @@ - (void)insertLinesAtRow:(int)row lineCount:(int)count
return;
}

[attribString replaceCharactersInRange:destRange
[backingStore replaceCharactersInRange:destRange
withAttributedString:emptyString];
[attribString setAttributes:attribs
[backingStore setAttributes:attribs
range:NSMakeRange(destRange.location, width)];

[self edited:(NSTextStorageEditedAttributes
Expand Down Expand Up @@ -613,9 +613,9 @@ - (void)clearBlockFromRow:(int)row1 column:(int)col1 toRow:(int)row2
return;
}

[attribString replaceCharactersInRange:range
[backingStore replaceCharactersInRange:range
withAttributedString:emptyString];
[attribString setAttributes:attribs
[backingStore setAttributes:attribs
range:NSMakeRange(range.location, cells)];

[self edited:(NSTextStorageEditedAttributes
Expand Down Expand Up @@ -917,7 +917,7 @@ - (NSRect)boundingRectForCharacterAtRow:(int)row column:(int)col
int cells = 1;
NSRange r = [self charRangeForRow:row column:&col cells:&cells];
if (NSNotFound != r.location
&& [attribString attribute:MMWideCharacterAttributeName
&& [backingStore attribute:MMWideCharacterAttributeName
atIndex:r.location
effectiveRange:nil])
rect.size.width += rect.size.width;
Expand Down Expand Up @@ -958,7 +958,7 @@ - (void)lazyResize:(BOOL)force
if (!force && actualRows == maxRows && actualColumns == maxColumns)
return;

NSRange oldRange = NSMakeRange(0, [attribString length]);
NSRange oldRange = NSMakeRange(0, [backingStore length]);

actualRows = maxRows;
actualColumns = maxColumns;
Expand Down Expand Up @@ -990,16 +990,16 @@ - (void)lazyResize:(BOOL)force
emptyRowString = [[NSAttributedString alloc] initWithString:rowString
attributes:dict];

[attribString release];
attribString = [[NSMutableAttributedString alloc] init];
[backingStore release];
backingStore = [[NSMutableAttributedString alloc] init];
for (i=0; i<maxRows; ++i) {
#if MM_USE_ROW_CACHE
rowCache[i].length = actualColumns + 1;
#endif
[attribString appendAttributedString:emptyRowString];
[backingStore appendAttributedString:emptyRowString];
}

NSRange fullRange = NSMakeRange(0, [attribString length]);
NSRange fullRange = NSMakeRange(0, [backingStore length]);
[self edited:(NSTextStorageEditedCharacters|NSTextStorageEditedAttributes)
range:oldRange changeInLength:fullRange.length-oldRange.length];
}
Expand All @@ -1014,7 +1014,7 @@ - (NSRange)charRangeForRow:(int)row column:(int*)pcol cells:(int*)pcells
if (characterEqualsColumn)
return NSMakeRange(row*(actualColumns+1) + col, cells);

NSString *string = [attribString string];
NSString *string = [backingStore string];
unsigned stringLen = [string length];
NSRange r, range = { NSNotFound, 0 };
unsigned idx;
Expand Down Expand Up @@ -1082,7 +1082,7 @@ - (NSRange)charRangeForRow:(int)row column:(int*)pcol cells:(int*)pcells
r = [string rangeOfComposedCharacterSequenceAtIndex:idx];

// Wide chars take up two display cells.
if ([attribString attribute:MMWideCharacterAttributeName
if ([backingStore attribute:MMWideCharacterAttributeName
atIndex:idx
effectiveRange:nil])
++i;
Expand All @@ -1100,7 +1100,7 @@ - (NSRange)charRangeForRow:(int)row column:(int*)pcol cells:(int*)pcells
--i;

// Wide chars take up two display cells.
if ([attribString attribute:MMWideCharacterAttributeName
if ([backingStore attribute:MMWideCharacterAttributeName
atIndex:idx
effectiveRange:nil])
--i;
Expand All @@ -1127,7 +1127,7 @@ - (NSRange)charRangeForRow:(int)row column:(int*)pcol cells:(int*)pcells
r = [string rangeOfComposedCharacterSequenceAtIndex:idx];

// Wide chars take up two display cells.
if ([attribString attribute:MMWideCharacterAttributeName
if ([backingStore attribute:MMWideCharacterAttributeName
atIndex:idx
effectiveRange:nil])
++i;
Expand All @@ -1149,7 +1149,7 @@ - (NSRange)charRangeForRow:(int)row column:(int*)pcol cells:(int*)pcells
r = [string rangeOfComposedCharacterSequenceAtIndex:idx];

// Wide chars take up two display cells.
if ([attribString attribute:MMWideCharacterAttributeName
if ([backingStore attribute:MMWideCharacterAttributeName
atIndex:idx
effectiveRange:nil])
++i;
Expand All @@ -1166,7 +1166,7 @@ - (NSRange)charRangeForRow:(int)row column:(int*)pcol cells:(int*)pcells
r = [string rangeOfComposedCharacterSequenceAtIndex:idx];

// Wide chars take up two display cells.
if ([attribString attribute:MMWideCharacterAttributeName
if ([backingStore attribute:MMWideCharacterAttributeName
atIndex:idx
effectiveRange:nil])
++i;
Expand Down Expand Up @@ -1218,14 +1218,14 @@ - (void)fixInvalidCharactersInRange:(NSRange)range
// TODO: Treat these separately inside of Vim so we don't have to bother
// here.
while (range.length > 0) {
invalidRange = [[attribString string]
invalidRange = [[backingStore string]
rangeOfCharacterFromSet:invalidCharacterSet
options:NSLiteralSearch
range:range];
if (NSNotFound == invalidRange.location)
break;

[attribString replaceCharactersInRange:invalidRange withString:@" "];
[backingStore replaceCharactersInRange:invalidRange withString:@" "];

end = NSMaxRange(invalidRange);
range.length -= end - range.location;
Expand Down

0 comments on commit a7579ca

Please sign in to comment.