Differences From Artifact [c329ebfb71]:
- File src/OFXMLParser.m — part of check-in [8892ae9fcc] at 2012-07-12 01:28:46 on branch trunk — Don't access isa directly. (user: js, size: 25231) [annotate] [blame] [check-ins using]
To Artifact [5b68538ff6]:
- File
src/OFXMLParser.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: 25000) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
26 27 28 29 30 31 32 | #import "OFString.h" #import "OFArray.h" #import "OFDictionary.h" #import "OFDataArray.h" #import "OFXMLAttribute.h" #import "OFStream.h" #import "OFFile.h" | < > > | | | 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 | #import "OFString.h" #import "OFArray.h" #import "OFDictionary.h" #import "OFDataArray.h" #import "OFXMLAttribute.h" #import "OFStream.h" #import "OFFile.h" #import "OFInitializationFailedException.h" #import "OFMalformedXMLException.h" #import "OFUnboundNamespaceException.h" #import "autorelease.h" typedef void (*state_function)(id, SEL, const char*, size_t*, size_t*); static SEL selectors[OF_XMLPARSER_NUM_STATES]; static state_function lookupTable[OF_XMLPARSER_NUM_STATES]; static OF_INLINE void cache_append(OFDataArray *cache, const char *string, of_string_encoding_t encoding, size_t length) { if (OF_LIKELY(encoding == OF_STRING_ENCODING_UTF_8)) [cache addItemsFromCArray: string count: length]; else { void *pool = objc_autoreleasePoolPush(); OFString *tmp = [OFString stringWithCString: string encoding: encoding length: length]; [cache addItemsFromCArray: [tmp UTF8String] count: [tmp UTF8StringLength]]; objc_autoreleasePoolPop(pool); } } static OFString* transform_string(OFDataArray *cache, size_t cut, BOOL unescape, OFObject <OFStringXMLUnescapingDelegate> *delegate) { |
︙ | ︙ | |||
179 180 181 182 183 184 185 | } - init { self = [super init]; @try { | | | | | 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 | } - init { self = [super init]; @try { void *pool; OFMutableDictionary *dict; cache = [[OFBigDataArray alloc] init]; previous = [[OFMutableArray alloc] init]; namespaces = [[OFMutableArray alloc] init]; attributes = [[OFMutableArray alloc] init]; pool = objc_autoreleasePoolPush(); dict = [OFMutableDictionary dictionaryWithKeysAndObjects: @"xml", @"http://www.w3.org/XML/1998/namespace", @"xmlns", @"http://www.w3.org/2000/xmlns/", nil]; [namespaces addObject: dict]; acceptProlog = YES; lineNumber = 1; encoding = OF_STRING_ENCODING_UTF_8; objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @throw e; } return self; } |
︙ | ︙ | |||
317 318 319 320 321 322 323 | if (buffer[*i] != '<') return; if ((length = *i - *last) > 0) cache_append(cache, buffer + *last, encoding, length); if ([cache count] > 0) { | < | < < | | | 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | if (buffer[*i] != '<') return; if ((length = *i - *last) > 0) cache_append(cache, buffer + *last, encoding, length); if ([cache count] > 0) { void *pool = objc_autoreleasePoolPush(); OFString *characters = transform_string(cache, 0, YES, self); [delegate parser: self foundCharacters: characters]; objc_autoreleasePoolPop(pool); } [cache removeAllItems]; *last = *i + 1; state = OF_XMLPARSER_TAG_OPENED; } |
︙ | ︙ | |||
472 473 474 475 476 477 478 | - (void)_parseInProcessingInstructionsWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { if (buffer[*i] == '?') level = 1; else if (level == 1 && buffer[*i] == '>') { | | | | | < < < < < < < | < < | > | | | 470 471 472 473 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 | - (void)_parseInProcessingInstructionsWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { if (buffer[*i] == '?') level = 1; else if (level == 1 && buffer[*i] == '>') { void *pool = objc_autoreleasePoolPush(); OFString *pi; cache_append(cache, buffer + *last, encoding, *i - *last); pi = transform_string(cache, 1, NO, nil); if ([pi isEqual: @"xml"] || [pi hasPrefix: @"xml "] || [pi hasPrefix: @"xml\t"] || [pi hasPrefix: @"xml\r"] || [pi hasPrefix: @"xml\n"]) if (![self _parseXMLProcessingInstructions: pi]) @throw [OFMalformedXMLException exceptionWithClass: [self class] parser: self]; [delegate parser: self foundProcessingInstructions: pi]; objc_autoreleasePoolPop(pool); [cache removeAllItems]; *last = *i + 1; state = OF_XMLPARSER_OUTSIDE_TAG; } else level = 0; } /* Inside a tag, no name yet */ - (void)_parseInTagNameWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { void *pool; const char *cacheCString, *tmp; size_t length, cacheLength; OFString *cacheString; if (buffer[*i] != ' ' && buffer[*i] != '\t' && buffer[*i] != '\n' && buffer[*i] != '\r' && buffer[*i] != '>' && buffer[*i] != '/') return; if ((length = *i - *last) > 0) cache_append(cache, buffer + *last, encoding, length); pool = objc_autoreleasePoolPush(); cacheCString = [cache cArray]; cacheLength = [cache count]; cacheString = [OFString stringWithUTF8String: cacheCString length: cacheLength]; if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) { name = [[OFString alloc] initWithUTF8String: tmp + 1 length: cacheLength - (tmp - cacheCString) - 1]; prefix = [[OFString alloc] initWithUTF8String: cacheCString length: tmp - cacheCString]; } else { name = [cacheString copy]; prefix = nil; } if (buffer[*i] == '>' || buffer[*i] == '/') { OFString *ns; ns = namespace_for_prefix(prefix, namespaces); if (prefix != nil && ns == nil) @throw [OFUnboundNamespaceException exceptionWithClass: [self class] prefix: prefix]; [delegate parser: self didStartElement: name withPrefix: prefix namespace: ns attributes: nil]; if (buffer[*i] == '/') { [delegate parser: self didEndElement: name withPrefix: prefix namespace: ns]; if ([previous count] == 0) finishedParsing = YES; } else [previous addObject: cacheString]; [name release]; [prefix release]; name = prefix = nil; state = (buffer[*i] == '/' ? OF_XMLPARSER_EXPECT_CLOSE : OF_XMLPARSER_OUTSIDE_TAG); } else state = OF_XMLPARSER_IN_TAG; if (buffer[*i] != '/') [namespaces addObject: [OFMutableDictionary dictionary]]; objc_autoreleasePoolPop(pool); [cache removeAllItems]; *last = *i + 1; } /* Inside a close tag, no name yet */ - (void)_parseInCloseTagNameWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { void *pool; const char *cacheCString, *tmp; size_t length, cacheLength; OFString *cacheString; OFString *ns; if (buffer[*i] != ' ' && buffer[*i] != '\t' && buffer[*i] != '\n' && buffer[*i] != '\r' && buffer[*i] != '>') return; if ((length = *i - *last) > 0) cache_append(cache, buffer + *last, encoding, length); pool = objc_autoreleasePoolPush(); cacheCString = [cache cArray]; cacheLength = [cache count]; cacheString = [OFString stringWithUTF8String: cacheCString length: cacheLength]; if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) { |
︙ | ︙ | |||
645 646 647 648 649 650 651 | prefix: prefix]; [delegate parser: self didEndElement: name withPrefix: prefix namespace: ns]; | | | | 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 | prefix: prefix]; [delegate parser: self didEndElement: name withPrefix: prefix namespace: ns]; objc_autoreleasePoolPop(pool); [namespaces removeLastObject]; [name release]; [prefix release]; name = prefix = nil; *last = *i + 1; state = (buffer[*i] == '>' ? OF_XMLPARSER_OUTSIDE_TAG : OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE); if ([previous count] == 0) finishedParsing = YES; } /* Inside a tag, name found */ - (void)_parseInTagWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { void *pool; OFString *ns; OFXMLAttribute **attributesObjects; size_t j, attributesCount; if (buffer[*i] != '>' && buffer[*i] != '/') { if (buffer[*i] != ' ' && buffer[*i] != '\t' && buffer[*i] != '\n' && buffer[*i] != '\r') { |
︙ | ︙ | |||
696 697 698 699 700 701 702 | exceptionWithClass: [self class] prefix: prefix]; for (j = 0; j < attributesCount; j++) resolve_attribute_namespace(attributesObjects[j], namespaces, self); | | | 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 | exceptionWithClass: [self class] prefix: prefix]; for (j = 0; j < attributesCount; j++) resolve_attribute_namespace(attributesObjects[j], namespaces, self); pool = objc_autoreleasePoolPush(); [delegate parser: self didStartElement: name withPrefix: prefix namespace: ns attributes: attributes]; |
︙ | ︙ | |||
721 722 723 724 725 726 727 | } else if (prefix != nil) { OFString *str = [OFString stringWithFormat: @"%@:%@", prefix, name]; [previous addObject: str]; } else [previous addObject: name]; | | | | | 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 | } else if (prefix != nil) { OFString *str = [OFString stringWithFormat: @"%@:%@", prefix, name]; [previous addObject: str]; } else [previous addObject: name]; objc_autoreleasePoolPop(pool); [name release]; [prefix release]; [attributes removeAllObjects]; name = prefix = nil; *last = *i + 1; state = (buffer[*i] == '/' ? OF_XMLPARSER_EXPECT_CLOSE : OF_XMLPARSER_OUTSIDE_TAG); } /* Looking for attribute name */ - (void)_parseInAttributeNameWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { void *pool; OFMutableString *cacheString; const char *cacheCString, *tmp; size_t length, cacheLength; if (buffer[*i] != '=') return; if ((length = *i - *last) > 0) cache_append(cache, buffer + *last, encoding, length); pool = objc_autoreleasePoolPush(); cacheString = [OFMutableString stringWithUTF8String: [cache cArray] length: [cache count]]; [cacheString deleteEnclosingWhitespaces]; /* Prevent a useless copy later */ [cacheString makeImmutable]; |
︙ | ︙ | |||
773 774 775 776 777 778 779 | initWithUTF8String: cacheCString length: tmp - cacheCString]; } else { attributeName = [cacheString copy]; attributePrefix = nil; } | | | 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 | initWithUTF8String: cacheCString length: tmp - cacheCString]; } else { attributeName = [cacheString copy]; attributePrefix = nil; } objc_autoreleasePoolPop(pool); [cache removeAllItems]; *last = *i + 1; state = OF_XMLPARSER_EXPECT_DELIM; } |
︙ | ︙ | |||
805 806 807 808 809 810 811 | } /* Looking for attribute value */ - (void)_parseInAttributeValueWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { | | | | | 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 | } /* Looking for attribute value */ - (void)_parseInAttributeValueWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { void *pool; OFString *attributeValue; size_t length; if (buffer[*i] != delimiter) return; if ((length = *i - *last) > 0) cache_append(cache, buffer + *last, encoding, length); pool = objc_autoreleasePoolPush(); attributeValue = transform_string(cache, 0, YES, self); if (attributePrefix == nil && [attributeName isEqual: @"xmlns"]) [[namespaces lastObject] setObject: attributeValue forKey: @""]; if ([attributePrefix isEqual: @"xmlns"]) [[namespaces lastObject] setObject: attributeValue forKey: attributeName]; [attributes addObject: [OFXMLAttribute attributeWithName: attributeName namespace: attributePrefix stringValue: attributeValue]]; objc_autoreleasePoolPop(pool); [cache removeAllItems]; [attributeName release]; [attributePrefix release]; attributeName = attributePrefix = nil; *last = *i + 1; |
︙ | ︙ | |||
926 927 928 929 930 931 932 | state = OF_XMLPARSER_IN_CDATA_2; } - (void)_parseInCDATA2WithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { | | | | | 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 | state = OF_XMLPARSER_IN_CDATA_2; } - (void)_parseInCDATA2WithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { void *pool; OFString *CDATA; if (buffer[*i] != '>') { state = OF_XMLPARSER_IN_CDATA_1; level = (buffer[*i] == ']' ? 1 : 0); return; } pool = objc_autoreleasePoolPush(); cache_append(cache, buffer + *last, encoding, *i - *last); CDATA = transform_string(cache, 2, NO, nil); [delegate parser: self foundCDATA: CDATA]; objc_autoreleasePoolPop(pool); [cache removeAllItems]; *last = *i + 1; state = OF_XMLPARSER_OUTSIDE_TAG; } |
︙ | ︙ | |||
983 984 985 986 987 988 989 | state = OF_XMLPARSER_IN_COMMENT_2; } - (void)_parseInComment2WithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { | | | | | 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 | state = OF_XMLPARSER_IN_COMMENT_2; } - (void)_parseInComment2WithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { void *pool; OFString *comment; if (buffer[*i] != '>') @throw [OFMalformedXMLException exceptionWithClass: [self class] parser: self]; pool = objc_autoreleasePoolPush(); cache_append(cache, buffer + *last, encoding, *i - *last); comment = transform_string(cache, 2, NO, nil); [delegate parser: self foundComment: comment]; objc_autoreleasePoolPop(pool); [cache removeAllItems]; *last = *i + 1; state = OF_XMLPARSER_OUTSIDE_TAG; } |
︙ | ︙ |