Skip to content

Commit da6766d

Browse files
nielsdosalexdowad
authored andcommitted
Use more optimal perfect hash table
1 parent 0ea4f39 commit da6766d

File tree

1 file changed

+93
-135
lines changed

1 file changed

+93
-135
lines changed

ext/mbstring/libmbfl/mbfl/mbfl_encoding.c

Lines changed: 93 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -146,179 +146,137 @@ static const mbfl_encoding *mbfl_encoding_ptr_list[] = {
146146

147147
/* The following perfect hashing table was amended from gperf, and hashing code was generated using gperf.
148148
* The table was amended to refer to the table above such that it is lighter for the data cache.
149-
* Command used: gperf encodings.txt --readonly-tables --null-strings --ignore-case
150-
* The encodings.txt contains all the contents of the name fields of the mbfl_encoding_ptr_list table. */
149+
* You can use the generate_name_perfect_hash_table.php script to help generate the necessary lookup tables. */
151150

152-
static const int8_t mbfl_encoding_ptr_list_after_hashing[231] = {
151+
static const int8_t mbfl_encoding_ptr_list_after_hashing[] = {
152+
-1, -1, -1, -1,
153153
-1, -1,
154-
61,
155154
66,
156-
23,
157-
73,
158-
59,
159155
-1,
160-
1,
161-
-1, -1, -1,
162-
11,
163-
-1,
164-
5,
165-
9,
156+
73,
166157
-1,
167-
10,
168-
38,
158+
78,
159+
61,
160+
76,
169161
-1,
162+
59,
163+
46,
170164
52,
171165
54,
172-
-1,
173-
2,
174-
40,
175-
46,
176-
27,
177-
76,
178-
26,
179-
-1,
180166
49,
181167
57,
182-
-1,
183-
75,
184-
-1,
185-
47,
186-
55,
187-
78,
188-
36,
189-
-1,
168+
69,
169+
21,
190170
50,
191171
58,
192-
8,
193-
-1, -1,
194-
69,
195-
39,
196-
7,
197-
-1, -1,
172+
75,
173+
35,
174+
9,
198175
64,
199-
67,
200-
-1, -1,
201-
30,
202176
48,
203177
56,
204-
-1, -1, -1,
205-
35,
206178
74,
207-
-1, -1,
208-
24,
209-
53,
210-
62,
211-
43,
212-
-1, -1,
179+
47,
180+
55,
181+
40,
213182
45,
214-
22,
215-
-1, -1, -1,
216-
6,
217-
3,
218-
-1, -1, -1,
183+
53,
219184
18,
185+
39,
186+
72,
187+
60,
188+
23,
189+
10,
190+
30,
191+
36,
192+
67,
220193
71,
221-
-1, -1, -1,
222-
21,
223-
-1,
224194
37,
225-
-1,
226-
4,
227-
60,
228-
25,
229-
-1, -1,
230-
72,
195+
27,
196+
77,
197+
26,
231198
51,
232-
-1,
233-
44,
199+
12,
200+
6,
201+
11,
202+
7,
234203
29,
235-
-1,
236-
28,
204+
5,
205+
24,
237206
0,
238-
-1,
239-
14,
207+
2,
208+
13,
209+
43,
240210
31,
211+
33,
212+
38,
241213
63,
242-
12,
214+
8,
215+
1,
216+
15,
217+
-1,
218+
16,
219+
-1,
220+
14,
221+
3,
222+
44,
243223
-1,
244-
13,
245-
33,
246-
-1, -1,
247-
68,
248-
-1, -1, -1, -1,
249-
-1, -1,
250224
20,
251-
-1, -1, -1, -1,
252-
-1, -1, -1,
253-
77,
254-
-1, -1, -1, -1,
255-
-1, -1, -1, -1,
256-
65,
257-
-1, -1, -1, -1,
258-
70,
259-
-1, -1, -1, -1,
260225
-1,
261-
41,
262-
-1, -1, -1, -1,
226+
32,
263227
-1,
228+
68,
229+
25,
264230
17,
231+
28,
265232
-1, -1, -1,
266-
42,
267-
16,
268-
-1, -1, -1, -1,
269-
-1, -1, -1, -1,
270-
-1, -1, -1, -1,
271-
15,
272-
-1, -1, -1, -1,
233+
22,
234+
-1, -1,
235+
4,
236+
-1, -1,
237+
62,
238+
-1, -1,
273239
34,
274-
-1, -1, -1, -1,
275-
-1, -1, -1,
276-
32,
277-
-1, -1, -1, -1,
278-
-1, -1, -1, -1,
279-
-1, -1, -1, -1,
280-
-1, -1, -1, -1,
281-
-1, -1, -1, -1,
282-
-1, -1, -1, -1,
283-
-1, -1, -1, -1,
284-
-1, -1, -1, -1,
285-
-1, -1, -1, -1,
286-
-1, -1, -1, -1,
287-
-1, -1, -1, -1,
288240
-1,
289-
19
241+
41,
242+
-1, -1, -1,
243+
42,
244+
70,
245+
19,
246+
-1, -1, -1,
247+
65
290248
};
291249

292250
static unsigned int mbfl_name2encoding_perfect_hash(const char *str, size_t len)
293251
{
294252
static const unsigned char asso_values[] =
295253
{
296-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
297-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
298-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
299-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
300-
231, 231, 231, 231, 231, 5, 231, 231, 0, 50,
301-
5, 15, 35, 10, 20, 75, 0, 45, 231, 231,
302-
231, 231, 231, 231, 231, 80, 5, 0, 0, 0,
303-
75, 75, 0, 0, 15, 70, 0, 5, 0, 0,
304-
25, 55, 30, 0, 10, 0, 231, 25, 231, 231,
305-
0, 231, 231, 231, 231, 231, 231, 80, 5, 0,
306-
0, 0, 75, 75, 0, 0, 15, 70, 0, 5,
307-
0, 0, 25, 55, 30, 0, 10, 0, 231, 25,
308-
231, 231, 0, 231, 231, 231, 231, 231, 231, 231,
309-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
310-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
311-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
312-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
313-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
314-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
315-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
316-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
317-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
318-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
319-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
320-
231, 231, 231, 231, 231, 231, 231, 231, 231, 231,
321-
231, 231, 231, 231, 231, 231
254+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
255+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
256+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
257+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
258+
109, 109, 109, 109, 109, 1, 109, 109, 1, 19,
259+
0, 16, 13, 3, 7, 35, 1, 20, 109, 109,
260+
109, 109, 109, 109, 109, 16, 1, 0, 44, 6,
261+
26, 53, 8, 0, 25, 32, 13, 12, 1, 0,
262+
25, 0, 32, 18, 51, 3, 109, 15, 109, 109,
263+
1, 109, 109, 109, 109, 109, 109, 16, 1, 0,
264+
44, 6, 26, 53, 8, 0, 25, 32, 13, 12,
265+
1, 0, 25, 0, 32, 18, 51, 3, 109, 15,
266+
109, 109, 1, 109, 109, 109, 109, 109, 109, 109,
267+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
268+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
269+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
270+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
271+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
272+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
273+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
274+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
275+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
276+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
277+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
278+
109, 109, 109, 109, 109, 109, 109, 109, 109, 109,
279+
109, 109, 109, 109, 109, 109
322280
};
323281
unsigned int hval = len;
324282

@@ -377,7 +335,7 @@ const mbfl_encoding *mbfl_name2encoding_ex(const char *name, size_t name_len)
377335
/* Use perfect hash lookup for name */
378336
if (name_len <= NAME_HASH_MAX_NAME_LENGTH && name_len >= NAME_HASH_MIN_NAME_LENGTH) {
379337
unsigned int key = mbfl_name2encoding_perfect_hash(name, name_len);
380-
if (key <= sizeof(mbfl_encoding_ptr_list_after_hashing)) {
338+
if (key < sizeof(mbfl_encoding_ptr_list_after_hashing) / sizeof(mbfl_encoding_ptr_list_after_hashing[0])) {
381339
int8_t offset = mbfl_encoding_ptr_list_after_hashing[key];
382340
if (offset >= 0) {
383341
encoding = mbfl_encoding_ptr_list + offset;

0 commit comments

Comments
 (0)