src/mds_buf.c

filter: Statementstmt 100% 66 / 66 Branchbran 67.5% 27 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.7% 93 / 106 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/mds_inline.c ↑ Summary src/mds.c »
1 #include "mds_buf.h"
2 #include <string.h>
3  
4 /* Measured output/input ratios per bench corpus:
5 * progit (prose) 1.04
6 * commonmark-spec 1.12
7 * synth-prose 1.21
8 * synth-tables 2.08 \u2190 dominates the high end
9 * Old heuristic was a flat 1.25\u00d7 + 64, which under-allocated by 2 grows
10 * on table-heavy inputs (a 1MB synth-tables corpus triggered two
11 * SvGROW realloc-copies of >1 MB each). New heuristic samples up to
12 * 4 KB of the head of the input to estimate pipe density; high pipe
13 * density (table corpora) gets 2.25\u00d7, everything else gets 1.50\u00d7.
14 * The 64-byte slack remains so tiny inputs never start at zero. */
15 164110 static size_t mds_buf_predict_cap(const char* s, size_t hint) {
16 size_t scan;
17 164110 size_t pipes = 0, newlines = 0;
18 int table_heavy;
19 size_t cap;
20 size_t i;
21  
22 164110 ▶ if (hint == 0) return 64;
Branch: 1 branch
BranchTrueFalseTotal
if (hint == 0) return 64; 3 164107 164110
23 164107 scan = hint < 4096 ? hint : 4096;
24 164107 ▶ if (s) {
Branch: 1 branch
BranchTrueFalseTotal
if (s) { 164107 0 164107
25 193797632 ▶ for (i = 0; i < scan; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < scan; i++) { 193633525 164107 193797632
26 193633525 unsigned char c = (unsigned char)s[i];
27 193633525 pipes += (c == '|');
28 193633525 newlines += (c == '\n');
29 }
30 }
31 /* Table heuristic: average >= 1 pipe per line in the sampled head. */
32 164107 ▶ table_heavy = (newlines && pipes >= newlines);
Branch: 2 branches
BranchTrueFalseTotal
table_heavy = (newlines && pipes >= newlines); 164081 26 164107
table_heavy = (newlines && pipes >= newlines); 73 164008 164081
33 164107 cap = table_heavy
34 73 ? hint + (hint >> 1) + (hint >> 2) + 64 /* ~2.25\u00d7 */
35 164107 ▶ : hint + (hint >> 1) + 64; /* ~1.50\u00d7 */
Branch: 1 branch
BranchTrueFalseTotal
: hint + (hint >> 1) + 64; /* ~1.50\u00d7 */ 73 164034 164107
36 164107 return cap;
37 }
38  
39 1 void mds_buf_init(pTHX_ mds_buf* b, SV* sv, size_t hint) {
40 size_t cap;
41 1 b->sv = sv;
42 1 cap = mds_buf_predict_cap(SvPVX(sv), hint);
43 /* Note: at this point sv has no PV yet; the predictor falls into the
44 * `s == NULL` branch and returns the prose-default 1.5\u00d7. Callers
45 * that want table-density sizing should call mds_buf_init_for_input
46 * below with the input pointer. */
47 1 ▶ SvUPGRADE(sv, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(sv, SVt_PV); 0 1 1
48 1 ▶ SvGROW(sv, cap);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(sv, cap); 1 0 1
SvGROW(sv, cap); 1 0 1
49 1 SvCUR_set(sv, 0);
50 1 SvPOK_on(sv);
51 1 b->base = SvPVX(sv);
52 1 b->cur = b->base;
53 1 b->end = b->base + SvLEN(sv) - 1; /* keep room for NUL */
54 1 *b->cur = '\0';
55 1 }
56  
57 164109 void mds_buf_init_for_input(pTHX_ mds_buf* b, SV* sv,
58 const char* input, size_t hint) {
59 size_t cap;
60 164109 b->sv = sv;
61 164109 cap = mds_buf_predict_cap(input, hint);
62 164109 ▶ SvUPGRADE(sv, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(sv, SVt_PV); 0 164109 164109
63 164109 ▶ SvGROW(sv, cap);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(sv, cap); 164109 0 164109
SvGROW(sv, cap); 164109 0 164109
64 164109 SvCUR_set(sv, 0);
65 164109 SvPOK_on(sv);
66 164109 b->base = SvPVX(sv);
67 164109 b->cur = b->base;
68 164109 b->end = b->base + SvLEN(sv) - 1;
69 164109 *b->cur = '\0';
70 164109 }
71  
72 1030348 MDS_COLD void mds_buf_reserve(pTHX_ mds_buf* b, size_t need) {
73 1030348 size_t used = (size_t)(b->cur - b->base);
74 1030348 size_t cap = SvLEN(b->sv);
75 1030348 size_t want = used + need + 1;
76 size_t newcap;
77 1030348 ▶ if (want <= cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (want <= cap) { 808051 222297 1030348
78 /* SvGROW may have been a no-op; just refresh end */
79 808051 b->end = b->base + cap - 1;
80 808051 return;
81 }
82 222297 ▶ newcap = cap ? cap : 64;
Branch: 1 branch
BranchTrueFalseTotal
newcap = cap ? cap : 64; 222297 0 222297
83 444594 ▶ while (newcap < want) newcap = newcap + (newcap >> 1) + 64; /* 1.5x */
Branch: 1 branch
BranchTrueFalseTotal
while (newcap < want) newcap = newcap + (newcap >> 1) + 64; /* 1.5x */ 222297 222297 444594
84 /* materialise current cursor into SvCUR so SvGROW preserves it */
85 222297 SvCUR_set(b->sv, used);
86 222297 ▶ SvGROW(b->sv, newcap);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(b->sv, newcap); 222297 0 222297
SvGROW(b->sv, newcap); 222297 0 222297
87 222297 b->base = SvPVX(b->sv);
88 222297 b->cur = b->base + used;
89 222297 b->end = b->base + SvLEN(b->sv) - 1;
90 }
91  
92 /* Splice `len` bytes into the middle of what has already been written.
93 *
94 * The heading renderer needs this: a heading's open tag carries an id slugged
95 * from the heading's own text, which is not known until the inline content
96 * has been rendered. Rather than divert the heading into a side buffer and
97 * lose every inline construct's interaction with the main one, the renderer
98 * notes the offset where the tag belongs, renders the content normally, and
99 * splices the finished tag back in here.
100 *
101 * `at` is a byte offset from the start of the buffer, not a pointer: reserve
102 * can realloc the SV out from under any pointer the caller was holding. */
103 808051 void mds_buf_insert(pTHX_ mds_buf* b, size_t at, const char* src, size_t len) {
104 size_t used;
105 808051 ▶ if (len == 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return; 0 808051 808051
106 808051 used = (size_t)(b->cur - b->base);
107 808051 ▶ if (at > used) return; /* nothing sensible to do */
Branch: 1 branch
BranchTrueFalseTotal
if (at > used) return; /* nothing sensible to do */ 0 808051 808051
108 808051 mds_buf_reserve(aTHX_ b, len); /* may move base */
109 808051 memmove(b->base + at + len, b->base + at, used - at);
110 808051 memcpy(b->base + at, src, len);
111 808051 b->cur += len;
112 }
113  
114 164110 void mds_buf_finalize(pTHX_ mds_buf* b) {
115 164110 size_t used = (size_t)(b->cur - b->base);
116 164110 SvCUR_set(b->sv, used);
117 164110 ▶ if (b->base) b->base[used] = '\0';
Branch: 1 branch
BranchTrueFalseTotal
if (b->base) b->base[used] = '\0'; 164110 0 164110
118 164110 SvPOK_on(b->sv);
119 164110 }
« src/mds_inline.c ↑ Summary src/mds.c »