From 31dc1fa6622c599fc3bfdc687f6fc5043bd0c05b Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 26 Jan 2025 13:59:45 +1300 Subject: [PATCH] LibWeb/HTML: Only get list of options once in 'update selectedness' It is not cheap to do this, so only do it once within this function. There is definitely some caching that we can do here, but this will require some smart invalidation to detect _relevant_ changes in the children. --- Libraries/LibWeb/HTML/HTMLSelectElement.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp b/Libraries/LibWeb/HTML/HTMLSelectElement.cpp index aad95f9c03a..424fe131041 100644 --- a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLSelectElement.cpp @@ -593,10 +593,12 @@ void HTMLSelectElement::update_selectedness() if (has_attribute(AttributeNames::multiple)) return; + auto list_of_options = this->list_of_options(); + // If element's multiple attribute is absent, and element's display size is 1, if (display_size() == 1) { bool has_selected_elements = false; - for (auto const& option_element : list_of_options()) { + for (auto const& option_element : list_of_options) { if (option_element->selected()) { has_selected_elements = true; break; @@ -607,7 +609,7 @@ void HTMLSelectElement::update_selectedness() if (!has_selected_elements) { // then set the selectedness of the first option element in the list of options in tree order // that is not disabled, if any, to true, and return. - for (auto const& option_element : list_of_options()) { + for (auto const& option_element : list_of_options) { if (!option_element->disabled()) { option_element->set_selected_internal(true); update_inner_text_element(); @@ -623,7 +625,7 @@ void HTMLSelectElement::update_selectedness() // then set the selectedness of all but the last option element with its selectedness set to true // in the list of options in tree order to false. int number_of_selected = 0; - for (auto const& option_element : list_of_options()) { + for (auto const& option_element : list_of_options) { if (option_element->selected()) ++number_of_selected; } @@ -634,7 +636,7 @@ void HTMLSelectElement::update_selectedness() GC::Ptr last_selected_option; u64 last_selected_option_update_index = 0; - for (auto const& option_element : list_of_options()) { + for (auto const& option_element : list_of_options) { if (!option_element->selected()) continue; if (!last_selected_option @@ -644,7 +646,7 @@ void HTMLSelectElement::update_selectedness() } } - for (auto const& option_element : list_of_options()) { + for (auto const& option_element : list_of_options) { if (option_element != last_selected_option) option_element->set_selected_internal(false); }