XS.xs

filter: Statementstmt 83.7% 144 / 172 Branchbran 76.3% 58 / 76 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.4% 202 / 248 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« txs_hash.h ↑ Summary
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "txs_hash.h"
6  
7 #ifdef TXS_SUB_OP
8 #include "sub_op.h"
9 #endif
10  
11 /*Change this if you care. Note that the memory used is this number
12 multiplied by 256 again. The default is 256 * 256 == 65535 */
13  
14 #define CHARTABLE_MAX 256
15  
16 typedef unsigned char TXS_chartable_t[256];
17  
18 #define txs_search_from_sv(sv) (struct TXS_Search*)(SvUVX(sv))
19 #define terms_from_search(srch) \
20 (struct TXS_String*)(((char*)srch) + sizeof(struct TXS_Search))
21  
22 #define TXS_COUNTERS
23  
24 #ifdef TXS_COUNTERS
25 #define OPTIMIZE_STATS_FIELDS \
26 struct { \
27 int opti_lengths; \
28 int opti_chartable; \
29 int opti_hash_firstpass; \
30 int opti_hash_secondpass; \
31 int opti_none; \
32 int opti_8; \
33 int opti_4; \
34 int opti_2; \
35 } statistics ;
36  
37 #define txs_inc_counter(srchp, v) \
38 (srchp->statistics.opti_ ## v)++;
39  
40 #define _txs_dumpstat(srchp, stat) \
41 printf("%s: %d\n", "TXS Optimized: " #stat, srch->statistics.opti_ ## stat);
42  
43 #define txs_dump_stats(srchp) \
44 _txs_dumpstat(srchp, lengths); _txs_dumpstat(srchp, chartable); \
45 _txs_dumpstat(srchp, hash_firstpass); _txs_dumpstat(srchp, hash_secondpass); \
46 _txs_dumpstat(srch, none);
47 #else
48  
49 #define OPTIMIZE_STATS_FIELDS
50 #define txs_inc_counter(a, b)
51 #define txs_dump_stats(a)
52  
53 #endif
54  
55  
56 enum {
57 TXSf_BAD_CHARTABLE = 1 << 0
58 };
59  
60 typedef uint8_t TXS_termlen_t;
61  
62 struct TXS_Search {
63 int flags;
64
65 /*The number of prefixes*/
66 int term_count;
67
68 /*The minimum (or rather, maximum) common prefix length for all prefixes*/
69 int min_len;
70
71 /*The maximum length for a term*/
72 int max_len;
73
74 /*How many unique lengths*/
75 int term_lengths_max;
76
77 /*If being kept across threads, we make duplicate pointer
78 references to our structure. This counter is set to one
79 for new object creation, and then increased by one during
80 each svt_dup, and decreased by one during svt_free*/
81 int refcount;
82
83 /*Sparse array for checking the existence of a char in a given position
84 for any term*/
85 TXS_chartable_t chartable[CHARTABLE_MAX];
86
87 /*Static array of term_lengths_max lengths*/
88 TXS_termlen_t term_lengths[CHARTABLE_MAX];
89
90 /*Prefix tree - This is a hash which is checked in the first hash pass,
91 and its keys are substrings of prefix, all exactly min_len length*/
92 struct TXS_HashTable *ht_min;
93
94 /*Full match index. This contains the actual prefixes in hash
95 form, and is traversed on the second hash pass*/
96 struct TXS_HashTable *ht_full;
97
98 /*Optimizations and statistics*/
99 OPTIMIZE_STATS_FIELDS;
100 };
101  
102 static int txs_freehook(pTHX_ SV* mysv, MAGIC* mg);
103 static int txs_duphook(pTHX_ MAGIC *mg, CLONE_PARAMS *param);
104 static void THX_txs_sv_init(pTHX_ SV *mysv, struct TXS_Search *srch);
105 #define txs_sv_init(mysv, srch) THX_txs_sv_init(aTHX_ mysv, srch)
106  
107 static MGVTBL txs_vtbl = {
108 .svt_free = txs_freehook,
109 .svt_dup = txs_duphook
110 };
111  
112  
113 typedef int(*txs_compar_fn_t)(const void*, const void*);
114  
115 13 static int _compar(const TXS_termlen_t *i1, const TXS_termlen_t *i2)
116 {
117 13 ▶ if(*i1 < *i2) {
Branch: 1 branch
BranchTrueFalseTotal
if(*i1 < *i2) { 2 11 13
118 2 return -1;
119 11 ▶ } else if ( *i1 == *i2 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( *i1 == *i2 ) { 0 11 11
120 0 die("Didn't expect to find equal!");
121 } else {
122 11 return 1;
123 }
124 }
125  
126 #define term_sanity_check(svpp, idx) \
127 if(!svpp) { die("Terms list is partially empty at idx=%d", idx); } \
128 if(SvROK(*svpp)) { die("Found reference in terms list at idx=%d", idx); } \
129 if(sv_len(*svpp) > CHARTABLE_MAX ) { \
130 die("Found string larger than %d at idx=%d", CHARTABLE_MAX, idx); \
131 }
132  
133 #define study_terms(srch, mortal_av) THX_study_terms(aTHX_ srch, mortal_av)
134 3 static void THX_study_terms(
135 pTHX_
136 struct TXS_Search *srch,
137 AV *mortal_av)
138 {
139 3 SV **old_sv = NULL;
140 3 char *term_s = NULL;
141 3 STRLEN term_len = 0;
142 int i, j;
143 3 int len_idx = 0;
144 3 int sort_min = 0;
145
146 3 int max = av_len(mortal_av);
147
148 15 ▶ for(i = 0; i <= max; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i <= max; i++) { 12 3 15
149 12 SV **old_sv = av_fetch(mortal_av, i, 0);
150 12 ▶ term_sanity_check(old_sv, i);
Branch: 3 branches
BranchTrueFalseTotal
term_sanity_check(old_sv, i); 0 12 12
term_sanity_check(old_sv, i); 0 12 12
term_sanity_check(old_sv, i); 0 12 12
151
152 12 term_s = SvPV(*old_sv, term_len);
153 95 ▶ for(j = 0; j < term_len; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for(j = 0; j < term_len; j++) { 83 12 95
154 83 srch->chartable[j][(unsigned char)term_s[j]] = 1;
155 }
156
157
158 /*Avoid duplicates*/
159 29 ▶ for(j = 0; j < len_idx; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for(j = 0; j < len_idx; j++) { 18 11 29
160 18 ▶ if(srch->term_lengths[j] == term_len) {
Branch: 1 branch
BranchTrueFalseTotal
if(srch->term_lengths[j] == term_len) { 1 17 18
161 1 j = -1;
162 1 break;
163 }
164 }
165
166 12 ▶ if(j == len_idx) {
Branch: 1 branch
BranchTrueFalseTotal
if(j == len_idx) { 11 1 12
167 11 srch->term_lengths[len_idx++] = term_len;
168 }
169 }
170
171 /*Sort the lengths list*/
172 3 qsort(srch->term_lengths, len_idx, sizeof(TXS_termlen_t),
173 (txs_compar_fn_t)&_compar);
174
175 3 len_idx--;
176
177 3 srch->term_lengths_max = len_idx;
178 3 srch->max_len = srch->term_lengths[len_idx];
179 3 srch->min_len = srch->term_lengths[0];
180
181 3 srch->term_count = max;
182
183 //build_chartable_cost(srch);
184 3 }
185  
186 #define prefix_search_build(av) THX_prefix_search_build(aTHX_ av);
187 3 SV* THX_prefix_search_build(pTHX_ AV *mortal_av)
188 {
189 3 int i = 0, j = 0;
190 3 int max = av_len(mortal_av);
191 3 int my_len = sizeof(struct TXS_Search);
192
193 3 char *term_s = NULL;
194 3 STRLEN term_len = 0;
195 3 char *strlist_p = NULL;
196
197 3 struct TXS_String *strp = NULL;
198 3 struct TXS_Search *srch = NULL;
199 3 struct TXS_String *terms = NULL;
200
201 3 Newxz(srch, my_len, char);
202 3 srch->refcount = 1;
203
204 3 SV *mysv = newSVuv((UV)srch);
205 3 HV *fullmatch = newHV();
206 3 HV *trie = newHV();
207
208 HV *stash;
209 SV *blessed;
210
211 3 study_terms(srch, mortal_av);
212
213 15 ▶ for(i = 0; i <= max; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i <= max; i++) { 12 3 15
214 12 SV **a_term = av_fetch(mortal_av, i, 0);
215 12 term_s = SvPV(*a_term, term_len);
216 12 hv_store(fullmatch, term_s, term_len, &PL_sv_undef, 0);
217 12 hv_store(trie, term_s, srch->min_len, &PL_sv_undef, 0);
218
219 }
220
221 3 srch->ht_full = txs_ht_build(fullmatch);
222 3 srch->ht_min = txs_ht_build(trie);
223
224 3 SvREFCNT_dec(fullmatch);
225 3 SvREFCNT_dec(trie);
226
227 /*Study the chartable, and see if it's worthwhile performing
228 lookups against it*/
229 3 txs_sv_init(mysv, srch);
230 3 blessed = newRV_noinc(mysv);
231 3 stash = gv_stashpv("Text::Prefix::XS", 0);
232 3 ▶ if(!stash) {
Branch: 1 branch
BranchTrueFalseTotal
if(!stash) { 0 3 3
233 0 die("Couldn't get stash!");
234 }
235 3 sv_bless(blessed, stash);
236 3 return blessed;
237 }
238  
239 #define prefix_search(mysv, input_sv) THX_prefix_search(aTHX_ mysv, input_sv)
240 28 SV* THX_prefix_search(pTHX_ SV* mysv, SV *input_sv)
241 {
242 28 register int i = 1, j = 0;
243  
244 28 SV *ret = &PL_sv_undef;
245
246 register struct TXS_String *strp;
247 register int strp_len;
248 STRLEN input_len;
249
250 register int term_len;
251 28 int match_len = 0;
252 28 int can_match = 0;
253
254 28 char *input = SvPV(input_sv, input_len);
255 28 ▶ if(!SvROK(mysv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(mysv)) { 0 28 28
256 0 die("Not a valid search blob");
257 }
258 28 struct TXS_Search *srch = txs_search_from_sv(SvRV(mysv));
259 28 struct TXS_String *terms = terms_from_search(srch);
260
261 28 ▶ if(input_len < srch->term_lengths[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if(input_len < srch->term_lengths[0]) { 0 28 28
262 /*Too short!*/
263 0 goto GT_RET;
264 }
265
266 /*FIRST PASS:
267 * Check all prefix lengths, and see if we're eligible for any*/
268 74 ▶ for(i = 0; i <= srch->term_lengths_max; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i <= srch->term_lengths_max; i++) { 64 10 74
269 64 term_len = srch->term_lengths[i];
270
271 64 ▶ if(term_len > input_len) {
Branch: 1 branch
BranchTrueFalseTotal
if(term_len > input_len) { 2 62 64
272 2 break;
273 }
274
275 /*We can. Break*/
276 62 ▶ if(srch->chartable[term_len-1][ (unsigned char)input[term_len-1] ]) {
Branch: 1 branch
BranchTrueFalseTotal
if(srch->chartable[term_len-1][ (unsigned char)input[term_len-1] ]) { 16 46 62
277 16 can_match = 1;
278 16 break;
279 }
280 }
281
282 28 ▶ if(!can_match) {
Branch: 1 branch
BranchTrueFalseTotal
if(!can_match) { 12 16 28
283 12 txs_inc_counter(srch, lengths);
284 12 goto GT_RET;
285 }
286  
287 /* SECOND PASS:
288 * In our little game of hangman, we know the that we have a last character
289 * for at least one prefix.
290 *
291 * Now, check for non-existent characters in any of the rest of the
292 * prefixes, for the smallest prefix size
293 */
294 74 ▶ for(i = 0; i < srch->min_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < srch->min_len; i++) { 58 16 74
295 58 ▶ if(!srch->chartable[i][ (unsigned char)input[i] ]) {
Branch: 1 branch
BranchTrueFalseTotal
if(!srch->chartable[i][ (unsigned char)input[i] ]) { 0 58 58
296 0 txs_inc_counter(srch, chartable);
297 0 goto GT_RET;
298 }
299 }
300  
301 /*THIRD PASS:
302 * Check if the sequence up to the minimum prefix length is valid*/
303 16 ▶ if(!txs_ht_check(srch->ht_min, input, srch->min_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!txs_ht_check(srch->ht_min, input, srch->min_len)) { 0 16 16
304 0 txs_inc_counter(srch, hash_firstpass);
305 0 goto GT_RET;
306 }
307
308 /*FOURTH PASS:
309 * Check if we have a valid full prefix. If can_match ends up being true,
310 * it essentially means we will ALWAYS have a match*/
311 16 can_match = 0;
312 34 ▶ for(i = 0; i <= srch->term_lengths_max; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i <= srch->term_lengths_max; i++) { 32 2 34
313 32 term_len = srch->term_lengths[i];
314 32 ▶ if(term_len > input_len) {
Branch: 1 branch
BranchTrueFalseTotal
if(term_len > input_len) { 0 32 32
315 0 warn("Too short!");
316 0 break;
317 }
318
319 32 ▶ if(txs_ht_check(srch->ht_full, input, term_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if(txs_ht_check(srch->ht_full, input, term_len)) { 14 18 32
320 14 can_match = 1;
321 14 break;
322 }
323 }
324
325 16 ▶ if(!can_match) {
Branch: 1 branch
BranchTrueFalseTotal
if(!can_match) { 2 14 16
326 2 txs_inc_counter(srch, hash_secondpass);
327 2 goto GT_RET;
328 }
329  
330 14 match_len = term_len;
331 38 ▶ for(j = srch->term_lengths_max; j > i; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for(j = srch->term_lengths_max; j > i; j--) { 26 12 38
332 26 term_len = srch->term_lengths[j];
333 26 ▶ if(term_len > input_len) {
Branch: 1 branch
BranchTrueFalseTotal
if(term_len > input_len) { 3 23 26
334 3 continue;
335 }
336  
337 23 ▶ if(txs_ht_check(srch->ht_full, input, term_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if(txs_ht_check(srch->ht_full, input, term_len)) { 2 21 23
338 2 match_len = term_len;
339 2 break;
340 }
341 }
342 14 ret = newSVpv(input, match_len);
343 14 ▶ if(SvUTF8(input_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvUTF8(input_sv)) { 2 12 14
344 2 SvUTF8_on(ret);
345 }
346 14 goto GT_RET;
347
348 28 GT_RET:
349 28 return ret;
350 }
351  
352 #define _print_optimized(v) printf("%s: %d\n", #v, (Optimized_ ## v))
353  
354  
355 #define prefix_search_multi(mysv, input_strings) \
356 THX_prefix_search_multi(aTHX_ mysv, input_strings)
357 2 SV* THX_prefix_search_multi(pTHX_ SV* mysv, AV *input_strings)
358 {
359 2 int i = 0;
360 2 int max = av_len(input_strings);
361 2 HV *ret = newHV();
362
363 SV **cur_sv;
364 2 AV *tmpav = NULL;
365 2 SV *prefix = NULL;
366 2 HE *prefix_ret_ent = NULL;
367
368 14 ▶ for(i = 0; i <= max; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i <= max; i++) { 12 2 14
369 12 cur_sv = av_fetch(input_strings, i, 0);
370
371 24 ▶ if(!cur_sv || (!SvPV_nolen(*cur_sv)) ) {
Branch: 2 branches
BranchTrueFalseTotal
if(!cur_sv || (!SvPV_nolen(*cur_sv)) ) { 12 0 12
if(!cur_sv || (!SvPV_nolen(*cur_sv)) ) { 0 12 12
372 /*Non existent or not a string. We don't care about SvPV since it's
373 gonna get converted anyway*/
374 0 continue;
375 }
376
377 12 prefix = prefix_search(mysv, *cur_sv);
378 12 ▶ if(prefix == &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
if(prefix == &PL_sv_undef) { 7 5 12
379 7 continue;
380 }
381
382 5 prefix_ret_ent = hv_fetch_ent(ret, prefix, 0, 0);
383 5 ▶ if(!prefix_ret_ent) {
Branch: 1 branch
BranchTrueFalseTotal
if(!prefix_ret_ent) { 5 0 5
384 5 prefix_ret_ent = hv_store_ent(
385 ret, prefix, newRV_noinc((SV*)newAV()), 0);
386 }
387
388 5 tmpav = SvRV(HeVAL(prefix_ret_ent));
389 5 av_store(tmpav, av_len(tmpav)+1, newSVsv(*cur_sv));
390 }
391 2 return newRV_noinc((SV*)ret);
392 }
393  
394 #ifdef TXS_SUB_OP
395 static OP* TXS_OP_psearch(pTHX)
396 {
397 dXSARGS;
398 SV *input = POPs;
399 SV *mysv = POPs;
400 SV *ret = prefix_search(mysv, input);
401 PUSHs(ret);
402 XSRETURN(1);
403 }
404 #endif
405  
406 #define prefix_search_dump(mysv) THX_prefix_search_dump(aTHX_ mysv)
407 0 SV* THX_prefix_search_dump(pTHX_ SV *mysv)
408 {
409 0 ▶ if(!SvROK(mysv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(mysv)) { 0 0 0
410 0 die("Bad parameter!");
411 }
412
413 0 struct TXS_Search *srch = txs_search_from_sv(SvRV(mysv));
414 0 txs_dump_stats(srch);
415
416 0 printf("ht_min: ");
417 0 txs_ht_dump_stats(srch->ht_min);
418
419 0 printf("ht_full: ");
420 0 txs_ht_dump_stats(srch->ht_full);
421
422 0 return &PL_sv_undef;
423 }
424  
425 3 static int txs_freehook(pTHX_ SV *mysv, MAGIC *mg)
426 {
427 3 struct TXS_Search *srch = (struct TXS_Search*)mg->mg_ptr;
428 3 ▶ if(PL_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if(PL_dirty) { 0 3 3
429 0 return 0;
430 }
431
432 3 ▶ if(!srch) {
Branch: 1 branch
BranchTrueFalseTotal
if(!srch) { 0 3 3
433 0 warn("TXS_Search object has already been freed?");
434 0 return 0;
435 }
436
437 3 srch->refcount--;
438
439 //warn("srch->refcount: %d", srch->refcount);
440
441 3 ▶ if(!srch->refcount) {
Branch: 1 branch
BranchTrueFalseTotal
if(!srch->refcount) { 3 0 3
442 //warn("TXS: Search being destroyed..");
443 3 txs_ht_free(srch->ht_full);
444 3 txs_ht_free(srch->ht_min);
445 3 Safefree(srch);
446 3 mg->mg_ptr = NULL;
447 }
448 3 }
449  
450 0 static int txs_duphook(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
451 {
452 0 struct TXS_Search *srch = txs_search_from_sv(mg->mg_obj);
453 0 srch->refcount++;
454 0 }
455  
456 3 static void THX_txs_sv_init(pTHX_ SV *mysv, struct TXS_Search *srch)
457 {
458 3 MAGIC *mg = sv_magicext(mysv, mysv,
459 PERL_MAGIC_ext, &txs_vtbl,
460 (char*)srch, 0);
461 3 srch->refcount = 1;
462 3 mg->mg_flags |= MGf_DUP;
463 3 }
464  
465 MODULE = Text::Prefix::XS PACKAGE = Text::Prefix::XS
466  
467 BOOT:
468 {
469 #ifdef TXS_SUB_OP
470 sub_op_config_t c;
471 c.name = "psearch";
472 c.namelen = sizeof("psearch")-1;
473 c.pp = TXS_OP_psearch;
474 c.check = 0;
475 c.ud = NULL;
476 sub_op_register(aTHX_ &c);
477 #else
478 ;
479 #endif
480 }
481  
482 PROTOTYPES: DISABLE
483  
484  
485 SV *
486 prefix_search_build (av_terms)
487 AV * av_terms
488  
489 PROTOTYPES: ENABLE
490  
491 SV *
492 prefix_search (mysv, input)
493 SV * mysv
494 SV * input
495 PROTOTYPE: $$
496  
497  
498  
499 SV *
500 prefix_search_multi (mysv, input_strings)
501 SV * mysv
502 AV * input_strings
503 PROTOTYPE: $\@
504  
505 SV *prefix_search_dump (mysv)
506 SV * mysv
« txs_hash.h ↑ Summary