Skip to content

Commit

Permalink
Add optimizations for ascii strings, plus various other smaller optim…
Browse files Browse the repository at this point in the history
…izations. Fix warnings. Fix a few bugs. Make thread save data when the terminal needs raw data for clipboard copying instead of passing possibly dangling pointers in token.position. Update architectures for test target.
  • Loading branch information
gnachman committed Mar 6, 2014
1 parent f483655 commit b0a2b80
Show file tree
Hide file tree
Showing 23 changed files with 288 additions and 203 deletions.
17 changes: 12 additions & 5 deletions PTYSession.m
Expand Up @@ -1116,7 +1116,7 @@ - (void)threadedReadTask:(char *)buffer length:(int)length {

// Parse the input stream into an array of tokens.
STOPWATCH_START(parsing);
NSMutableArray *tokens = [[NSMutableArray alloc] init];
NSMutableArray *tokens = [[NSMutableArray alloc] initWithCapacity:100];
[_terminal.parser addParsedTokensToArray:tokens];
STOPWATCH_LAP(parsing);

Expand Down Expand Up @@ -1225,7 +1225,7 @@ - (void)readTask:(const char *)buffer length:(int)length
}
[_terminal.parser putStreamData:buffer length:length];

NSMutableArray *tokens = [NSMutableArray array];
NSMutableArray *tokens = [NSMutableArray arrayWithCapacity:100];
[_terminal.parser addParsedTokensToArray:tokens];
for (VT100Token *token in tokens) {
[_terminal executeToken:token];
Expand Down Expand Up @@ -3466,7 +3466,7 @@ - (void)tmuxWindowRenamedWithId:(int)windowId to:(NSString *)newName

- (void)tmuxPrintLine:(NSString *)line
{
[_screen appendStringAtCursor:line ascii:NO];
[_screen appendStringAtCursor:line];
[_screen crlf];
}

Expand All @@ -3480,7 +3480,7 @@ - (void)tmuxHostDisconnected
_tmuxGateway = nil;
[_tmuxController release];
_tmuxController = nil;
[_screen appendStringAtCursor:@"Detached" ascii:YES];
[_screen appendStringAtCursor:@"Detached"];
[_screen crlf];
self.tmuxMode = TMUX_NONE;
_tmuxLogging = NO;
Expand Down Expand Up @@ -4648,7 +4648,7 @@ - (void)printTmuxMessage:(NSString *)message
alternateSemantics:YES];
[_terminal setBackgroundColor:ALTSEM_BG_DEFAULT
alternateSemantics:YES];
[_screen appendStringAtCursor:message ascii:YES];
[_screen appendStringAtCursor:message];
[_screen crlf];
[_terminal setForegroundColor:savedFgColor.foregroundColor
alternateSemantics:savedFgColor.foregroundColorMode == ColorModeAlternate];
Expand Down Expand Up @@ -4725,6 +4725,13 @@ - (void)screenDidAppendStringToCurrentLine:(NSString *)string {
[self appendStringToTriggerLine:string];
}

- (void)screenDidAppendAsciiDataToCurrentLine:(NSData *)asciiData {
if ([_triggers count]) {
NSString *string = [[NSString alloc] initWithData:asciiData encoding:NSASCIIStringEncoding];
[self screenDidAppendStringToCurrentLine:string];
}
}

- (void)screenSetCursorType:(ITermCursorType)type {
[[self textview] setCursorType:type];
}
Expand Down
3 changes: 2 additions & 1 deletion PTYTask.h
Expand Up @@ -16,6 +16,8 @@ extern NSString *kCoprocessStatusChangeNotification;

@interface PTYTask : NSObject

@property(atomic, readonly) BOOL hasMuteCoprocess;

- (id)init;
- (void)dealloc;
- (BOOL)hasBrokenPipe;
Expand Down Expand Up @@ -60,7 +62,6 @@ extern NSString *kCoprocessStatusChangeNotification;
- (Coprocess *)coprocess;
- (BOOL)writeBufferHasRoom;
- (BOOL)hasCoprocess;
- (BOOL)hasMuteCoprocess;
- (void)stopCoprocess;

- (void)logData:(const char *)buffer length:(int)length;
Expand Down
16 changes: 7 additions & 9 deletions PTYTask.m
Expand Up @@ -72,6 +72,10 @@
win->ws_ypixel = 0;
}

@interface PTYTask ()
@property(atomic, assign) BOOL hasMuteCoprocess;
@end

@implementation PTYTask
{
pid_t pid;
Expand Down Expand Up @@ -276,7 +280,7 @@ - (BOOL)writeBufferHasRoom

- (void)processRead
{
int iterations = 10;
int iterations = 4;
int bytesRead = 0;

char buffer[MAXRW * iterations];
Expand Down Expand Up @@ -614,6 +618,7 @@ - (void)stopCoprocess
[coprocess_ terminate];
[coprocess_ release];
coprocess_ = nil;
self.hasMuteCoprocess = NO;
}
if (thePid) {
[[TaskNotifier sharedInstance] waitForPid:thePid];
Expand All @@ -628,6 +633,7 @@ - (void)setCoprocess:(Coprocess *)coprocess
@synchronized (self) {
[coprocess_ autorelease];
coprocess_ = [coprocess retain];
self.hasMuteCoprocess = coprocess_.mute;
}
[[TaskNotifier sharedInstance] unblock];
}
Expand All @@ -648,13 +654,5 @@ - (BOOL)hasCoprocess
return NO;
}

