srl_str_dedupe.h

filter: Statementstmt 7.2% 9 / 125 Branchbran 2.0% 2 / 96 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 4.9% 11 / 221 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« srl_float.h ↑ Summary srl_compress.h »
1 #ifndef SRL_STR_DEDUPE_H_
2 #define SRL_STR_DEDUPE_H_
3  
4 /*
5 * Single open-addressed C hash table for Sereal::Encoder string deduplication
6 * (dedupe_strings and aliased_dedupe_strings).
7 *
8 * Design notes:
9 * - Stores buffer offsets relative to enc->buf.body_pos rather than copying
10 * string bytes or interning into Perl's PL_strtab via HV/HEK.
11 * - Because offsets are resolved dynamically as (buf->body_pos + ofs), output
12 * buffer reallocations (Renew) never invalidate table entries.
13 * - Every occupied slot has copy_ofs != 0 (V1 body offsets are >= 6 after the
14 * header; V2+ body and user-header offsets are >= 1 because body_pos is set
15 * to start_of_body - 1). Thus copy_ofs == 0 is a sound empty-slot sentinel.
16 * - Identical raw bytes with differing SvUTF8 flags are distinguished in the
17 * mixed hash, in slot->flags, and by the wire tag (SRL_HDR_STR_UTF8 vs
18 * SRL_HDR_SHORT_BINARY / SRL_HDR_BINARY), so binary and UTF-8 strings never
19 * collide.
20 */
21  
22 #include <string.h>
23 #include "assert.h"
24 #include "srl_inline.h"
25 #include "srl_common.h"
26 #include "srl_protocol.h"
27 #include "srl_buffer_types.h"
28  
29 #ifndef IS_SRL_HDR_SHORT_BINARY
30 #define IS_SRL_HDR_SHORT_BINARY(tag) \
31 (((tag) >= SRL_HDR_SHORT_BINARY_LOW) && ((tag) <= SRL_HDR_SHORT_BINARY_HIGH))
32 #endif
33  
34 #define SRL_STR_DEDUPE_INIT_CAP 128U
35 #define SRL_STR_DEDUPE_SHRINK_CAP 4096U
36 #define SRL_STR_DEDUPE_FLAG_UTF8 1U
37  
38 typedef struct {
39 UV copy_ofs; /* 0 = empty slot; >= 1 = offset of literal string in body */
40 UV alias_ofs; /* 0 = no alias target yet (e.g. first seen as hash key) */
41 U32 hash; /* PERL_HASH mixed with UTF-8 flag */
42 U32 flags; /* bit 0: SRL_STR_DEDUPE_FLAG_UTF8 */
43 } srl_str_dedupe_slot_t;
44  
45 typedef struct {
46 srl_str_dedupe_slot_t *slots;
47 UV cap; /* always a power of 2 when allocated */
48 UV mask; /* cap - 1 */
49 UV items; /* number of occupied slots */
50 } srl_str_dedupe_tbl_t;
51  
52 SRL_STATIC_INLINE U32
53 0 srl_str_dedupe_hash(const char *str, STRLEN len, U32 utf8_flag)
54 {
55 U32 h;
56 0 ▶ PERL_HASH(h, str ? str : "", len);
Branch: 3 branches
BranchTrueFalseTotal
PERL_HASH(h, str ? str : "", len); 0 0 0
PERL_HASH(h, str ? str : "", len); 0 0 0
PERL_HASH(h, str ? str : "", len); 0 0 0
57 0 ▶ return h ^ (utf8_flag ? 0x9e3779b9U : 0U);
Branch: 1 branch
BranchTrueFalseTotal
return h ^ (utf8_flag ? 0x9e3779b9U : 0U); 0 0 0
58 }
59  
60 /* Decode an unsigned LEB128 length varint previously written into enc->buf,
61 * bounded by [p, end) and guarded against STRLEN shift overflow on both
62 * 32-bit and 64-bit platforms. Returns 1 on success, 0 on malformed input. */
63 SRL_STATIC_INLINE int
64 0 srl_str_dedupe_read_varint(const srl_buffer_char *p,
65 const srl_buffer_char *end,
66 STRLEN *out_len,
67 const srl_buffer_char **out_next)
68 {
69 0 STRLEN val = 0;
70 0 unsigned int shift = 0;
71 0 const unsigned int max_shift = (unsigned int)(sizeof(STRLEN) * 8);
72  
73 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
74 0 U8 b = (U8)*p++;
75 0 U8 low = (U8)(b & 0x7FU);
76 0 ▶ if (expect_false(shift >= max_shift))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(shift >= max_shift)) 0 0 0
77 0 return 0;
78 0 ▶ if (expect_false(shift + 7 > max_shift
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(shift + 7 > max_shift 0 0 0
if (expect_false(shift + 7 > max_shift 0 0 0
if (expect_false(shift + 7 > max_shift 0 0 0
79 && (low >> (max_shift - shift)) != 0))
80 0 return 0;
81 0 val |= ((STRLEN)low << shift);
82 0 ▶ if (expect_true(!(b & 0x80U))) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_true(!(b & 0x80U))) { 0 0 0
83 0 *out_len = val;
84 0 *out_next = p;
85 0 return 1;
86 }
87 0 shift += 7;
88 }
89 0 return 0;
90 }
91  
92 /* Compare candidate (ptr, len, hash, utf8_flag) against the literal string
93 * stored in buf at (buf->body_pos + slot->copy_ofs). */
94 SRL_STATIC_INLINE int
95 0 srl_str_dedupe_slot_matches(const srl_buffer_t *buf,
96 const srl_str_dedupe_slot_t *slot,
97 const char *ptr, STRLEN len,
98 U32 hash, U32 utf8_flag)
99 {
100 const srl_buffer_char *entry;
101 const srl_buffer_char *end;
102 const srl_buffer_char *stored_ptr;
103 STRLEN stored_len;
104 U8 tag;
105  
106 0 ▶ if (slot->hash != hash || slot->flags != utf8_flag)
Branch: 2 branches
BranchTrueFalseTotal
if (slot->hash != hash || slot->flags != utf8_flag) 0 0 0
if (slot->hash != hash || slot->flags != utf8_flag) 0 0 0
107 0 return 0;
108  
109 0 ▶ if (expect_false(buf->pos <= buf->body_pos
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(buf->pos <= buf->body_pos 0 0 0
if (expect_false(buf->pos <= buf->body_pos 0 0 0
if (expect_false(buf->pos <= buf->body_pos 0 0 0
110 || slot->copy_ofs >= (UV)(buf->pos - buf->body_pos)))
111 0 return 0;
112  
113 0 entry = buf->body_pos + slot->copy_ofs;
114 0 end = buf->pos;
115 0 tag = (U8)(*(const U8 *)entry) & (U8)~SRL_HDR_TRACK_FLAG;
116  
117 0 ▶ if (utf8_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8_flag) { 0 0 0
118 0 ▶ if (expect_false(tag != SRL_HDR_STR_UTF8))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(tag != SRL_HDR_STR_UTF8)) 0 0 0
119 0 return 0;
120 0 ▶ if (expect_false(!srl_str_dedupe_read_varint(entry + 1, end,
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(!srl_str_dedupe_read_varint(entry + 1, end, 0 0 0
121 &stored_len, &stored_ptr)))
122 0 return 0;
123 0 ▶ } else if (IS_SRL_HDR_SHORT_BINARY(tag)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (IS_SRL_HDR_SHORT_BINARY(tag)) { 0 0 0
} else if (IS_SRL_HDR_SHORT_BINARY(tag)) { 0 0 0
124 0 stored_len = (STRLEN)SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(tag);
125 0 stored_ptr = entry + 1;
126 0 ▶ } else if (expect_true(tag == SRL_HDR_BINARY)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (expect_true(tag == SRL_HDR_BINARY)) { 0 0 0
127 0 ▶ if (expect_false(!srl_str_dedupe_read_varint(entry + 1, end,
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(!srl_str_dedupe_read_varint(entry + 1, end, 0 0 0
128 &stored_len, &stored_ptr)))
129 0 return 0;
130 } else {
131 0 return 0;
132 }
133  
134 0 ▶ if (stored_len != len)
Branch: 1 branch
BranchTrueFalseTotal
if (stored_len != len) 0 0 0
135 0 return 0;
136 0 ▶ if (expect_false((size_t)(end - stored_ptr) < (size_t)len))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false((size_t)(end - stored_ptr) < (size_t)len)) 0 0 0
137 0 return 0;
138  
139 0 ▶ return (len == 0) || (memcmp(stored_ptr, ptr, len) == 0);
Branch: 2 branches
BranchTrueFalseTotal
return (len == 0) || (memcmp(stored_ptr, ptr, len) == 0); 0 0 0
return (len == 0) || (memcmp(stored_ptr, ptr, len) == 0); 0 0 0
140 }
141  
142 SRL_STATIC_INLINE void
143 0 srl_str_dedupe_init(pTHX_ srl_str_dedupe_tbl_t *tbl, UV cap)
144 {
145 assert(cap >= 4 && (cap & (cap - 1)) == 0);
146 0 ▶ if (expect_false(cap > (~(size_t)0) / sizeof(srl_str_dedupe_slot_t)))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(cap > (~(size_t)0) / sizeof(srl_str_dedupe_slot_t))) 0 0 0
147 0 croak("String deduplication table size overflow");
148 0 ▶ Newxz(tbl->slots, cap, srl_str_dedupe_slot_t);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(tbl->slots, cap, srl_str_dedupe_slot_t); 0 0 0
149 0 ▶ if (expect_false(tbl->slots == NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(tbl->slots == NULL)) 0 0 0
150 0 croak("Out of memory");
151 0 tbl->cap = cap;
152 0 tbl->mask = cap - 1;
153 0 tbl->items = 0;
154 0 }
155  
156 SRL_STATIC_INLINE void
157 7 srl_str_dedupe_free(pTHX_ srl_str_dedupe_tbl_t *tbl)
158 {
159 PERL_UNUSED_CONTEXT;
160 7 ▶ if (tbl->slots != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tbl->slots != NULL) { 0 7 7
161 0 Safefree(tbl->slots);
162 0 tbl->slots = NULL;
163 }
164 7 tbl->cap = 0;
165 7 tbl->mask = 0;
166 7 tbl->items = 0;
167 7 }
168  
169 /* Reset the table between encodes (or between header data and body).
170 * If a reusable encoder previously grew past SRL_STR_DEDUPE_SHRINK_CAP and
171 * the most recent encode used < 12.5% of capacity, release the oversized
172 * array without allocating (safe during SAVEDESTRUCTOR_X cleanup); the next
173 * lookup will lazily allocate SRL_STR_DEDUPE_INIT_CAP slots on demand. */
174 SRL_STATIC_INLINE void
175 7 srl_str_dedupe_clear(pTHX_ srl_str_dedupe_tbl_t *tbl)
176 {
177 PERL_UNUSED_CONTEXT;
178 7 ▶ if (tbl->slots == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (tbl->slots == NULL) 7 0 7
179 7 return;
180  
181 0 ▶ if (tbl->cap > SRL_STR_DEDUPE_SHRINK_CAP
Branch: 1 branch
BranchTrueFalseTotal
if (tbl->cap > SRL_STR_DEDUPE_SHRINK_CAP 0 0 0
182 0 ▶ && tbl->items < (tbl->cap >> 3))
Branch: 1 branch
BranchTrueFalseTotal
&& tbl->items < (tbl->cap >> 3)) 0 0 0
183 {
184 0 srl_str_dedupe_free(aTHX_ tbl);
185 0 return;
186 }
187  
188 0 ▶ if (tbl->items > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (tbl->items > 0) { 0 0 0
189 0 ▶ Zero(tbl->slots, tbl->cap, srl_str_dedupe_slot_t);
Branch: 1 branch
BranchTrueFalseTotal
Zero(tbl->slots, tbl->cap, srl_str_dedupe_slot_t); 0 0 0
190 0 tbl->items = 0;
191 }
192 }
193  
194 SRL_STATIC_INLINE void
195 0 srl_str_dedupe_grow(pTHX_ srl_str_dedupe_tbl_t *tbl)
196 {
197 0 UV old_cap = tbl->cap;
198 0 srl_str_dedupe_slot_t *old_slots = tbl->slots;
199 srl_str_dedupe_slot_t *new_slots;
200 UV new_cap;
201 UV new_mask;
202 UV i;
203  
204 0 ▶ if (expect_false(old_cap > ((~(UV)0) >> 1)))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(old_cap > ((~(UV)0) >> 1))) 0 0 0
205 0 croak("String deduplication table size overflow");
206 0 new_cap = old_cap << 1;
207 0 ▶ if (expect_false(new_cap > (~(size_t)0) / sizeof(srl_str_dedupe_slot_t)))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(new_cap > (~(size_t)0) / sizeof(srl_str_dedupe_slot_t))) 0 0 0
208 0 croak("String deduplication table size overflow");
209  
210 0 new_mask = new_cap - 1;
211 0 ▶ Newxz(new_slots, new_cap, srl_str_dedupe_slot_t);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(new_slots, new_cap, srl_str_dedupe_slot_t); 0 0 0
212 0 ▶ if (expect_false(new_slots == NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(new_slots == NULL)) 0 0 0
213 0 croak("Out of memory");
214  
215 0 ▶ for (i = 0; i < old_cap; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < old_cap; i++) { 0 0 0
216 0 ▶ if (old_slots[i].copy_ofs != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (old_slots[i].copy_ofs != 0) { 0 0 0
217 0 UV idx = (UV)old_slots[i].hash & new_mask;
218 0 ▶ while (new_slots[idx].copy_ofs != 0)
Branch: 1 branch
BranchTrueFalseTotal
while (new_slots[idx].copy_ofs != 0) 0 0 0
219 0 idx = (idx + 1) & new_mask;
220 0 new_slots[idx] = old_slots[i];
221 }
222 }
223  
224 0 Safefree(old_slots);
225 0 tbl->slots = new_slots;
226 0 tbl->cap = new_cap;
227 0 tbl->mask = new_mask;
228 0 }
229  
230 /* Look up (str, len, is_utf8) in tbl, inserting a new slot with
231 * copy_ofs = current_ofs if absent. Sets *found = 1 on hit, 0 on insert. */
232 SRL_STATIC_INLINE srl_str_dedupe_slot_t *
233 0 srl_str_dedupe_lookup_or_insert(pTHX_ srl_str_dedupe_tbl_t *tbl,
234 const srl_buffer_t *buf,
235 const char *str, STRLEN len, U32 is_utf8,
236 UV current_ofs, int *found)
237 {
238 0 ▶ U32 utf8_flag = is_utf8 ? SRL_STR_DEDUPE_FLAG_UTF8 : 0U;
Branch: 1 branch
BranchTrueFalseTotal
U32 utf8_flag = is_utf8 ? SRL_STR_DEDUPE_FLAG_UTF8 : 0U; 0 0 0
239 0 U32 hash = srl_str_dedupe_hash(str, len, utf8_flag);
240 UV mask;
241 UV idx;
242 srl_str_dedupe_slot_t *slot;
243  
244 assert(current_ofs != 0);
245  
246 0 ▶ if (expect_false(tbl->slots == NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(tbl->slots == NULL)) 0 0 0
247 0 srl_str_dedupe_init(aTHX_ tbl, SRL_STR_DEDUPE_INIT_CAP);
248  
249 0 mask = tbl->mask;
250 0 idx = (UV)hash & mask;
251 for (;;) {
252 0 slot = &tbl->slots[idx];
253 0 ▶ if (slot->copy_ofs == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (slot->copy_ofs == 0) 0 0 0
254 0 break;
255 0 ▶ if (srl_str_dedupe_slot_matches(buf, slot, str, len, hash, utf8_flag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (srl_str_dedupe_slot_matches(buf, slot, str, len, hash, utf8_flag)) { 0 0 0
256 0 *found = 1;
257 0 return slot;
258 }
259 0 idx = (idx + 1) & mask;
260 }
261  
262 /* Keep load factor <= 75% (items < cap * 3 / 4) without integer overflow. */
263 0 ▶ if (expect_false(tbl->items >= tbl->cap - (tbl->cap >> 2))) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(tbl->items >= tbl->cap - (tbl->cap >> 2))) { 0 0 0
264 0 srl_str_dedupe_grow(aTHX_ tbl);
265 0 mask = tbl->mask;
266 0 idx = (UV)hash & mask;
267 0 ▶ while (tbl->slots[idx].copy_ofs != 0)
Branch: 1 branch
BranchTrueFalseTotal
while (tbl->slots[idx].copy_ofs != 0) 0 0 0
268 0 idx = (idx + 1) & mask;
269 0 slot = &tbl->slots[idx];
270 }
271  
272 0 slot->copy_ofs = current_ofs;
273 0 slot->alias_ofs = 0;
274 0 slot->hash = hash;
275 0 slot->flags = utf8_flag;
276 0 tbl->items++;
277 0 *found = 0;
278 0 return slot;
279 }
280  
281 #endif
« srl_float.h ↑ Summary srl_compress.h »