From 52059740e378e2cceacabb74acec096cbd33583e Mon Sep 17 00:00:00 2001 From: Saurav Sachidanand Date: Wed, 24 Feb 2016 17:41:07 +0530 Subject: [PATCH] Inline functions HTMLCollection::get_length and get_item --- components/script/dom/htmlcollection.rs | 88 +++++++++++-------------- 1 file changed, 40 insertions(+), 48 deletions(-) diff --git a/components/script/dom/htmlcollection.rs b/components/script/dom/htmlcollection.rs index 0c429c295271..b2c691fb762a 100644 --- a/components/script/dom/htmlcollection.rs +++ b/components/script/dom/htmlcollection.rs @@ -103,19 +103,6 @@ impl HTMLCollection { } } - fn get_length(&self) -> u32 { - // Call validate_cache before calling this method! - if let Some(cached_length) = self.cached_length.get().to_option() { - // Cache hit - cached_length - } else { - // Cache miss, calculate the length - let length = self.elements_iter().count() as u32; - self.cached_length.set(OptionU32::some(length)); - length - } - } - fn set_cached_cursor(&self, index: u32, element: Option>) -> Option> { if let Some(element) = element { self.cached_cursor_index.set(OptionU32::some(index)); @@ -126,39 +113,6 @@ impl HTMLCollection { } } - fn get_item(&self, index: u32) -> Option> { - // Call validate_cache before calling this method! - if let Some(element) = self.cached_cursor_element.get() { - // Cache hit, the cursor element is set - if let Some(cached_index) = self.cached_cursor_index.get().to_option() { - if cached_index == index { - // The cursor is the element we're looking for - Some(element) - } else if cached_index < index { - // The cursor is before the element we're looking for - // Iterate forwards, starting at the cursor. - let offset = index - (cached_index + 1); - let node: Root = Root::upcast(element); - self.set_cached_cursor(index, self.elements_iter_after(node.r()).nth(offset as usize)) - } else { - // The cursor is after the element we're looking for - // Iterate backwards, starting at the cursor. - let offset = cached_index - (index + 1); - let node: Root = Root::upcast(element); - self.set_cached_cursor(index, self.elements_iter_before(node.r()).nth(offset as usize)) - } - } else { - // Cache miss - // Iterate forwards through all the nodes - self.set_cached_cursor(index, self.elements_iter().nth(index as usize)) - } - } else { - // Cache miss - // Iterate forwards through all the nodes - self.set_cached_cursor(index, self.elements_iter().nth(index as usize)) - } - } - pub fn by_tag_name(window: &Window, root: &Node, mut tag: DOMString) -> Root { let tag_atom = Atom::from(&*tag); // FIXME(ajeffrey): Convert directly from DOMString to Atom @@ -319,13 +273,51 @@ impl HTMLCollectionMethods for HTMLCollection { // https://dom.spec.whatwg.org/#dom-htmlcollection-length fn Length(&self) -> u32 { self.validate_cache(); - self.get_length() + + if let Some(cached_length) = self.cached_length.get().to_option() { + // Cache hit + cached_length + } else { + // Cache miss, calculate the length + let length = self.elements_iter().count() as u32; + self.cached_length.set(OptionU32::some(length)); + length + } } // https://dom.spec.whatwg.org/#dom-htmlcollection-item fn Item(&self, index: u32) -> Option> { self.validate_cache(); - self.get_item(index) + + if let Some(element) = self.cached_cursor_element.get() { + // Cache hit, the cursor element is set + if let Some(cached_index) = self.cached_cursor_index.get().to_option() { + if cached_index == index { + // The cursor is the element we're looking for + Some(element) + } else if cached_index < index { + // The cursor is before the element we're looking for + // Iterate forwards, starting at the cursor. + let offset = index - (cached_index + 1); + let node: Root = Root::upcast(element); + self.set_cached_cursor(index, self.elements_iter_after(node.r()).nth(offset as usize)) + } else { + // The cursor is after the element we're looking for + // Iterate backwards, starting at the cursor. + let offset = cached_index - (index + 1); + let node: Root = Root::upcast(element); + self.set_cached_cursor(index, self.elements_iter_before(node.r()).nth(offset as usize)) + } + } else { + // Cache miss + // Iterate forwards through all the nodes + self.set_cached_cursor(index, self.elements_iter().nth(index as usize)) + } + } else { + // Cache miss + // Iterate forwards through all the nodes + self.set_cached_cursor(index, self.elements_iter().nth(index as usize)) + } } // https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem