Title: [294984] trunk/Source/WebCore/html/InputType.cpp
Revision
294984
Author
[email protected]
Date
2022-05-27 21:22:16 -0700 (Fri, 27 May 2022)

Log Message

Optimize InputType::create() by reducing calls to AtomString::convertToASCIILowercase()
https://bugs.webkit.org/show_bug.cgi?id=241043

Reviewed by Cameron McCormack.

In the common case, InputType::create() gets called with a `type` string that
is already lowercase. As a result, we would iterate over the whole string
unnecessarily under convertToASCIILowercase() to verify that it is already
lowercase, just to look up the type in a HashMap.

To avoid this, we now trying to look up the type in the HashMap directly, and
only fallback to calling convertToASCIILowercase() in the rare case where the
look up failed.

On my Apple Silicon MacBook Pro, this reduces the number of samples under
convertToASCIILowercase() from 17 to 4. Also reduces the number of samples
under InputType::create() from 21 to 12.

* Source/WebCore/html/InputType.cpp:
(WebCore::createInputTypeFactoryMap):
(WebCore::findFactory):
(WebCore::InputType::create):

Canonical link: https://commits.webkit.org/251088@main

Modified Paths

Diff

Modified: trunk/Source/WebCore/html/InputType.cpp (294983 => 294984)


--- trunk/Source/WebCore/html/InputType.cpp	2022-05-28 04:10:06 UTC (rev 294983)
+++ trunk/Source/WebCore/html/InputType.cpp	2022-05-28 04:22:16 UTC (rev 294984)
@@ -131,6 +131,7 @@
         { nullptr, &InputTypeNames::search, &createInputType<SearchInputType> },
         { nullptr, &InputTypeNames::submit, &createInputType<SubmitInputType> },
         { nullptr, &InputTypeNames::telephone, &createInputType<TelephoneInputType> },
+        { nullptr, &InputTypeNames::text, &createInputType<TextInputType> },
 #if ENABLE(INPUT_TYPE_TIME)
         { &Settings::inputTypeTimeEnabled, &InputTypeNames::time, &createInputType<TimeInputType> },
 #endif
@@ -138,7 +139,6 @@
 #if ENABLE(INPUT_TYPE_WEEK)
         { &Settings::inputTypeWeekEnabled, &InputTypeNames::week, &createInputType<WeekInputType> },
 #endif
-        // No need to register "text" because it is the default type.
     };
 
     InputTypeFactoryMap map;
@@ -147,12 +147,20 @@
     return map;
 }
 
+static inline std::pair<InputTypeConditionalFunction, InputTypeFactoryFunction> findFactory(const AtomString& typeName)
+{
+    static NeverDestroyed factoryMap = createInputTypeFactoryMap();
+    auto factory = factoryMap.get().get(typeName);
+    if (UNLIKELY(!factory.second))
+        factory = factoryMap.get().get(typeName.convertToASCIILowercase());
+    return factory;
+}
+
 Ref<InputType> InputType::create(HTMLInputElement& element, const AtomString& typeName)
 {
     if (!typeName.isEmpty()) {
-        static NeverDestroyed factoryMap = createInputTypeFactoryMap();
-        auto&& [conditional, factory] = factoryMap.get().get(typeName.convertToASCIILowercase());
-        if (factory && (!conditional || std::invoke(conditional, element.document().settings())))
+        auto [conditional, factory] = findFactory(typeName);
+        if (LIKELY(factory && (!conditional || std::invoke(conditional, element.document().settings()))))
             return factory(element);
     }
     return adoptRef(*new TextInputType(element));
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to