From a352cbfd08b039d393a68ee8bc62d5d86cf02fbf Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Fri, 2 Oct 2020 18:21:45 -0400 Subject: Implemented a significant performance optimization to the process of searching the parse tree. Patch by Morotti. [bug=1898212] --- bs4/element.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'bs4') diff --git a/bs4/element.py b/bs4/element.py index 370b153..09a81d9 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -1995,6 +1995,14 @@ class SoupStrainer(object): if isinstance(markup_name, Tag): markup = markup_name markup_attrs = markup + + if isinstance(self.name, basestring): + # Optimization for a very common case where the user is + # searching for a tag with one specific name, and we're + # looking at a tag with a different name. + if markup and not markup.prefix and self.name != markup.name: + return False + call_function_with_tag_data = ( isinstance(self.name, Callable) and not isinstance(markup_name, Tag)) -- cgit v1.2.3