mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 20:52:45 +00:00 
			
		
		
		
	LibWeb: Add cached global attribute name FlyStrings
Instead of creating extremely common FlyStrings like "id" and "class"
on demand every time they are needed, we now have AttributeNames.h,
which provides Web::HTML::AttributeNames::{id,class_}
This avoids a bunch of string allocations during selector matching.
			
			
This commit is contained in:
		
							parent
							
								
									5069d380a8
								
							
						
					
					
						commit
						82444048de
					
				
					 7 changed files with 102 additions and 5 deletions
				
			
		|  | @ -31,6 +31,7 @@ | ||||||
| #include <LibJS/Runtime/PrimitiveString.h> | #include <LibJS/Runtime/PrimitiveString.h> | ||||||
| #include <LibJS/Runtime/Value.h> | #include <LibJS/Runtime/Value.h> | ||||||
| #include <LibWeb/Bindings/ElementWrapper.h> | #include <LibWeb/Bindings/ElementWrapper.h> | ||||||
|  | #include <LibWeb/DOM/AttributeNames.h> | ||||||
| #include <LibWeb/DOM/Element.h> | #include <LibWeb/DOM/Element.h> | ||||||
| 
 | 
 | ||||||
| namespace Web { | namespace Web { | ||||||
|  | @ -131,7 +132,7 @@ void ElementWrapper::inner_html_setter(JS::Interpreter& interpreter, JS::Value v | ||||||
| JS::Value ElementWrapper::id_getter(JS::Interpreter& interpreter) | JS::Value ElementWrapper::id_getter(JS::Interpreter& interpreter) | ||||||
| { | { | ||||||
|     if (auto* impl = impl_from(interpreter)) |     if (auto* impl = impl_from(interpreter)) | ||||||
|         return JS::js_string(interpreter, impl->attribute("id")); |         return JS::js_string(interpreter, impl->attribute(HTML::AttributeNames::id)); | ||||||
|     return {}; |     return {}; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -141,7 +142,7 @@ void ElementWrapper::id_setter(JS::Interpreter& interpreter, JS::Value value) | ||||||
|         auto string = value.to_string(interpreter); |         auto string = value.to_string(interpreter); | ||||||
|         if (interpreter.exception()) |         if (interpreter.exception()) | ||||||
|             return; |             return; | ||||||
|         impl->set_attribute("id", string); |         impl->set_attribute(HTML::AttributeNames::id, string); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -25,6 +25,7 @@ set(SOURCES | ||||||
|     CSS/StyleRule.cpp |     CSS/StyleRule.cpp | ||||||
|     CSS/StyleSheet.cpp |     CSS/StyleSheet.cpp | ||||||
|     CSS/StyleValue.cpp |     CSS/StyleValue.cpp | ||||||
|  |     DOM/AttributeNames.cpp | ||||||
|     DOM/CanvasRenderingContext2D.cpp |     DOM/CanvasRenderingContext2D.cpp | ||||||
|     DOM/CharacterData.cpp |     DOM/CharacterData.cpp | ||||||
|     DOM/Comment.cpp |     DOM/Comment.cpp | ||||||
|  |  | ||||||
|  | @ -25,6 +25,7 @@ | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| #include <LibWeb/CSS/SelectorEngine.h> | #include <LibWeb/CSS/SelectorEngine.h> | ||||||
|  | #include <LibWeb/DOM/AttributeNames.h> | ||||||
| #include <LibWeb/DOM/Document.h> | #include <LibWeb/DOM/Document.h> | ||||||
| #include <LibWeb/DOM/Element.h> | #include <LibWeb/DOM/Element.h> | ||||||
| #include <LibWeb/DOM/Text.h> | #include <LibWeb/DOM/Text.h> | ||||||
|  | @ -98,7 +99,7 @@ bool matches(const Selector::SimpleSelector& component, const Element& element) | ||||||
|     case Selector::SimpleSelector::Type::Universal: |     case Selector::SimpleSelector::Type::Universal: | ||||||
|         return true; |         return true; | ||||||
|     case Selector::SimpleSelector::Type::Id: |     case Selector::SimpleSelector::Type::Id: | ||||||
|         return component.value == element.attribute("id"); |         return component.value == element.attribute(HTML::AttributeNames::id); | ||||||
|     case Selector::SimpleSelector::Type::Class: |     case Selector::SimpleSelector::Type::Class: | ||||||
|         return element.has_class(component.value); |         return element.has_class(component.value); | ||||||
|     case Selector::SimpleSelector::Type::TagName: |     case Selector::SimpleSelector::Type::TagName: | ||||||
|  |  | ||||||
							
								
								
									
										48
									
								
								Libraries/LibWeb/DOM/AttributeNames.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								Libraries/LibWeb/DOM/AttributeNames.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,48 @@ | ||||||
|  | /*
 | ||||||
|  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | ||||||
|  |  * All rights reserved. | ||||||
|  |  * | ||||||
|  |  * Redistribution and use in source and binary forms, with or without | ||||||
|  |  * modification, are permitted provided that the following conditions are met: | ||||||
|  |  * | ||||||
|  |  * 1. Redistributions of source code must retain the above copyright notice, this | ||||||
|  |  *    list of conditions and the following disclaimer. | ||||||
|  |  * | ||||||
|  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | ||||||
|  |  *    this list of conditions and the following disclaimer in the documentation | ||||||
|  |  *    and/or other materials provided with the distribution. | ||||||
|  |  * | ||||||
|  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||||||
|  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||||||
|  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||||||
|  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||||||
|  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||||||
|  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||||||
|  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||||||
|  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||||||
|  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||||||
|  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | #include <LibWeb/DOM/AttributeNames.h> | ||||||
|  | 
 | ||||||
|  | namespace Web { | ||||||
|  | namespace HTML { | ||||||
|  | namespace AttributeNames { | ||||||
|  | 
 | ||||||
|  | FlyString id; | ||||||
|  | FlyString class_; | ||||||
|  | 
 | ||||||
|  | void initialize() | ||||||
|  | { | ||||||
|  |     bool s_initialized = false; | ||||||
|  |     if (s_initialized) | ||||||
|  |         return; | ||||||
|  |     id = "id"; | ||||||
|  |     class_ = "class"; | ||||||
|  |     s_initialized = true; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | } | ||||||
|  | } | ||||||
							
								
								
									
										42
									
								
								Libraries/LibWeb/DOM/AttributeNames.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								Libraries/LibWeb/DOM/AttributeNames.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,42 @@ | ||||||
|  | /*
 | ||||||
|  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | ||||||
|  |  * All rights reserved. | ||||||
|  |  * | ||||||
|  |  * Redistribution and use in source and binary forms, with or without | ||||||
|  |  * modification, are permitted provided that the following conditions are met: | ||||||
|  |  * | ||||||
|  |  * 1. Redistributions of source code must retain the above copyright notice, this | ||||||
|  |  *    list of conditions and the following disclaimer. | ||||||
|  |  * | ||||||
|  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | ||||||
|  |  *    this list of conditions and the following disclaimer in the documentation | ||||||
|  |  *    and/or other materials provided with the distribution. | ||||||
|  |  * | ||||||
|  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||||||
|  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||||||
|  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||||||
|  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||||||
|  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||||||
|  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||||||
|  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||||||
|  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||||||
|  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||||||
|  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | #pragma once | ||||||
|  | 
 | ||||||
|  | #include <AK/FlyString.h> | ||||||
|  | 
 | ||||||
|  | namespace Web { | ||||||
|  | namespace HTML { | ||||||
|  | namespace AttributeNames { | ||||||
|  | 
 | ||||||
|  | void initialize(); | ||||||
|  | 
 | ||||||
|  | extern FlyString id; | ||||||
|  | extern FlyString class_; | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | } | ||||||
|  | } | ||||||
|  | @ -37,6 +37,7 @@ | ||||||
| #include <LibWeb/Bindings/WindowObject.h> | #include <LibWeb/Bindings/WindowObject.h> | ||||||
| #include <LibWeb/CSS/SelectorEngine.h> | #include <LibWeb/CSS/SelectorEngine.h> | ||||||
| #include <LibWeb/CSS/StyleResolver.h> | #include <LibWeb/CSS/StyleResolver.h> | ||||||
|  | #include <LibWeb/DOM/AttributeNames.h> | ||||||
| #include <LibWeb/DOM/Document.h> | #include <LibWeb/DOM/Document.h> | ||||||
| #include <LibWeb/DOM/DocumentType.h> | #include <LibWeb/DOM/DocumentType.h> | ||||||
| #include <LibWeb/DOM/Element.h> | #include <LibWeb/DOM/Element.h> | ||||||
|  | @ -65,6 +66,8 @@ Document::Document(const URL& url) | ||||||
|     , m_url(url) |     , m_url(url) | ||||||
|     , m_window(Window::create_with_document(*this)) |     , m_window(Window::create_with_document(*this)) | ||||||
| { | { | ||||||
|  |     HTML::AttributeNames::initialize(); | ||||||
|  | 
 | ||||||
|     m_style_update_timer = Core::Timer::create_single_shot(0, [this] { |     m_style_update_timer = Core::Timer::create_single_shot(0, [this] { | ||||||
|         update_style(); |         update_style(); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|  | @ -27,6 +27,7 @@ | ||||||
| #pragma once | #pragma once | ||||||
| 
 | 
 | ||||||
| #include <AK/Forward.h> | #include <AK/Forward.h> | ||||||
|  | #include <LibWeb/DOM/AttributeNames.h> | ||||||
| #include <LibWeb/Forward.h> | #include <LibWeb/Forward.h> | ||||||
| #include <LibWeb/TreeNode.h> | #include <LibWeb/TreeNode.h> | ||||||
| 
 | 
 | ||||||
|  | @ -39,7 +40,7 @@ public: | ||||||
|     { |     { | ||||||
|         const Element* found_element = nullptr; |         const Element* found_element = nullptr; | ||||||
|         static_cast<const NodeType*>(this)->template for_each_in_subtree_of_type<Element>([&](auto& element) { |         static_cast<const NodeType*>(this)->template for_each_in_subtree_of_type<Element>([&](auto& element) { | ||||||
|             if (element.attribute("id") == id) { |             if (element.attribute(HTML::AttributeNames::id) == id) { | ||||||
|                 found_element = &element; |                 found_element = &element; | ||||||
|                 return IterationDecision::Break; |                 return IterationDecision::Break; | ||||||
|             } |             } | ||||||
|  | @ -53,7 +54,7 @@ public: | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| protected: | protected: | ||||||
|     NonElementParentNode() {} |     NonElementParentNode() { } | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling