XS.xs

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 106 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Text/Prefix/trie.pl ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 /*Assume 64 bit search prefixes*/
6  
7 #define TERMS_MAX 30
8 #define CHARTABLE_MAX 100
9  
10 #define str_bits_cmp(T, s1, s2) (*(T*)(s1) == *(T*)(s2))
11  
12 typedef char TXS_chartable_t[256];
13  
14 #define txs_search_from_sv(sv) SvPVX(sv)
15 #define terms_from_search(srch) \
16 (((char*)srch) + sizeof(struct TXS_Search))
17 struct TXS_String {
18 int len;
19 char *str;
20 };
21  
22 struct TXS_Search {
23 HV *trie;
24 AV *orig_terms;
25 int term_count;
26 int min_len;
27 TXS_chartable_t chartable[CHARTABLE_MAX];
28 };
29  
30 0 SV* prefix_search_build(AV *mortal_av)
31 {
32 0 int i = 0;
33 0 int max = av_len(mortal_av);
34 0 int my_len = sizeof(struct TXS_Search) + ( (sizeof(struct TXS_String)) * (max+1) );
35
36 0 SV *mysv = newSV(my_len);
37 AV *av_terms;
38
39 0 struct TXS_Search *srch = SvPVX(mysv);
40 0 struct TXS_String *terms = terms_from_search(srch);
41 0 Zero(srch->chartable, CHARTABLE_MAX, TXS_chartable_t);
42
43 0 srch->min_len = 100;
44 0 srch->term_count = 0;
45
46 0 srch->trie = newHV();
47 0 srch->orig_terms = newAV();
48 0 av_terms = srch->orig_terms;
49
50 0 ▶ for(i = 0; i <= max; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i <= max; i++) { 0 0 0
51 0 SV **old_sv = av_fetch(mortal_av, i, 0);
52 0 ▶ if(!old_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if(!old_sv) { 0 0 0
53 0 die("Terms list is partially empty at index %d", i);
54 }
55 0 ▶ if(SvROK(*old_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvROK(*old_sv)) { 0 0 0
56 0 die("Found reference SV at index %d", i);
57 }
58 0 av_store(av_terms, i, newSVsv(*old_sv));
59 }
60
61 //warn("Have %d terms", max+1);
62
63 0 ▶ for(i = 0; i <= max; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i <= max; i++) { 0 0 0
64 0 struct TXS_String *strp = &terms[i];
65
66 0 SV **a_term = av_fetch(av_terms, i, 0);
67 int term_len;
68 0 char *term_s = SvPV(*a_term, term_len);
69
70 0 strp->len = term_len;
71 0 strp->str = term_s;
72 //warn("Len is %d", strp->len);
73
74 int j;
75 0 ▶ for(j = term_len; j; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for(j = term_len; j; j--) { 0 0 0
76 0 hv_store(srch->trie, term_s, j, &PL_sv_undef, 0);
77 }
78 0 ▶ for(j = 0; j < term_len; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for(j = 0; j < term_len; j++) { 0 0 0
79 0 srch->chartable[j][term_s[j]] = 1;
80 }
81
82 0 ▶ if(srch->min_len > term_len) {
Branch: 1 branch
BranchTrueFalseTotal
if(srch->min_len > term_len) { 0 0 0
83 0 srch->min_len = term_len;
84 }
85 }
86
87 0 srch->term_count = max;
88 0 return newRV_noinc(mysv);
89 }
90  
91 static int Optimized_8 = 0;
92 static int Optimized_4 = 0;
93 static int Optimized_2 = 0;
94 static int Optimized_chartable = 0;
95 static int Optimized_hash = 0;
96  
97 0 SV* prefix_search(SV* mysv, char *input)
98 {
99 0 int i = 1;
100 int j, k;
101
102 0 SV *ret = &PL_sv_undef;
103
104 register struct TXS_String *strp;
105 register int strp_len;
106 0 register int input_len = strlen(input);
107 0 ▶ if(!SvROK(mysv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(mysv)) { 0 0 0
108 0 die("Not a valid search blob");
109 }
110 0 struct TXS_Search *srch = txs_search_from_sv(SvRV(mysv));
111 0 struct TXS_String *terms = terms_from_search(srch);
112
113 //warn("Input: %s", input);
114
115 0 ▶ if(input_len < srch->min_len) {
Branch: 1 branch
BranchTrueFalseTotal
if(input_len < srch->min_len) { 0 0 0
116 //warn("Input length is smaller than minimum length");
117 0 goto GT_RET;
118 }
119
120 0 ▶ for(i = 1; i <= srch->min_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 1; i <= srch->min_len; i++) { 0 0 0
121 0 ▶ if(!srch->chartable[ i-1 ][input[ i-1 ]]) {
Branch: 1 branch
BranchTrueFalseTotal
if(!srch->chartable[ i-1 ][input[ i-1 ]]) { 0 0 0
122 0 Optimized_chartable++;
123 0 return NULL;
124 }
125 0 ▶ if(!hv_exists(srch->trie, input, i)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!hv_exists(srch->trie, input, i)) { 0 0 0
126 0 Optimized_hash++;
127 0 goto GT_RET;
128 }
129 }
130
131 0 ▶ for(i = 0; i <= srch->term_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i <= srch->term_count; i++) { 0 0 0
132 0 strp = &terms[i];
133
134 0 ▶ if(input_len < strp->len) {
Branch: 1 branch
BranchTrueFalseTotal
if(input_len < strp->len) { 0 0 0
135 //warn("Term with length %d too long", strp->len);
136 0 continue;
137 }
138 /*
139 For some reason, this isn't as fast as i would have hoped for?
140 switch(strp->len) {
141 case 8:
142 Optimized_8++;
143 if(!str_bits_cmp(uint64_t, input, strp->str)) {
144 continue;
145 } else {
146 return strp->str;
147 }
148 break;
149 case 4:
150 Optimized_4++;
151 if(!str_bits_cmp(uint32_t, input, strp->str)) {
152 continue;
153 } else {
154 return strp->str;
155 }
156 break;
157 case 2:
158 Optimized_2++;
159 if(!str_bits_cmp(uint16_t, input, strp->str)) {
160 continue;
161 } else {
162 return strp->str;
163 }
164 break;
165 default:
166 break;
167 }
168  
169 if(strp->len > 8) {
170 if(!str_bits_cmp(uint64_t, input, strp->str)) {
171 continue;
172 }
173 goto GT_CMP;
174 }
175
176 else if(strp->len > 4) {
177 if(!str_bits_cmp(uint32_t, input, strp->str)) {
178 continue;
179 } else {
180
181 goto GT_CMP;
182 }
183 }
184
185 else if(strp->len > 2) {
186 if(!str_bits_cmp(uint16_t, input, strp->str)) {
187 continue;
188 } else {
189 goto GT_CMP;
190 }
191 }
192 */
193 0 GT_CMP:
194
195 0 ▶ if(strncmp(input, strp->str, strp->len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(strncmp(input, strp->str, strp->len) == 0) { 0 0 0
196 0 ret = newSVpv(strp->str, strp->len);
197 0 goto GT_RET;
198 }
199 }
200
201 0 GT_RET:
202 //if(ret != &PL_sv_undef) {
203 // warn("Found i=%d, match=%s\n", i, SvPV_nolen(ret));
204 //}
205 0 return ret;
206 }
207  
208 #define _print_optimized(v) printf("%s: %d\n", #v, (v))
209  
210 0 void print_optimized(char* foo)
211 {
212 0 _print_optimized(Optimized_2);
213 0 _print_optimized(Optimized_4);
214 0 _print_optimized(Optimized_8);
215 0 _print_optimized(Optimized_chartable);
216 0 _print_optimized(Optimized_hash);
217 0 }
218 MODULE = Text::Prefix::XS PACKAGE = Text::Prefix::XS
219  
220 PROTOTYPES: DISABLE
221  
222  
223 SV *
224 prefix_search_build (av_terms)
225 AV * av_terms
226  
227 SV *
228 prefix_search (mysv, input)
229 SV * mysv
230 char * input
231  
232 void
233 print_optimized (foo)
234 char * foo
235 PREINIT:
236 I32* temp;
237 PPCODE:
238 0 temp = PL_markstack_ptr++;
239 0 print_optimized(foo);
240 0 ▶ if (PL_markstack_ptr != temp) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_markstack_ptr != temp) { 0 0 0
241 /* truly void, because dXSARGS not invoked */
242 0 PL_markstack_ptr = temp;
243 0 XSRETURN_EMPTY; /* return empty stack */
244 }
245 /* must have used dXSARGS; list context implied */
246 0 return; /* assume stack size is correct */
247  
« blib/lib/Text/Prefix/trie.pl ↑ Summary