mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-31 16:50:58 +00:00
Small performance optimisations
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@3205 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
bc84052581
commit
bb9cd82e42
1 changed files with 59 additions and 120 deletions
|
@ -108,10 +108,12 @@ static int debug_run_loop = 0;
|
||||||
* runloop must ask the 'receiver' (or its delegate) to supply a date
|
* runloop must ask the 'receiver' (or its delegate) to supply a date
|
||||||
* using the '[-limitDateForMode:]' message.
|
* using the '[-limitDateForMode:]' message.
|
||||||
*
|
*
|
||||||
|
* NB. This class is private to NSRunLoop and must not be subclassed.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@interface RunLoopWatcher: NSObject
|
@interface RunLoopWatcher: NSObject
|
||||||
{
|
{
|
||||||
|
@public
|
||||||
BOOL invalidated;
|
BOOL invalidated;
|
||||||
void *data;
|
void *data;
|
||||||
id receiver;
|
id receiver;
|
||||||
|
@ -121,20 +123,11 @@ static int debug_run_loop = 0;
|
||||||
}
|
}
|
||||||
- (void) eventFor: (void*)info
|
- (void) eventFor: (void*)info
|
||||||
mode: (NSString*)mode;
|
mode: (NSString*)mode;
|
||||||
- (void*) getData;
|
|
||||||
- (NSDate*) getLimit;
|
|
||||||
- (id) getReceiver;
|
|
||||||
- (RunLoopEventType) getType;
|
|
||||||
- (BOOL) decrement;
|
|
||||||
- (void) increment;
|
|
||||||
- initWithType: (RunLoopEventType)type
|
- initWithType: (RunLoopEventType)type
|
||||||
receiver: (id)anObj
|
receiver: (id)anObj
|
||||||
data: (void*)data;
|
data: (void*)data;
|
||||||
- (void) invalidate;
|
- (void) invalidate;
|
||||||
- (BOOL) isValid;
|
- (BOOL) isValid;
|
||||||
- (void) setData: (void*)item;
|
|
||||||
- (void) setLimit: (NSDate*)when;
|
|
||||||
- (void) setReceiver: (id)anObj;
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation RunLoopWatcher
|
@implementation RunLoopWatcher
|
||||||
|
@ -147,19 +140,6 @@ static int debug_run_loop = 0;
|
||||||
[super dealloc];
|
[super dealloc];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL) decrement
|
|
||||||
{
|
|
||||||
if (count > 0)
|
|
||||||
{
|
|
||||||
count--;
|
|
||||||
if (count > 0)
|
|
||||||
{
|
|
||||||
return YES;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NO;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void) eventFor: (void*)info
|
- (void) eventFor: (void*)info
|
||||||
mode: (NSString*)mode
|
mode: (NSString*)mode
|
||||||
{
|
{
|
||||||
|
@ -189,53 +169,22 @@ static int debug_run_loop = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void*) getData
|
|
||||||
{
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (NSDate*) getLimit
|
|
||||||
{
|
|
||||||
return limit;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (id) getReceiver
|
|
||||||
{
|
|
||||||
return receiver;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (RunLoopEventType) getType
|
|
||||||
{
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void) increment
|
|
||||||
{
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
- initWithType: (RunLoopEventType)aType
|
- initWithType: (RunLoopEventType)aType
|
||||||
receiver: (id)anObj
|
receiver: (id)anObj
|
||||||
data: (void*)item
|
data: (void*)item
|
||||||
{
|
{
|
||||||
self = [super init];
|
invalidated = NO;
|
||||||
if (self)
|
switch (aType)
|
||||||
{
|
{
|
||||||
invalidated = NO;
|
case ET_RDESC: type = aType; break;
|
||||||
switch (aType)
|
case ET_WDESC: type = aType; break;
|
||||||
{
|
case ET_RPORT: type = aType; break;
|
||||||
case ET_RDESC: type = aType; break;
|
default:
|
||||||
case ET_WDESC: type = aType; break;
|
[NSException raise: NSInvalidArgumentException
|
||||||
case ET_RPORT: type = aType; break;
|
format: @"NSRunLoop - unknown event type"];
|
||||||
default:
|
|
||||||
[NSException raise: NSInvalidArgumentException
|
|
||||||
format: @"NSRunLoop - unknown event type"];
|
|
||||||
}
|
|
||||||
[self setReceiver: anObj];
|
|
||||||
[self setData: item];
|
|
||||||
[self setLimit: nil];
|
|
||||||
count = 0;
|
|
||||||
}
|
}
|
||||||
|
receiver = [anObj retain];
|
||||||
|
data = item;
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,28 +208,6 @@ static int debug_run_loop = 0;
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void) setData: (void*)item
|
|
||||||
{
|
|
||||||
data = item;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void) setLimit: (NSDate*)when
|
|
||||||
{
|
|
||||||
NSDate* d = [when retain];
|
|
||||||
|
|
||||||
[limit release];
|
|
||||||
limit = d;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void) setReceiver: (id)anObject
|
|
||||||
{
|
|
||||||
id obj = receiver;
|
|
||||||
|
|
||||||
receiver = [anObject retain];
|
|
||||||
|
|
||||||
[obj release];
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
||||||
|
@ -529,20 +456,24 @@ static int debug_run_loop = 0;
|
||||||
* 'limitDateForMode:' then we ask them for the limit date for
|
* 'limitDateForMode:' then we ask them for the limit date for
|
||||||
* this watcher.
|
* this watcher.
|
||||||
*/
|
*/
|
||||||
obj = [item getReceiver];
|
obj = item->receiver;
|
||||||
if ([obj respondsToSelector: @selector(limitDateForMode:)])
|
if ([obj respondsToSelector: @selector(limitDateForMode:)])
|
||||||
{
|
{
|
||||||
[item setLimit: [obj limitDateForMode:mode]];
|
NSDate *d = [obj limitDateForMode: mode];
|
||||||
|
|
||||||
|
ASSIGN(item->limit, d);
|
||||||
}
|
}
|
||||||
else if ([obj respondsToSelector: @selector(delegate)])
|
else if ([obj respondsToSelector: @selector(delegate)])
|
||||||
{
|
{
|
||||||
obj = [obj delegate];
|
obj = [obj delegate];
|
||||||
if ([obj respondsToSelector: @selector(limitDateForMode:)])
|
if ([obj respondsToSelector: @selector(limitDateForMode:)])
|
||||||
{
|
{
|
||||||
[item setLimit: [obj limitDateForMode:mode]];
|
NSDate *d = [obj limitDateForMode: mode];
|
||||||
|
|
||||||
|
ASSIGN(item->limit, d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
limit = [item getLimit];
|
limit = item->limit;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Make sure that the items in the watchers list are ordered.
|
* Make sure that the items in the watchers list are ordered.
|
||||||
|
@ -557,7 +488,8 @@ static int debug_run_loop = 0;
|
||||||
|
|
||||||
for (i = 0; i < count; i++)
|
for (i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
NSDate* when = [[watchers objectAtIndex:i] getLimit];
|
RunLoopWatcher *watcher = [watchers objectAtIndex: i];
|
||||||
|
NSDate *when = watcher->limit;
|
||||||
|
|
||||||
if (when == nil || [limit earlierDate:when] == when)
|
if (when == nil || [limit earlierDate:when] == when)
|
||||||
{
|
{
|
||||||
|
@ -648,10 +580,10 @@ static int debug_run_loop = 0;
|
||||||
|
|
||||||
info = [self _getWatcher: data type: type forMode: mode];
|
info = [self _getWatcher: data type: type forMode: mode];
|
||||||
|
|
||||||
if (info && [info getReceiver] == (id)watcher)
|
if (info && info->receiver == (id)watcher)
|
||||||
{
|
{
|
||||||
/* Increment usage count for this watcher. */
|
/* Increment usage count for this watcher. */
|
||||||
[info increment];
|
info->count++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -664,7 +596,7 @@ static int debug_run_loop = 0;
|
||||||
data: data];
|
data: data];
|
||||||
/* Add the object to the array for the mode and keep count. */
|
/* Add the object to the array for the mode and keep count. */
|
||||||
[self _addWatcher:info forMode:mode];
|
[self _addWatcher:info forMode:mode];
|
||||||
[info increment];
|
info->count++;
|
||||||
|
|
||||||
[info release]; /* Now held in array. */
|
[info release]; /* Now held in array. */
|
||||||
}
|
}
|
||||||
|
@ -710,10 +642,17 @@ static int debug_run_loop = 0;
|
||||||
|
|
||||||
info = [self _getWatcher: data type: type forMode: mode];
|
info = [self _getWatcher: data type: type forMode: mode];
|
||||||
|
|
||||||
if (info && [info decrement] == NO)
|
if (info)
|
||||||
{
|
{
|
||||||
[self _removeWatcher: data type: type forMode: mode];
|
if (info->count == 0)
|
||||||
}
|
{
|
||||||
|
[self _removeWatcher: data type: type forMode: mode];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
info->count--;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -845,7 +784,7 @@ static int debug_run_loop = 0;
|
||||||
Heap *timers;
|
Heap *timers;
|
||||||
NSTimer *min_timer = nil;
|
NSTimer *min_timer = nil;
|
||||||
RunLoopWatcher *min_watcher = nil;
|
RunLoopWatcher *min_watcher = nil;
|
||||||
NSArray *watchers;
|
NSMutableArray *watchers;
|
||||||
NSDate *when;
|
NSDate *when;
|
||||||
|
|
||||||
saved_mode = _current_mode;
|
saved_mode = _current_mode;
|
||||||
|
@ -893,12 +832,12 @@ static int debug_run_loop = 0;
|
||||||
|
|
||||||
if (![min_watcher isValid])
|
if (![min_watcher isValid])
|
||||||
{
|
{
|
||||||
[watchers removeObjectAtIndex:0];
|
[watchers removeObjectAtIndex: 0];
|
||||||
min_watcher = nil;
|
min_watcher = nil;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
when = [min_watcher getLimit];
|
when = min_watcher->limit;
|
||||||
if (when == nil || [when timeIntervalSinceNow] > 0)
|
if (when == nil || [when timeIntervalSinceNow] > 0)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
|
@ -913,13 +852,13 @@ static int debug_run_loop = 0;
|
||||||
* timeouts - inform it and give it a chance to set a
|
* timeouts - inform it and give it a chance to set a
|
||||||
* revised limit date.
|
* revised limit date.
|
||||||
*/
|
*/
|
||||||
obj = [min_watcher getReceiver];
|
obj = min_watcher->receiver;
|
||||||
if ([obj respondsToSelector:
|
if ([obj respondsToSelector:
|
||||||
@selector(timedOutEvent:type:forMode:)])
|
@selector(timedOutEvent:type:forMode:)])
|
||||||
{
|
{
|
||||||
nxt = [obj timedOutEvent:[min_watcher getData]
|
nxt = [obj timedOutEvent: min_watcher->data
|
||||||
type:[min_watcher getType]
|
type: min_watcher->type
|
||||||
forMode:_current_mode];
|
forMode: _current_mode];
|
||||||
}
|
}
|
||||||
else if ([obj respondsToSelector:@selector(delegate)])
|
else if ([obj respondsToSelector:@selector(delegate)])
|
||||||
{
|
{
|
||||||
|
@ -927,9 +866,9 @@ static int debug_run_loop = 0;
|
||||||
if ([obj respondsToSelector:
|
if ([obj respondsToSelector:
|
||||||
@selector(timedOutEvent:type:forMode:)])
|
@selector(timedOutEvent:type:forMode:)])
|
||||||
{
|
{
|
||||||
nxt = [obj timedOutEvent:[min_watcher getData]
|
nxt = [obj timedOutEvent: min_watcher->data
|
||||||
type:[min_watcher getType]
|
type: min_watcher->type
|
||||||
forMode:_current_mode];
|
forMode: _current_mode];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (nxt && [nxt timeIntervalSinceNow] > 0.0)
|
if (nxt && [nxt timeIntervalSinceNow] > 0.0)
|
||||||
|
@ -939,9 +878,9 @@ static int debug_run_loop = 0;
|
||||||
* re-insert it into the queue in the correct place.
|
* re-insert it into the queue in the correct place.
|
||||||
*/
|
*/
|
||||||
[min_watcher retain];
|
[min_watcher retain];
|
||||||
[min_watcher setLimit:nxt];
|
ASSIGN(min_watcher->limit, nxt);
|
||||||
[watchers removeObjectAtIndex:0];
|
[watchers removeObjectAtIndex: 0];
|
||||||
[self _addWatcher:min_watcher forMode:mode];
|
[self _addWatcher: min_watcher forMode: mode];
|
||||||
[min_watcher release];
|
[min_watcher release];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -982,13 +921,13 @@ static int debug_run_loop = 0;
|
||||||
{
|
{
|
||||||
NSDate* lim;
|
NSDate* lim;
|
||||||
|
|
||||||
if ([min_watcher getLimit] == nil) /* No limit for watcher */
|
if (min_watcher->limit == nil) /* No limit for watcher */
|
||||||
{
|
{
|
||||||
lim = [NSDate distantFuture]; /* - watches forever. */
|
lim = [NSDate distantFuture]; /* - watches forever. */
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
lim = [min_watcher getLimit];
|
lim = min_watcher->limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (when == nil)
|
if (when == nil)
|
||||||
|
@ -1037,9 +976,9 @@ static int debug_run_loop = 0;
|
||||||
{
|
{
|
||||||
info = [watchers objectAtIndex: count];
|
info = [watchers objectAtIndex: count];
|
||||||
|
|
||||||
if ([info getType] == type)
|
if (info->type == type)
|
||||||
{
|
{
|
||||||
if ([info getData] == data)
|
if (info->data == data)
|
||||||
{
|
{
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
@ -1069,7 +1008,7 @@ static int debug_run_loop = 0;
|
||||||
RunLoopWatcher* info;
|
RunLoopWatcher* info;
|
||||||
|
|
||||||
info = (RunLoopWatcher*)[watchers objectAtIndex:(i-1)];
|
info = (RunLoopWatcher*)[watchers objectAtIndex:(i-1)];
|
||||||
if ([info getType] == type && [info getData] == data)
|
if (info->type == type && info->data == data)
|
||||||
{
|
{
|
||||||
[info invalidate];
|
[info invalidate];
|
||||||
[watchers removeObject: info];
|
[watchers removeObject: info];
|
||||||
|
@ -1161,7 +1100,7 @@ static int debug_run_loop = 0;
|
||||||
|
|
||||||
/* Do the pre-listening set-up for the file descriptors of this mode. */
|
/* Do the pre-listening set-up for the file descriptors of this mode. */
|
||||||
{
|
{
|
||||||
NSArray *watchers;
|
NSMutableArray *watchers;
|
||||||
|
|
||||||
watchers = NSMapGet (_mode_2_watchers, mode);
|
watchers = NSMapGet (_mode_2_watchers, mode);
|
||||||
if (watchers) {
|
if (watchers) {
|
||||||
|
@ -1172,19 +1111,19 @@ static int debug_run_loop = 0;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
if ([info isValid] == NO) {
|
if ([info isValid] == NO) {
|
||||||
[watchers removeObjectAtIndex:(i-1)];
|
[watchers removeObjectAtIndex: (i-1)];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
switch ([info getType]) {
|
switch (info->type) {
|
||||||
case ET_WDESC:
|
case ET_WDESC:
|
||||||
fd = (int)[info getData];
|
fd = (int)info->data;
|
||||||
FD_SET (fd, &write_fds);
|
FD_SET (fd, &write_fds);
|
||||||
NSMapInsert (wfd_2_object, (void*)fd, info);
|
NSMapInsert (wfd_2_object, (void*)fd, info);
|
||||||
num_inputs++;
|
num_inputs++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ET_RDESC:
|
case ET_RDESC:
|
||||||
fd = (int)[info getData];
|
fd = (int)info->data;
|
||||||
FD_SET (fd, &fds);
|
FD_SET (fd, &fds);
|
||||||
NSMapInsert (rfd_2_object, (void*)fd, info);
|
NSMapInsert (rfd_2_object, (void*)fd, info);
|
||||||
num_inputs++;
|
num_inputs++;
|
||||||
|
@ -1192,7 +1131,7 @@ static int debug_run_loop = 0;
|
||||||
|
|
||||||
case ET_RPORT:
|
case ET_RPORT:
|
||||||
{
|
{
|
||||||
id port = [info getReceiver];
|
id port = info->receiver;
|
||||||
int port_fd_count = 128; // xxx #define this constant
|
int port_fd_count = 128; // xxx #define this constant
|
||||||
int port_fd_array[port_fd_count];
|
int port_fd_array[port_fd_count];
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue