File: 0019-xpath-Use-separate-static-hash-table-for-standard-fu.patch

package info (click to toggle)
ruby-nokogiri 1.18.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,076 kB
  • sloc: ansic: 38,893; xml: 27,665; ruby: 27,285; java: 15,348; cpp: 7,107; yacc: 244; sh: 208; makefile: 154; sed: 14
file content (244 lines) | stat: -rw-r--r-- 8,916 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
From d3e3526111097560cf7c002613e2cb1d469b59e0 Mon Sep 17 00:00:00 2001
From: Nick Wellnhofer <wellnhofer@aevum.de>
Date: Sat, 21 Dec 2024 16:03:46 +0100
Subject: [PATCH] xpath: Use separate static hash table for standard functions

This avoids registering standard functions when creating an XPath
context.

Lookup of extension functions is a bit slower now, but ultimately, all
function lookups should be moved to the compilation phase.

(cherry picked from commit bf5fcf6e646bb51a0f6a3655a1d64bea97274867)
---
 xpath.c | 170 ++++++++++++++++++++++++++++++++------------------------
 1 file changed, 98 insertions(+), 72 deletions(-)

diff --git a/xpath.c b/xpath.c
index 485d7747..21711653 100644
--- a/xpath.c
+++ b/xpath.c
@@ -136,11 +136,48 @@
 
 #if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
 
-/************************************************************************
- *									*
- *			Floating point stuff				*
- *									*
- ************************************************************************/
+static void
+xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs);
+
+static const struct {
+    const char *name;
+    xmlXPathFunction func;
+} xmlXPathStandardFunctions[] = {
+    { "boolean", xmlXPathBooleanFunction },
+    { "ceiling", xmlXPathCeilingFunction },
+    { "count", xmlXPathCountFunction },
+    { "concat", xmlXPathConcatFunction },
+    { "contains", xmlXPathContainsFunction },
+    { "id", xmlXPathIdFunction },
+    { "false", xmlXPathFalseFunction },
+    { "floor", xmlXPathFloorFunction },
+    { "last", xmlXPathLastFunction },
+    { "lang", xmlXPathLangFunction },
+    { "local-name", xmlXPathLocalNameFunction },
+    { "not", xmlXPathNotFunction },
+    { "name", xmlXPathNameFunction },
+    { "namespace-uri", xmlXPathNamespaceURIFunction },
+    { "normalize-space", xmlXPathNormalizeFunction },
+    { "number", xmlXPathNumberFunction },
+    { "position", xmlXPathPositionFunction },
+    { "round", xmlXPathRoundFunction },
+    { "string", xmlXPathStringFunction },
+    { "string-length", xmlXPathStringLengthFunction },
+    { "starts-with", xmlXPathStartsWithFunction },
+    { "substring", xmlXPathSubstringFunction },
+    { "substring-before", xmlXPathSubstringBeforeFunction },
+    { "substring-after", xmlXPathSubstringAfterFunction },
+    { "sum", xmlXPathSumFunction },
+    { "true", xmlXPathTrueFunction },
+    { "translate", xmlXPathTranslateFunction }
+};
+
+#define NUM_STANDARD_FUNCTIONS \
+    (sizeof(xmlXPathStandardFunctions) / sizeof(xmlXPathStandardFunctions[0]))
+
+#define SF_HASH_SIZE 64
+
+static unsigned char xmlXPathSFHash[SF_HASH_SIZE];
 
 double xmlXPathNAN = 0.0;
 double xmlXPathPINF = 0.0;
@@ -156,6 +193,18 @@ xmlXPathInit(void) {
     xmlInitParser();
 }
 
+ATTRIBUTE_NO_SANITIZE_INTEGER
+static unsigned
+xmlXPathSFComputeHash(const xmlChar *name) {
+    unsigned hashValue = 5381;
+    const xmlChar *ptr;
+
+    for (ptr = name; *ptr; ptr++)
+        hashValue = hashValue * 33 + *ptr;
+
+    return(hashValue);
+}
+
 /**
  * xmlInitXPathInternal:
  *
@@ -164,6 +213,8 @@ xmlXPathInit(void) {
 ATTRIBUTE_NO_SANITIZE("float-divide-by-zero")
 void
 xmlInitXPathInternal(void) {
+    size_t i;
+
 #if defined(NAN) && defined(INFINITY)
     xmlXPathNAN = NAN;
     xmlXPathPINF = INFINITY;
@@ -175,8 +226,34 @@ xmlInitXPathInternal(void) {
     xmlXPathPINF = 1.0 / zero;
     xmlXPathNINF = -xmlXPathPINF;
 #endif
+
+    /*
+     * Initialize hash table for standard functions
+     */
+
+    for (i = 0; i < SF_HASH_SIZE; i++)
+        xmlXPathSFHash[i] = UCHAR_MAX;
+
+    for (i = 0; i < NUM_STANDARD_FUNCTIONS; i++) {
+        const char *name = xmlXPathStandardFunctions[i].name;
+        int bucketIndex = xmlXPathSFComputeHash(BAD_CAST name) % SF_HASH_SIZE;
+
+        while (xmlXPathSFHash[bucketIndex] != UCHAR_MAX) {
+            bucketIndex += 1;
+            if (bucketIndex >= SF_HASH_SIZE)
+                bucketIndex = 0;
+        }
+
+        xmlXPathSFHash[bucketIndex] = i;
+    }
 }
 
+/************************************************************************
+ *									*
+ *			Floating point stuff				*
+ *									*
+ ************************************************************************/
+
 /**
  * xmlXPathIsNaN:
  * @val:  a double value
@@ -3979,18 +4056,6 @@ xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt,
  */
 xmlXPathFunction
 xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) {
-    if (ctxt == NULL)
-	return (NULL);
-
-    if (ctxt->funcLookupFunc != NULL) {
-	xmlXPathFunction ret;
-	xmlXPathFuncLookupFunc f;
-
-	f = ctxt->funcLookupFunc;
-	ret = f(ctxt->funcLookupData, name, NULL);
-	if (ret != NULL)
-	    return(ret);
-    }
     return(xmlXPathFunctionLookupNS(ctxt, name, NULL));
 }
 
@@ -4015,6 +4080,22 @@ xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name,
     if (name == NULL)
 	return(NULL);
 
+    if (ns_uri == NULL) {
+        int bucketIndex = xmlXPathSFComputeHash(name) % SF_HASH_SIZE;
+
+        while (xmlXPathSFHash[bucketIndex] != UCHAR_MAX) {
+            int funcIndex = xmlXPathSFHash[bucketIndex];
+
+            if (strcmp(xmlXPathStandardFunctions[funcIndex].name,
+                       (char *) name) == 0)
+                return(xmlXPathStandardFunctions[funcIndex].func);
+
+            bucketIndex += 1;
+            if (bucketIndex >= SF_HASH_SIZE)
+                bucketIndex = 0;
+        }
+    }
+
     if (ctxt->funcLookupFunc != NULL) {
 	xmlXPathFuncLookupFunc f;
 
@@ -13494,61 +13575,6 @@ xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt, int nargs) {
 void
 xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt)
 {
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean",
-                         xmlXPathBooleanFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling",
-                         xmlXPathCeilingFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count",
-                         xmlXPathCountFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat",
-                         xmlXPathConcatFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains",
-                         xmlXPathContainsFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id",
-                         xmlXPathIdFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false",
-                         xmlXPathFalseFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor",
-                         xmlXPathFloorFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last",
-                         xmlXPathLastFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang",
-                         xmlXPathLangFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name",
-                         xmlXPathLocalNameFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not",
-                         xmlXPathNotFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name",
-                         xmlXPathNameFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri",
-                         xmlXPathNamespaceURIFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space",
-                         xmlXPathNormalizeFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number",
-                         xmlXPathNumberFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position",
-                         xmlXPathPositionFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round",
-                         xmlXPathRoundFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string",
-                         xmlXPathStringFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length",
-                         xmlXPathStringLengthFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with",
-                         xmlXPathStartsWithFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring",
-                         xmlXPathSubstringFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before",
-                         xmlXPathSubstringBeforeFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after",
-                         xmlXPathSubstringAfterFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum",
-                         xmlXPathSumFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true",
-                         xmlXPathTrueFunction);
-    xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate",
-                         xmlXPathTranslateFunction);
-
     xmlXPathRegisterFuncNS(ctxt, (const xmlChar *)"escape-uri",
 	 (const xmlChar *)"http://www.w3.org/2002/08/xquery-functions",
                          xmlXPathEscapeUriFunction);
-- 
2.47.1