Changeset 223860 in webkit
- Timestamp:
- Oct 23, 2017 3:37:33 PM (6 years ago)
- Location:
- trunk/Source/WebCore
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebCore/ChangeLog
r223858 r223860 1 2017-10-23 Wenson Hsieh <wenson_hsieh@apple.com> 2 3 [iOS] DocumentWriter::createDocument can spend ~100ms unnecessarily converting image UTIs to MIME types 4 https://bugs.webkit.org/show_bug.cgi?id=178618 5 <rdar://problem/35108852> 6 7 Reviewed by Said Abou-Hallawa. 8 9 Currently, in setting up a new Document, DocumentWriter::createDocument() always asks whether or not the 10 Document should be a PDF document by calling MIMETypeRegistry::isPDFMIMEType(), which forces lazy initialization 11 of every MIME type dictionary (e.g. image types, PDF types, JavaScript types, etc.). As evidenced by traces, 12 this can be an expensive operation on certain devices. 13 14 This patch implements two optimizations. First, we refactor the initializeSupportedImageMIMETypes() helper to 15 stop asking for MIMETypeForImageSourceType for each of the supported UTIs. This is because the known MIME types 16 corresponding to these hard-coded UTI types is a fixed set anyways, so we can simply iterate over a constant 17 array of MIME types and populate the supported image (and image resource) types. Also, add assertions to ensure 18 that we keep allowed image MIME types in sync with allowed image UTIs. 19 20 The second optimization removes initializeMIMETypeRegistry() altogether in favor of calling just the 21 initialize*MIMETypes() functions needed to ensure the information required. For instance, getPDFMIMETypes() 22 currently calls initializeMIMETypeRegistry() if the pdfMIMETypes dictionary doesn't exist, when it really only 23 needs to ensure that the pdfMIMETypes is initialized, for which initializePDFMIMETypes() is sufficient. 24 25 * platform/MIMETypeRegistry.cpp: 26 (WebCore::initializeSupportedImageMIMETypes): 27 (WebCore::initializeSupportedJavaScriptMIMETypes): 28 (WebCore::initializePDFMIMETypes): 29 (WebCore::initializeSupportedNonImageMimeTypes): 30 (WebCore::initializeUnsupportedTextMIMETypes): 31 32 Move MIME type dictionary creation into initialize*MIMETypes() helpers. Additionally, remove 33 initializePDFAndPostScriptMIMETypes, which is no longer necessary. 34 35 (WebCore::MIMETypeRegistry::isSupportedImageMIMEType): 36 (WebCore::MIMETypeRegistry::isSupportedImageResourceMIMEType): 37 (WebCore::MIMETypeRegistry::isSupportedJavaScriptMIMEType): 38 (WebCore::MIMETypeRegistry::isSupportedNonImageMIMEType): 39 (WebCore::MIMETypeRegistry::isUnsupportedTextMIMEType): 40 (WebCore::MIMETypeRegistry::isPDFOrPostScriptMIMEType): 41 42 Tweak to check that the type isPDFMIMEType(), or that it's otherwise "application/postscript". 43 44 (WebCore::MIMETypeRegistry::isPDFMIMEType): 45 (WebCore::MIMETypeRegistry::getSupportedImageMIMETypes): 46 (WebCore::MIMETypeRegistry::getSupportedImageResourceMIMETypes): 47 (WebCore::MIMETypeRegistry::getSupportedNonImageMIMETypes): 48 (WebCore::MIMETypeRegistry::getPDFMIMETypes): 49 (WebCore::MIMETypeRegistry::getUnsupportedTextMIMETypes): 50 51 Call only the relevant MIME type initializers when needed. 52 53 (WebCore::initializePostScriptMIMETypes): Deleted. 54 (WebCore::initializeMIMETypeRegistry): Deleted. 55 (WebCore::MIMETypeRegistry::getPDFAndPostScriptMIMETypes): Deleted. 56 57 Remove an unused and unexported function. 58 59 * platform/MIMETypeRegistry.h: 60 1 61 2017-10-23 Andy Estes <aestes@apple.com> 2 62 -
trunk/Source/WebCore/platform/MIMETypeRegistry.cpp
r221778 r223860 37 37 #include "ImageSourceCG.h" 38 38 #include "UTIRegistry.h" 39 #include "UTIUtilities.h" 39 40 #include <wtf/RetainPtr.h> 40 41 #endif … … 61 62 static HashSet<String, ASCIICaseInsensitiveHash>* supportedMediaMIMETypes; 62 63 static HashSet<String, ASCIICaseInsensitiveHash>* pdfMIMETypes; 63 static HashSet<String, ASCIICaseInsensitiveHash>* pdfAndPostScriptMIMETypes;64 64 static HashSet<String, ASCIICaseInsensitiveHash>* unsupportedTextMIMETypes; 65 65 … … 67 67 { 68 68 #if USE(CG) 69 HashSet<String>& imageUTIs = allowedImageUTIs(); 70 for (auto& imageUTI : imageUTIs) { 71 String mimeType = MIMETypeForImageSourceType(imageUTI); 69 supportedImageResourceMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>; 70 supportedImageMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>; 71 72 // This represents the subset of allowed image UTIs for which CoreServices has a corresponding MIME type. Keep this in sync with allowedImageUTIs(). 73 static const char* const allowedImageMIMETypes[] = { "image/tiff", "image/gif", "image/jpeg", "image/vnd.microsoft.icon", "image/jp2", "image/png", "image/bmp" }; 74 for (auto& mimeType : allowedImageMIMETypes) { 75 supportedImageMIMETypes->add(ASCIILiteral { mimeType }); 76 supportedImageResourceMIMETypes->add(ASCIILiteral { mimeType }); 77 } 78 79 #ifndef NDEBUG 80 for (auto& uti : allowedImageUTIs()) { 81 auto mimeType = MIMETypeForImageSourceType(uti); 72 82 if (!mimeType.isEmpty()) { 73 supportedImageMIMETypes->add(mimeType);74 supportedImageResourceMIMETypes->add(mimeType);83 ASSERT(supportedImageMIMETypes->contains(mimeType)); 84 ASSERT(supportedImageResourceMIMETypes->contains(mimeType)); 75 85 } 76 86 } 77 87 78 // On Tiger and Leopard, com.microsoft.bmp doesn't have a MIME type in the registry.79 supportedImageMIMETypes->add("image/bmp");80 supportedImageResourceMIMETypes->add("image/bmp"); 88 for (auto& mime : *supportedImageMIMETypes) 89 ASSERT_UNUSED(mime, allowedImageUTIs().contains(UTIFromMIMEType(mime))); 90 #endif 81 91 82 92 // Favicons don't have a MIME type in the registry either. 83 supportedImageMIMETypes->add("image/vnd.microsoft.icon");84 93 supportedImageMIMETypes->add("image/x-icon"); 85 supportedImageResourceMIMETypes->add("image/vnd.microsoft.icon");86 94 supportedImageResourceMIMETypes->add("image/x-icon"); 87 95 … … 89 97 supportedImageMIMETypes->add("image/pjpeg"); 90 98 supportedImageResourceMIMETypes->add("image/pjpeg"); 91 92 // We don't want to try to treat all binary data as an image93 supportedImageMIMETypes->remove("application/octet-stream");94 supportedImageResourceMIMETypes->remove("application/octet-stream");95 96 // Don't treat pdf/postscript as images directly97 supportedImageMIMETypes->remove("application/pdf");98 supportedImageMIMETypes->remove("application/postscript");99 99 100 100 #if PLATFORM(IOS) … … 204 204 "text/x-ecmascript" 205 205 }; 206 207 supportedJavaScriptMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>; 206 208 for (auto* type : types) 207 209 supportedJavaScriptMIMETypes->add(type); … … 214 216 "text/pdf" 215 217 }; 218 219 pdfMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>; 216 220 for (auto& type : types) 217 221 pdfMIMETypes->add(type); 218 }219 220 static void initializePostScriptMIMETypes()221 {222 pdfAndPostScriptMIMETypes->add("application/postscript");223 222 } 224 223 … … 248 247 }; 249 248 249 if (!supportedJavaScriptMIMETypes) 250 initializeSupportedJavaScriptMIMETypes(); 251 252 supportedNonImageMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash> { *supportedJavaScriptMIMETypes }; 250 253 for (auto& type : types) 251 254 supportedNonImageMIMETypes->add(type); … … 416 419 #endif 417 420 }; 421 422 unsupportedTextMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>; 418 423 for (auto& type : types) 419 424 unsupportedTextMIMETypes->add(ASCIILiteral { type }); 420 }421 422 static void initializeMIMETypeRegistry()423 {424 supportedJavaScriptMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>;425 initializeSupportedJavaScriptMIMETypes();426 427 supportedNonImageMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>(*supportedJavaScriptMIMETypes);428 initializeSupportedNonImageMimeTypes();429 430 supportedImageResourceMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>;431 supportedImageMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>;432 initializeSupportedImageMIMETypes();433 434 pdfMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>;435 initializePDFMIMETypes();436 437 pdfAndPostScriptMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>(*pdfMIMETypes);438 initializePostScriptMIMETypes();439 440 unsupportedTextMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>;441 initializeUnsupportedTextMIMETypes();442 425 } 443 426 … … 459 442 return false; 460 443 if (!supportedImageMIMETypes) 461 initialize MIMETypeRegistry();444 initializeSupportedImageMIMETypes(); 462 445 return supportedImageMIMETypes->contains(getNormalizedMIMEType(mimeType)); 463 446 } … … 473 456 return false; 474 457 if (!supportedImageResourceMIMETypes) 475 initialize MIMETypeRegistry();458 initializeSupportedImageMIMETypes(); 476 459 return supportedImageResourceMIMETypes->contains(getNormalizedMIMEType(mimeType)); 477 460 } … … 493 476 return false; 494 477 if (!supportedJavaScriptMIMETypes) 495 initialize MIMETypeRegistry();478 initializeSupportedNonImageMimeTypes(); 496 479 return supportedJavaScriptMIMETypes->contains(mimeType); 497 480 } … … 538 521 return false; 539 522 if (!supportedNonImageMIMETypes) 540 initialize MIMETypeRegistry();523 initializeSupportedNonImageMimeTypes(); 541 524 return supportedNonImageMIMETypes->contains(mimeType); 542 525 } … … 561 544 return false; 562 545 if (!unsupportedTextMIMETypes) 563 initialize MIMETypeRegistry();546 initializeUnsupportedTextMIMETypes(); 564 547 return unsupportedTextMIMETypes->contains(mimeType); 565 548 } … … 619 602 bool MIMETypeRegistry::isPDFOrPostScriptMIMEType(const String& mimeType) 620 603 { 621 if (mimeType.isEmpty()) 622 return false; 623 if (!pdfAndPostScriptMIMETypes) 624 initializeMIMETypeRegistry(); 625 return pdfAndPostScriptMIMETypes->contains(mimeType); 604 return isPDFMIMEType(mimeType) || mimeType == "application/postscript"; 626 605 } 627 606 … … 631 610 return false; 632 611 if (!pdfMIMETypes) 633 initialize MIMETypeRegistry();612 initializePDFMIMETypes(); 634 613 return pdfMIMETypes->contains(mimeType); 635 614 } … … 652 631 { 653 632 if (!supportedImageMIMETypes) 654 initialize MIMETypeRegistry();633 initializeSupportedImageMIMETypes(); 655 634 return *supportedImageMIMETypes; 656 635 } … … 659 638 { 660 639 if (!supportedImageResourceMIMETypes) 661 initialize MIMETypeRegistry();640 initializeSupportedImageMIMETypes(); 662 641 return *supportedImageResourceMIMETypes; 663 642 } … … 673 652 { 674 653 if (!supportedNonImageMIMETypes) 675 initialize MIMETypeRegistry();654 initializeSupportedNonImageMimeTypes(); 676 655 return *supportedNonImageMIMETypes; 677 656 } … … 688 667 { 689 668 if (!pdfMIMETypes) 690 initialize MIMETypeRegistry();669 initializePDFMIMETypes(); 691 670 return *pdfMIMETypes; 692 671 } 693 672 694 HashSet<String, ASCIICaseInsensitiveHash>& MIMETypeRegistry::getPDFAndPostScriptMIMETypes()695 {696 if (!pdfAndPostScriptMIMETypes)697 initializeMIMETypeRegistry();698 return *pdfAndPostScriptMIMETypes;699 }700 701 673 HashSet<String, ASCIICaseInsensitiveHash>& MIMETypeRegistry::getUnsupportedTextMIMETypes() 702 674 { 703 675 if (!unsupportedTextMIMETypes) 704 initialize MIMETypeRegistry();676 initializeUnsupportedTextMIMETypes(); 705 677 return *unsupportedTextMIMETypes; 706 678 } -
trunk/Source/WebCore/platform/MIMETypeRegistry.h
r221778 r223860 112 112 WEBCORE_EXPORT static HashSet<String, ASCIICaseInsensitiveHash>& getSupportedMediaMIMETypes(); 113 113 WEBCORE_EXPORT static HashSet<String, ASCIICaseInsensitiveHash>& getPDFMIMETypes(); 114 static HashSet<String, ASCIICaseInsensitiveHash>& getPDFAndPostScriptMIMETypes();115 114 WEBCORE_EXPORT static HashSet<String, ASCIICaseInsensitiveHash>& getUnsupportedTextMIMETypes(); 116 115
Note: See TracChangeset
for help on using the changeset viewer.