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(pTHX_ const char *str, STRLEN len, U32 utf8_flag)
54 {
55 U32 h;
56 /* Older threaded Perls keep the PERL_HASH seed in interpreter state. */
57 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
58 0 ▶ return h ^ (utf8_flag ? 0x9e3779b9U : 0U);
Branch: 1 branch
BranchTrueFalseTotal
return h ^ (utf8_flag ? 0x9e3779b9U : 0U); 0 0 0
59 }
60  
61 /* Decode an unsigned LEB128 length varint previously written into enc->buf,
62 * bounded by [p, end) and guarded against STRLEN shift overflow on both
63 * 32-bit and 64-bit platforms. Returns 1 on success, 0 on malformed input. */
64 SRL_STATIC_INLINE int
65 0 srl_str_dedupe_read_varint(const srl_buffer_char *p,
66 const srl_buffer_char *end,
67 STRLEN *out_len,
68 const srl_buffer_char **out_next)
69 {
70 0 STRLEN val = 0;
71 0 unsigned int shift = 0;
72 0 const unsigned int max_shift = (unsigned int)(sizeof(STRLEN) * 8);
73  
74 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
75 0 U8 b = (U8)*p++;
76 0 U8 low = (U8)(b & 0x7FU);
77 0 ▶ if (expect_false(shift >= max_shift))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(shift >= max_shift)) 0 0 0
78 0 return 0;
79 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
80 && (low >> (max_shift - shift)) != 0))
81 0 return 0;
82 0 val |= ((STRLEN)low << shift);
83 0 ▶ if (expect_true(!(b & 0x80U))) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_true(!(b & 0x80U))) { 0 0 0
84 0 *out_len = val;
85 0 *out_next = p;
86 0 return 1;
87 }
88 0 shift += 7;
89 }
90 0 return 0;
91 }
92  
93 /* Compare candidate (ptr, len, hash, utf8_flag) against the literal string
94 * stored in buf at (buf->body_pos + slot->copy_ofs). */
95 SRL_STATIC_INLINE int
96 0 srl_str_dedupe_slot_matches(const srl_buffer_t *buf,
97 const srl_str_dedupe_slot_t *slot,
98 const char *ptr, STRLEN len,
99 U32 hash, U32 utf8_flag)
100 {
101 const srl_buffer_char *entry;
102 const srl_buffer_char *end;
103 const srl_buffer_char *stored_ptr;
104 STRLEN stored_len;
105 U8 tag;
106  
107 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
108 0 return 0;
109  
110 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
111 || slot->copy_ofs >= (UV)(buf->pos - buf->body_pos)))
112 0 return 0;
113  
114 0 entry = buf->body_pos + slot->copy_ofs;
115 0 end = buf->pos;
116 0 tag = (U8)(*(const U8 *)entry) & (U8)~SRL_HDR_TRACK_FLAG;
117  
118 0 ▶ if (utf8_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8_flag) { 0 0 0
119 0 ▶ if (expect_false(tag != SRL_HDR_STR_UTF8))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(tag != SRL_HDR_STR_UTF8)) 0 0 0
120 0 return 0;
121 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
122 &stored_len, &stored_ptr)))
123 0 return 0;
124 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
125 0 stored_len = (STRLEN)SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(tag);
126 0 stored_ptr = entry + 1;
127 0 ▶ } else if (expect_true(tag == SRL_HDR_BINARY)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (expect_true(tag == SRL_HDR_BINARY)) { 0 0 0
128 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
129 &stored_len, &stored_ptr)))
130 0 return 0;
131 } else {
132 0 return 0;
133 }
134  
135 0 ▶ if (stored_len != len)
Branch: 1 branch
BranchTrueFalseTotal
if (stored_len != len) 0 0 0
136 0 return 0;
137 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
138 0 return 0;
139  
140 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
141 }
142  
143 SRL_STATIC_INLINE void
144 0 srl_str_dedupe_init(pTHX_ srl_str_dedupe_tbl_t *tbl, UV cap)
145 {
146 assert(cap >= 4 && (cap & (cap - 1)) == 0);
147 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
148 0 croak("String deduplication table size overflow");
149 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
150 0 ▶ if (expect_false(tbl->slots == NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(tbl->slots == NULL)) 0 0 0
151 0 croak("Out of memory");
152 0 tbl->cap = cap;
153 0 tbl->mask = cap - 1;
154 0 tbl->items = 0;
155 0 }
156  
157 SRL_STATIC_INLINE void
158 7 srl_str_dedupe_free(pTHX_ srl_str_dedupe_tbl_t *tbl)
159 {
160 PERL_UNUSED_CONTEXT;
161 7 ▶ if (tbl->slots != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tbl->slots != NULL) { 0 7 7
162 0 Safefree(tbl->slots);
163 0 tbl->slots = NULL;
164 }
165 7 tbl->cap = 0;
166 7 tbl->mask = 0;
167 7 tbl->items = 0;
168 7 }
169  
170 /* Reset the table between encodes (or between header data and body).
171 * If a reusable encoder previously grew past SRL_STR_DEDUPE_SHRINK_CAP and
172 * the most recent encode used < 12.5% of capacity, release the oversized
173 * array without allocating (safe during SAVEDESTRUCTOR_X cleanup); the next
174 * lookup will lazily allocate SRL_STR_DEDUPE_INIT_CAP slots on demand. */
175 SRL_STATIC_INLINE void
176 7 srl_str_dedupe_clear(pTHX_ srl_str_dedupe_tbl_t *tbl)
177 {
178 PERL_UNUSED_CONTEXT;
179 7 ▶ if (tbl->slots == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (tbl->slots == NULL) 7 0 7
180 7 return;
181  
182 0 ▶ if (tbl->cap > SRL_STR_DEDUPE_SHRINK_CAP
Branch: 1 branch
BranchTrueFalseTotal
if (tbl->cap > SRL_STR_DEDUPE_SHRINK_CAP 0 0 0
183 0 ▶ && tbl->items < (tbl->cap >> 3))
Branch: 1 branch
BranchTrueFalseTotal
&& tbl->items < (tbl->cap >> 3)) 0 0 0
184 {
185 0 srl_str_dedupe_free(aTHX_ tbl);
186 0 return;
187 }
188  
189 0 ▶ if (tbl->items > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (tbl->items > 0) { 0 0 0
190 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
191 0 tbl->items = 0;
192 }
193 }
194  
195 SRL_STATIC_INLINE void
196 0 srl_str_dedupe_grow(pTHX_ srl_str_dedupe_tbl_t *tbl)
197 {
198 0 UV old_cap = tbl->cap;
199 0 srl_str_dedupe_slot_t *old_slots = tbl->slots;
200 srl_str_dedupe_slot_t *new_slots;
201 UV new_cap;
202 UV new_mask;
203 UV i;
204  
205 0 ▶ if (expect_false(old_cap > ((~(UV)0) >> 1)))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(old_cap > ((~(UV)0) >> 1))) 0 0 0
206 0 croak("String deduplication table size overflow");
207 0 new_cap = old_cap << 1;
208 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
209 0 croak("String deduplication table size overflow");
210  
211 0 new_mask = new_cap - 1;
212 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
213 0 ▶ if (expect_false(new_slots == NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(new_slots == NULL)) 0 0 0
214 0 croak("Out of memory");
215  
216 0 ▶ for (i = 0; i < old_cap; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < old_cap; i++) { 0 0 0
217 0 ▶ if (old_slots[i].copy_ofs != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (old_slots[i].copy_ofs != 0) { 0 0 0
218 0 UV idx = (UV)old_slots[i].hash & new_mask;
219 0 ▶ while (new_slots[idx].copy_ofs != 0)
Branch: 1 branch
BranchTrueFalseTotal
while (new_slots[idx].copy_ofs != 0) 0 0 0
220 0 idx = (idx + 1) & new_mask;
221 0 new_slots[idx] = old_slots[i];
222 }
223 }
224  
225 0 Safefree(old_slots);
226 0 tbl->slots = new_slots;
227 0 tbl->cap = new_cap;
228 0 tbl->mask = new_mask;
229 0 }
230  
231 /* Look up (str, len, is_utf8) in tbl, inserting a new slot with
232 * copy_ofs = current_ofs if absent. Sets *found = 1 on hit, 0 on insert. */
233 SRL_STATIC_INLINE srl_str_dedupe_slot_t *
234 0 srl_str_dedupe_lookup_or_insert(pTHX_ srl_str_dedupe_tbl_t *tbl,
235 const srl_buffer_t *buf,
236 const char *str, STRLEN len, U32 is_utf8,
237 UV current_ofs, int *found)
238 {
239 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
240 0 U32 hash = srl_str_dedupe_hash(aTHX_ str, len, utf8_flag);
241 UV mask;
242 UV idx;
243 srl_str_dedupe_slot_t *slot;
244  
245 assert(current_ofs != 0);
246  
247 0 ▶ if (expect_false(tbl->slots == NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(tbl->slots == NULL)) 0 0 0
248 0 srl_str_dedupe_init(aTHX_ tbl, SRL_STR_DEDUPE_INIT_CAP);
249  
250 0 mask = tbl->mask;
251 0 idx = (UV)hash & mask;
252 for (;;) {
253 0 slot = &tbl->slots[idx];
254 0 ▶ if (slot->copy_ofs == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (slot->copy_ofs == 0) 0 0 0
255 0 break;
256 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
257 0 *found = 1;
258 0 return slot;
259 }
260 0 idx = (idx + 1) & mask;
261 }
262  
263 /* Keep load factor <= 75% (items < cap * 3 / 4) without integer overflow. */
264 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
265 0 srl_str_dedupe_grow(aTHX_ tbl);
266 0 mask = tbl->mask;
267 0 idx = (UV)hash & mask;
268 0 ▶ while (tbl->slots[idx].copy_ofs != 0)
Branch: 1 branch
BranchTrueFalseTotal
while (tbl->slots[idx].copy_ofs != 0) 0 0 0
269 0 idx = (idx + 1) & mask;
270 0 slot = &tbl->slots[idx];
271 }
272  
273 0 slot->copy_ofs = current_ofs;
274 0 slot->alias_ofs = 0;
275 0 slot->hash = hash;
276 0 slot->flags = utf8_flag;
277 0 tbl->items++;
278 0 *found = 0;
279 0 return slot;
280 }
281  
282 #endif
« srl_float.h ↑ Summary srl_compress.h »