- (BOOL)hasMuteCoprocess
{
@synchronized (self) {
return coprocess_ != nil && coprocess_.mute;
}
return NO;
}

@end

4 changes: 4 additions & 0 deletions TmuxHistoryParser.m
Expand Up @@ -36,6 +36,10 @@ - (NSData *)dataForHistoryLine:(NSString *)hist
for (VT100Token *token in tokens) {
[terminal executeToken:token];
NSString *string = token.isStringType ? token.string : nil;
if (!string && token.isStringType && token.data) {
string = [[[NSString alloc] initWithData:token.data encoding:NSASCIIStringEncoding] autorelease];
}

if (string) {
// Allocate double space in case they're all double-width characters.
screenChars = malloc(sizeof(screen_char_t) * 2 * string.length);
Expand Down
2 changes: 1 addition & 1 deletion VT100AnsiParser.h
Expand Up @@ -9,7 +9,7 @@
#import <Foundation/Foundation.h>
#import "VT100Token.h"

static BOOL isANSI(unsigned char *code, int len) {
NS_INLINE BOOL isANSI(unsigned char *code, int len) {
// Currently, we only support esc-c as an ANSI code (other ansi codes are CSI).
if (len >= 2 && code[0] == ESC && code[1] == 'c') {
return YES;
Expand Down
2 changes: 1 addition & 1 deletion VT100CSIParser.h
Expand Up @@ -18,7 +18,7 @@ typedef enum {
kIncidentalDeleteCharacterAtCursor
} VT100CSIIncidentalType;

static BOOL isCSI(unsigned char *code, int len) {
NS_INLINE BOOL isCSI(unsigned char *code, int len) {
if (len >= 2 && code[0] == ESC && (code[1] == '[')) {
return YES;
}
Expand Down
2 changes: 1 addition & 1 deletion VT100CSIParser.m
Expand Up @@ -42,7 +42,7 @@ static int advanceAndEatControlChars(unsigned char **ppdata,
case VT100CC_SUB:
case VT100CC_ESC:
case VT100CC_DEL:
[incidentals addObject:[VT100Token tokenForControlCharacter:*ppdata]];
[incidentals addObject:[VT100Token tokenForControlCharacter:**ppdata]];
break;
default:
if (**ppdata >= 0x20)
Expand Down
2 changes: 1 addition & 1 deletion VT100ControlParser.h
Expand Up @@ -9,7 +9,7 @@
#import <Foundation/Foundation.h>
#import "VT100Token.h"

static BOOL iscontrol(int c) {
NS_INLINE BOOL iscontrol(int c) {
return c <= 0x1f;
}

Expand Down
2 changes: 1 addition & 1 deletion VT100DCSParser.h
Expand Up @@ -9,7 +9,7 @@
#import <Foundation/Foundation.h>
#import "VT100Token.h"

static BOOL isDCS(unsigned char *code, int len) {
NS_INLINE BOOL isDCS(unsigned char *code, int len) {
return (len >= 2 && code[0] == ESC && code[1] == 'P');
}

Expand Down
1 change: 0 additions & 1 deletion VT100Grid.m
Expand Up @@ -566,7 +566,6 @@ - (int)appendCharsAtCursor:(screen_char_t *)buffer
#ifdef VERBOSE_STRING
NSLog(@"Begin inserting line. cursor_.x=%d, WIDTH=%d", cursor_.x, WIDTH);
#endif
NSAssert(buffer[idx].code != DWC_RIGHT, @"DWC cut off");

if (buffer[idx].code == DWC_SKIP) {
// I'm pretty sure this can never happen and that this code is just a historical leftover.
Expand Down
5 changes: 2 additions & 3 deletions VT100Parser.m
Expand Up @@ -40,7 +40,6 @@ - (BOOL)addNextParsedTokensToArray:(NSMutableArray *)output {
int datalen;

VT100Token *token = [VT100Token token];
token->isControl = NO;
token.string = nil;
// get our current position in the stream
datap = _stream + _streamOffset;
Expand Down Expand Up @@ -86,7 +85,6 @@ - (BOOL)addNextParsedTokensToArray:(NSMutableArray *)output {
}
length = rmlen;
position = datap;
token->isControl = YES;
} else {
if (isString(datap, self.encoding)) {
[VT100StringParser decodeBytes:datap
Expand Down Expand Up @@ -139,8 +137,9 @@ - (BOOL)addNextParsedTokensToArray:(NSMutableArray *)output {
}
}

token->savingData = _saveData;
if (token->type != VT100_WAIT && token->type != VT100CC_NULL) {
if (_saveData) {
if (_saveData || token->type == VT100_ASCIISTRING) {
token.data = [NSData dataWithBytes:position length:length];
}
[output addObject:token];
Expand Down
3 changes: 2 additions & 1 deletion VT100Screen.h
Expand Up @@ -146,7 +146,8 @@ extern int kVT100ScreenMinRows;
// Append a string to the screen at the current cursor position. The terminal's insert and wrap-
// around modes are respected, the cursor is advanced, the screen may be scrolled, and the line
// buffer may change.
- (void)appendStringAtCursor:(NSString *)s ascii:(BOOL)ascii;
- (void)appendStringAtCursor:(NSString *)string;
- (void)appendAsciiDataAtCursor:(NSData *)asciiData;

// This is a hacky thing that moves the cursor to the next line, not respecting scroll regions.
// It's used for the tmux status screen.
Expand Down

0 comments on commit b0a2b80

Please sign in to comment.