-
Notifications
You must be signed in to change notification settings - Fork 17
/
MPWFastInvocation.m
410 lines (338 loc) · 9.75 KB
/
MPWFastInvocation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//
// MPWFastInvocation.m
// MPWFoundation
//
// Created by Marcel Weiher on 27/3/07.
// Copyright 2010-2017 by Marcel Weiher. All rights reserved.
//
#import "MPWFastInvocation.h"
#import "DebugMacros.h"
#import <AccessorMacros.h>
#import "NSInvocationAdditions_lookup.h"
#define FULL_MPWFOUNDATION 1
#if FULL_MPWFOUNDATION && !WINDOWS && !LINUX
#import "MPWRusage.h"
#endif
#import "MPWObjectCache.h"
#define MAXARGS 10
@implementation MPWFastInvocation {
SEL selector;
id target;
int numargs;
id args[MAXARGS];
id result;
IMP0 cached;
BOOL useCaching;
NSMethodSignature *methodSignature;
}
CACHING_ALLOC( quickInvocation, 5, YES )
scalarAccessor( id, target, _setTarget )
idAccessor( result, setResult )
boolAccessor( useCaching, _setUseCaching )
lazyAccessor( NSMethodSignature*, methodSignature, setMethodSignature, getSignature)
// extern id objc_msgSend( id receiver, SEL _cmd, ... );
-(NSMethodSignature*)getSignature
{
return [target methodSignatureForSelector:selector ];
}
-copyWithZone:aZone
{
return [self retain];
}
-init
{
self = [super init];
if ( self && !invokeFun) {
invokeFun=(IMP0)[self methodForSelector:@selector(resultOfInvoking)];
}
return self;
}
-initWithMethodSignature:(NSMethodSignature*)signature
{
return [self init];
}
+invocation
{
return [[[self alloc] init] autorelease];
}
+invocationWithMethodSignature:(NSMethodSignature*)signature
{
return [self invocation];
}
//extern id objc_msgSend( id target, SEL selector, ... );
-(void)recache
{
cached=NULL;
if ( useCaching && target && selector) {
// cached=objc_msg_lookup( target, selector );
cached=(IMP0)[target methodForSelector:selector];
}
#if !LINUX
if (!cached) {
cached=(IMP0)objc_msgSend;
}
#endif
}
-(void)setIMP:(IMP)newImp
{
cached=(IMP0)newImp;
}
-(void)setTarget:newTarget
{
BOOL needToRecache=target != newTarget;
[self _setTarget:newTarget];
if (useCaching && needToRecache) {
[self recache];
}
}
-(void)setUseCaching:(BOOL)shouldUseCaching
{
[self _setUseCaching:shouldUseCaching];
[self recache];
}
-(SEL)selector
{
return selector;
}
-(void)setSelector:(SEL)newSelector
{
selector=newSelector;
[self recache];
}
-(id)evaluateOnObject:newTarget parameters:(NSArray*)parameters
{
long paramCount = parameters.count;
for (long i=0,max=MIN(MAXARGS,paramCount); i<max;i++) {
args[i]=parameters[i];
}
return [self returnValueAfterInvokingWithTarget:newTarget];
}
-(void)setArgument:(void*)argbuf atIndex:(NSInteger)anIndex
{
anIndex-=2;
if ( anIndex >= 0 && anIndex < 6 ) {
args[anIndex]=*(id*)argbuf;
}
}
-(void)getReturnValue:(void*)returnValueBuf
{
*(id*)returnValueBuf = result;
}
-(void)setReturnValue:(void*)retval
{
[self setResult:*(id*)retval];
}
//typedef id (*IMP4)(id, SEL, id,id,id,id);
-resultOfInvoking
{
return ((IMP4)cached)( target, selector, args[0], args[1],args[2],args[3] );
}
-(id)invokeWithArgs:(va_list)varArgs
{
for (int i=0;i<3;i++) {
args[i]=va_arg(varArgs, id);
}
va_end(varArgs);
return [self resultOfInvoking];
}
-returnValueAfterInvokingWithTarget:aTarget
{
id retval = ((IMP4)cached)( aTarget, selector, args[0], args[1],args[2],args[3] );
return retval;
}
-resultOfInvokingWithArgs:(id*)newArgs count:(int)count
{
return ((IMP4)cached)( target, selector, newArgs[0], newArgs[1],newArgs[2],newArgs[3] );
}
-(void)invoke
{
[self setResult:[self resultOfInvoking]];
}
-(void)retainArguments
{
// NSLog(@"MPWFastInvocation retainArguments");
}
-(NSString *)description
{
//--- target may be deallocated so don't %@ it...
return [NSString stringWithFormat:@"<%@:%p:[%p %@]>",[self class],self,target,NSStringFromSelector(selector)];
}
-(void)dealloc
{
// NSLog(@"dealloc MPWFastInvocation %p",self);
[methodSignature release];
[result release];
[super dealloc];
}
@end
@implementation NSInvocation(convenience)
-resultOfInvoking
{
id result;
[self invoke];
[self getReturnValue:&result];
return result;
}
@end
@implementation MPWFastInvocation(testing)
+testInvocationOfClass:(Class)invocationClass
{
SEL cat = @selector(stringByAppendingString:);
NSMethodSignature *sig = [NSString instanceMethodSignatureForSelector:cat];
NSInvocation *invocation=[invocationClass invocationWithMethodSignature:sig];
id world=@" World!";
[invocation setTarget:@"Hello"];
[invocation setSelector:cat];
[invocation setArgument:&world atIndex:2];
return invocation;
}
+simpleSendWithInvocationOfClass:(Class)invocationClass
{
SEL cat = @selector(class);
NSMethodSignature *sig = [NSString instanceMethodSignatureForSelector:cat];
NSInvocation *invocation=[invocationClass invocationWithMethodSignature:sig];
[invocation setTarget:@"Hello"];
[invocation setSelector:cat];
return invocation;
}
+testBasicSend:(Class)invocationClass
{
id invocationResult;
id invocation = [self testInvocationOfClass:invocationClass];
[invocation invoke];
[invocation getReturnValue:&invocationResult];
return invocationResult;
}
+(void)testBasicSendNSInvocation
{
IDEXPECT( [self testBasicSend:[NSInvocation class]], @"Hello World!", @"concating via NSInvocation");
}
+(void)testBasicSend
{
IDEXPECT( [self testBasicSend:[MPWFastInvocation class]], @"Hello World!", @"concating via MPWFastInvocation");
}
//#if FULL_MPWFOUNDATION && !WINDOWS && !LINUX
+(double)ratioOfNSInvocationToMPWFastInvocationSpeed:(BOOL)caching
{
int i;
MPWFastInvocation* fast=[self simpleSendWithInvocationOfClass:[MPWFastInvocation class]];
id slow=[self simpleSendWithInvocationOfClass:[NSInvocation class]];
IMP0 invoke = (IMP0)[slow methodForSelector:@selector(resultOfInvoking)];
MPWRusage* slowStart=[MPWRusage current];
#define SEND_COUNT 10000
for (i=0;i<SEND_COUNT;i++) {
invoke( slow, @selector(resultOfInvoking));
}
MPWRusage* slowTime=[MPWRusage timeRelativeTo:slowStart];
// NSLog(@"slowTime: %@",slowTime);
// NSLog(@"slowTime: %g",(double)[slowTime userMicroseconds]);
[fast setUseCaching:caching];
MPWRusage* fastStart=[MPWRusage current];
for (i=0;i<SEND_COUNT;i++) {
INVOKE(fast);
}
MPWRusage* fastTime=[MPWRusage timeRelativeTo:fastStart];
// NSLog(@"fastTime: %@",fastTime);
// NSLog(@"fastTime: %g",(double)[fastTime userMicroseconds]);
double ratio = (double)[slowTime cpu] / (double)[fastTime cpu];
NSLog(@"ratio of %@cached MPWFastInvocation compared to NSInvocation: %g",caching?@"":@"un",ratio);
return ratio;
}
+(void)testFasterThanNSInvocationWithoutCaching
{
double ratio = [self ratioOfNSInvocationToMPWFastInvocationSpeed:NO];
NSAssert2( ratio > 5 ,@"ratio of non-cached fast invocation to normal invocation %g < %d (this may fluctuate)",
ratio,5);
(void)ratio;
}
+(void)testFasterThanNSInvocationWitCaching
{
double ratio = [self ratioOfNSInvocationToMPWFastInvocationSpeed:YES];
NSAssert2( ratio > 12 ,@"ratio of cached fast invocation to normal invocation %g < %d (this may fluctuate)",
ratio,12); // actual up to factor 18
(void)ratio;
}
+(void)testCachingFasterThanNonCaching
{
double ratio1 = [self ratioOfNSInvocationToMPWFastInvocationSpeed:YES];
double ratio2 = [self ratioOfNSInvocationToMPWFastInvocationSpeed:NO];
NSAssert2( ratio1 > ratio2 ,@"cached ratio (%g) slower than noncaced %g",
ratio1,ratio2);
(void)ratio1;
(void)ratio2;
}
#undef SEND_COUNT
#define SEND_COUNT 10000
+(void)testCachedInvocationFasterThanMessaging
{
MPWFastInvocation* fast=[self simpleSendWithInvocationOfClass:[MPWFastInvocation class]];
int i;
MPWRusage* slowStart=[MPWRusage current];
for (i=0;i<SEND_COUNT;i++) {
[@"Hello" length];
}
MPWRusage* slowTime=[MPWRusage timeRelativeTo:slowStart];
[fast setUseCaching:YES];
INVOKE(fast);
MPWRusage* fastStart=[MPWRusage current];
for (i=0;i<SEND_COUNT;i++) {
INVOKE(fast);
}
MPWRusage* fastTime=[MPWRusage timeRelativeTo:fastStart];
double ratio = (double)[slowTime cpu] / (double)[fastTime cpu];
// NSLog(@"cached invocation (%d) vs. plain message send (%d): %g x speed of normal message send",(int)[fastTime cpu],(int)[slowTime cpu],ratio);
NSAssert2( ratio > 0.2 ,@"ratio of cached fast invocation to normal message send %g < %g",
ratio,0.2);
}
//#endif
+(void)testIntArgAndReturnValue
{
MPWFastInvocation *invocation = [self invocation];
long positionArg=3;
[invocation setSelector:@selector(characterAtIndex:)];
[invocation setTarget:@"Hello World!"];
[invocation setArgument:&positionArg atIndex:2];
// charAtThree=(NSInteger)[invocation resultOfInvoking];
INTEXPECT( (NSInteger)[invocation resultOfInvoking], 'l', @"character at three");
positionArg=4;
[invocation setArgument:&positionArg atIndex:2];
INTEXPECT( (NSInteger)[invocation resultOfInvoking], 'o', @"character at four");
}
+(void)testEvaluateOn
{
NSDictionary *d=@{@"a": @(2), @"b": @(10) };
MPWFastInvocation *inv = [d invocationForSelector:@selector(objectForKey:)];
IDEXPECT( ([inv evaluateOnObject:d parameters:@[ @"a"]]), @(2), @"evaluate");
}
+testSelectors
{
return [NSArray arrayWithObjects:
@"testBasicSendNSInvocation",
@"testBasicSend",
@"testFasterThanNSInvocationWithoutCaching",
@"testFasterThanNSInvocationWitCaching",
// @"testCachingFasterThanNonCaching",
@"testCachedInvocationFasterThanMessaging",
@"testIntArgAndReturnValue",
@"testEvaluateOn",
nil];
}
@end
@implementation NSObject(invocations)
-invocationForSelector:(SEL)selector
{
MPWFastInvocation *inv = [MPWFastInvocation invocation];
[inv setSelector:selector];
[inv setIMP:[self methodForSelector:selector]];
[inv setUseCaching:NO];
return inv;
}
+instanceInvocationForSelector:(SEL)selector
{
MPWFastInvocation *inv = [MPWFastInvocation invocation];
[inv setSelector:selector];
[inv setIMP:[self instanceMethodForSelector:selector]];
[inv setUseCaching:NO];
return inv;
}
@end