Differences From Artifact [4a522c344b]:
- File src/OFDictionary.m — part of check-in [8892ae9fcc] at 2012-07-12 01:28:46 on branch trunk — Don't access isa directly. (user: js, size: 12883) [annotate] [blame] [check-ins using]
To Artifact [5774d8a182]:
- File
src/OFDictionary.m
— part of check-in
[1255f3a11a]
at
2012-08-10 20:08:24
on branch trunk
— Directly use the runtime's autorelease pools.
This greatly improves performance, as it gets rid of the overhead of
OFAutoreleasePool. (user: js, size: 12775) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
19 20 21 22 23 24 25 | #include <assert.h> #import "OFDictionary.h" #import "OFDictionary_hashtable.h" #import "OFArray.h" #import "OFString.h" #import "OFXMLElement.h" | < > > | 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <assert.h> #import "OFDictionary.h" #import "OFDictionary_hashtable.h" #import "OFArray.h" #import "OFString.h" #import "OFXMLElement.h" #import "OFNotImplementedException.h" #import "autorelease.h" static struct { Class isa; } placeholder; @interface OFDictionary_placeholder: OFDictionary @end |
︙ | ︙ | |||
280 281 282 283 284 285 286 | { return [[OFMutableDictionary alloc] initWithDictionary: self]; } - (BOOL)isEqual: (id)object { OFDictionary *otherDictionary; | | | | | | | | | | | | < < | | < < | | 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 | { return [[OFMutableDictionary alloc] initWithDictionary: self]; } - (BOOL)isEqual: (id)object { OFDictionary *otherDictionary; void *pool; OFEnumerator *enumerator; id key; if (![object isKindOfClass: [OFDictionary class]]) return NO; otherDictionary = object; if ([otherDictionary count] != [self count]) return NO; pool = objc_autoreleasePoolPush(); enumerator = [self keyEnumerator]; while ((key = [enumerator nextObject]) != nil) { id object = [otherDictionary objectForKey: key]; if (object == nil || ![object isEqual: [self objectForKey: key]]) { objc_autoreleasePoolPop(pool); return NO; } } objc_autoreleasePoolPop(pool); return YES; } - (BOOL)containsObject: (id)object { void *pool = objc_autoreleasePoolPush(); OFEnumerator *enumerator = [self objectEnumerator]; id currentObject; while ((currentObject = [enumerator nextObject]) != nil) { if ([currentObject isEqual: object]) { objc_autoreleasePoolPop(pool); return YES; } } objc_autoreleasePoolPop(pool); return NO; } - (BOOL)containsObjectIdenticalTo: (id)object { void *pool = objc_autoreleasePoolPush(); OFEnumerator *enumerator = [self objectEnumerator]; id currentObject; while ((currentObject = [enumerator nextObject]) != nil) { if (currentObject == object) { objc_autoreleasePoolPop(pool); return YES; } } objc_autoreleasePoolPop(pool); return NO; } - (OFArray*)allKeys { void *pool = objc_autoreleasePoolPush(); id *keys = [self allocMemoryWithSize: sizeof(id) count: [self count]]; OFArray *ret; OFEnumerator *enumerator; id key; size_t i = 0; enumerator = [self keyEnumerator]; while ((key = [enumerator nextObject]) != nil) keys[i++] = key; assert(i == [self count]); objc_autoreleasePoolPop(pool); @try { ret = [OFArray arrayWithObjects: keys count: [self count]]; } @finally { [self freeMemory: keys]; } return ret; } - (OFArray*)allObjects { void *pool = objc_autoreleasePoolPush(); id *objects = [self allocMemoryWithSize: sizeof(id) count: [self count]]; OFArray *ret; OFEnumerator *enumerator; id object; size_t i = 0; enumerator = [self objectEnumerator]; while ((object = [enumerator nextObject]) != nil) objects[i++] = object; assert(i == [self count]); objc_autoreleasePoolPop(pool); @try { ret = [OFArray arrayWithObjects: objects count: [self count]]; } @finally { [self freeMemory: objects]; } |
︙ | ︙ | |||
477 478 479 480 481 482 483 | return new; } #endif - (uint32_t)hash { | | | | | < < > > | | < | < < > | | | | | < < > > | < < > > | 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 | return new; } #endif - (uint32_t)hash { void *pool = objc_autoreleasePoolPush(); OFEnumerator *enumerator = [self keyEnumerator]; id key; uint32_t hash = 0; while ((key = [enumerator nextObject]) != nil) { hash += [key hash]; hash += [[self objectForKey: key] hash]; } objc_autoreleasePoolPop(pool); return hash; } - (OFString*)description { OFMutableString *ret; void *pool; OFEnumerator *keyEnumerator, *objectEnumerator; id key, object; size_t i, count = [self count]; if (count == 0) return @"{}"; ret = [OFMutableString stringWithString: @"{\n"]; pool = objc_autoreleasePoolPush(); keyEnumerator = [self keyEnumerator]; objectEnumerator = [self objectEnumerator]; i = 0; while ((key = [keyEnumerator nextObject]) != nil && (object = [objectEnumerator nextObject]) != nil) { void *pool2 = objc_autoreleasePoolPush(); [ret appendString: [key description]]; [ret appendString: @" = "]; [ret appendString: [object description]]; if (++i < count) [ret appendString: @";\n"]; objc_autoreleasePoolPop(pool2); } [ret replaceOccurrencesOfString: @"\n" withString: @"\n\t"]; [ret appendString: @";\n}"]; [ret makeImmutable]; objc_autoreleasePoolPop(pool); return ret; } - (OFXMLElement*)XMLElementBySerializing { void *pool = objc_autoreleasePoolPush(); OFXMLElement *element; OFEnumerator *keyEnumerator, *objectEnumerator; id key, object; if ([self isKindOfClass: [OFMutableDictionary class]]) element = [OFXMLElement elementWithName: @"OFMutableDictionary" namespace: OF_SERIALIZATION_NS]; else element = [OFXMLElement elementWithName: @"OFDictionary" namespace: OF_SERIALIZATION_NS]; keyEnumerator = [self keyEnumerator]; objectEnumerator = [self objectEnumerator]; while ((key = [keyEnumerator nextObject]) != nil && (object = [objectEnumerator nextObject]) != nil) { void *pool2 = objc_autoreleasePoolPush(); OFXMLElement *keyElement, *objectElement; keyElement = [OFXMLElement elementWithName: @"key" namespace: OF_SERIALIZATION_NS]; [keyElement addChild: [key XMLElementBySerializing]]; objectElement = [OFXMLElement elementWithName: @"object" namespace: OF_SERIALIZATION_NS]; [objectElement addChild: [object XMLElementBySerializing]]; [element addChild: keyElement]; [element addChild: objectElement]; objc_autoreleasePoolPop(pool2); } [element retain]; objc_autoreleasePoolPop(pool); return [element autorelease]; } - (OFString*)JSONRepresentation { OFMutableString *JSON = [OFMutableString stringWithString: @"{"]; void *pool = objc_autoreleasePoolPush(); OFEnumerator *keyEnumerator = [self keyEnumerator]; OFEnumerator *objectEnumerator = [self objectEnumerator]; size_t i = 0, count = [self count]; OFString *key; OFString *object; while ((key = [keyEnumerator nextObject]) != nil && (object = [objectEnumerator nextObject]) != nil) { void *pool2 = objc_autoreleasePoolPush(); [JSON appendString: [key JSONRepresentation]]; [JSON appendString: @":"]; [JSON appendString: [object JSONRepresentation]]; if (++i < count) [JSON appendString: @","]; objc_autoreleasePoolPop(pool2); } [JSON appendString: @"}"]; [JSON makeImmutable]; objc_autoreleasePoolPop(pool); return JSON; } @end |