[libxml2] Speed up htmlTagLookup



commit b25acce858d4eea49b02b5d4e32708b914107b29
Author: Nick Wellnhofer <wellnhofer aevum de>
Date:   Thu Mar 4 17:44:45 2021 +0100

    Speed up htmlTagLookup
    
    Switch to binary search. This is the first time bsearch is used in the
    libxml2 code base. But it's a standard library function since C89 and
    should be portable.

 HTMLparser.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
---
diff --git a/HTMLparser.c b/HTMLparser.c
index c9a64c78..376fbd71 100644
--- a/HTMLparser.c
+++ b/HTMLparser.c
@@ -1269,6 +1269,14 @@ htmlInitAutoClose(void) {
     htmlStartCloseIndexinitialized = 1;
 }
 
+static int
+htmlCompareTags(const void *key, const void *member) {
+    const char *tag = (const char *) key;
+    const htmlElemDesc *desc = (const htmlElemDesc *) member;
+
+    return(strcmp(tag, desc->name));
+}
+
 /**
  * htmlTagLookup:
  * @tag:  The tag name in lowercase
@@ -1279,14 +1287,12 @@ htmlInitAutoClose(void) {
  */
 const htmlElemDesc *
 htmlTagLookup(const xmlChar *tag) {
-    unsigned int i;
+    if (tag == NULL)
+        return(NULL);
 
-    for (i = 0; i < (sizeof(html40ElementTable) /
-                     sizeof(html40ElementTable[0]));i++) {
-        if (!xmlStrcasecmp(tag, BAD_CAST html40ElementTable[i].name))
-           return((htmlElemDescPtr) &html40ElementTable[i]);
-    }
-    return(NULL);
+    return((const htmlElemDesc *) bsearch(tag, html40ElementTable,
+                sizeof(html40ElementTable) / sizeof(htmlElemDesc),
+                sizeof(htmlElemDesc), htmlCompareTags));
 }
 
 /**


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]