src/xs/merge.cc

filter: Statementstmt 91.1% 82 / 90 Branchbran 64.0% 105 / 164 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.6% 187 / 254 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Recursive_xsgen.cc ↑ Summary src/xs/clone.cc »
1 #include "merge.h"
2 #include <xs/clone.h>
3  
4 #define MERGE_CAN_ALIAS(flags, value) (!(flags & MergeFlags::COPY_SOURCE) && !SvROK(value))
5 #define MERGE_CAN_LAZY(flags, value) ((flags & MergeFlags::LAZY) && !SvROK(value))
6 #define MERGE_MAX_DEPTH 5000
7 #define MERGE_DEPTH_ERROR std::invalid_argument("synchronous cycled reference in source and dest data detected")
8  
9 namespace xs {
10  
11 static void _hash_merge (pTHX_ HV* dest, HV* source, IV flags, I32 depth);
12 static void _array_merge (pTHX_ AV* dest, AV* source, IV flags, I32 depth);
13  
14 5024 static inline void _elem_merge (pTHX_ SV* dest, SV* source, IV flags, I32 depth) {
15 5024 ▶ if (SvROK(source)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(source)) { 5018 6 5024
16 5018 uint8_t type = SvTYPE(SvRV(source));
17 5018 ▶ if (type == SVt_PVHV && dest != NULL && SvROK(dest) && SvTYPE(SvRV(dest)) == type) {
Branch: 4 branches
BranchTrueFalseTotal
if (type == SVt_PVHV && dest != NULL && SvROK(dest) && SvTYPE(SvRV(dest)) == type) { 5006 12 5018
if (type == SVt_PVHV && dest != NULL && SvROK(dest) && SvTYPE(SvRV(dest)) == type) { 5006 0 5006
if (type == SVt_PVHV && dest != NULL && SvROK(dest) && SvTYPE(SvRV(dest)) == type) { 5005 1 5006
if (type == SVt_PVHV && dest != NULL && SvROK(dest) && SvTYPE(SvRV(dest)) == type) { 5005 0 5005
18 5005 _hash_merge(aTHX_ (HV*) SvRV(dest), (HV*) SvRV(source), flags, depth+1);
19 5 return;
20 }
21 13 ▶ else if (type == SVt_PVAV && (flags & (MergeFlags::ARRAY_MERGE|MergeFlags::ARRAY_CONCAT)) && dest != NULL && SvROK(dest) && SvTYPE(SvRV(dest)) == type) {
Branch: 5 branches
BranchTrueFalseTotal
else if (type == SVt_PVAV && (flags & (MergeFlags::ARRAY_MERGE|MergeFlags::ARRAY_CONCAT)) && dest != NULL && SvROK(dest) && SvTYPE(SvRV(dest)) == type) { 12 1 13
else if (type == SVt_PVAV && (flags & (MergeFlags::ARRAY_MERGE|MergeFlags::ARRAY_CONCAT)) && dest != NULL && SvROK(dest) && SvTYPE(SvRV(dest)) == type) { 10 2 12
else if (type == SVt_PVAV && (flags & (MergeFlags::ARRAY_MERGE|MergeFlags::ARRAY_CONCAT)) && dest != NULL && SvROK(dest) && SvTYPE(SvRV(dest)) == type) { 10 0 10
else if (type == SVt_PVAV && (flags & (MergeFlags::ARRAY_MERGE|MergeFlags::ARRAY_CONCAT)) && dest != NULL && SvROK(dest) && SvTYPE(SvRV(dest)) == type) { 9 1 10
else if (type == SVt_PVAV && (flags & (MergeFlags::ARRAY_MERGE|MergeFlags::ARRAY_CONCAT)) && dest != NULL && SvROK(dest) && SvTYPE(SvRV(dest)) == type) { 9 0 9
22 9 _array_merge(aTHX_ (AV*) SvRV(dest), (AV*) SvRV(source), flags, depth+1);
23 9 return;
24 }
25  
26 4 ▶ if ((flags & MergeFlags::LAZY) && SvOK(dest)) return;
Branch: 2 branches
BranchTrueFalseTotal
if ((flags & MergeFlags::LAZY) && SvOK(dest)) return; 2 2 4
if ((flags & MergeFlags::LAZY) && SvOK(dest)) return; 0 2 2
27  
28 4 ▶ if (flags & MergeFlags::COPY_SOURCE) { // deep copy reference value
Branch: 1 branch
BranchTrueFalseTotal
if (flags & MergeFlags::COPY_SOURCE) { // deep copy reference value 3 1 4
29 3 ▶ SV* copy = newRV_noinc(clone(SvRV(source), 0).detach());
Branch: 2 branches
BranchTrueFalseTotal
SV* copy = newRV_noinc(clone(SvRV(source), 0).detach()); 3 0 3
SV* copy = newRV_noinc(clone(SvRV(source), 0).detach()); 3 0 3
30 3 ▶ SvSetSV_nosteal(dest, copy);
Branch: 1 branch
BranchTrueFalseTotal
SvSetSV_nosteal(dest, copy); 3 0 3
31 3 SvREFCNT_dec(copy);
32 3 return;
33 }
34  
35 1 ▶ SvSetSV_nosteal(dest, source);
Branch: 1 branch
BranchTrueFalseTotal
SvSetSV_nosteal(dest, source); 1 0 1
36 }
37 else {
38 6 ▶ if ((flags & MergeFlags::LAZY) && SvOK(dest)) return;
Branch: 2 branches
BranchTrueFalseTotal
if ((flags & MergeFlags::LAZY) && SvOK(dest)) return; 2 4 6
if ((flags & MergeFlags::LAZY) && SvOK(dest)) return; 1 1 2
39 5 ▶ SvSetSV_nosteal(dest, source);
Branch: 1 branch
BranchTrueFalseTotal
SvSetSV_nosteal(dest, source); 5 0 5
40 }
41 }
42  
43 5022 static void _hash_merge (pTHX_ HV* dest, HV* source, IV flags, I32 depth) {
44 5022 ▶ if (dest == source) return; // nothing to merge, avoid recursive cycle-reference self-merge
Branch: 1 branch
BranchTrueFalseTotal
if (dest == source) return; // nothing to merge, avoid recursive cycle-reference self-merge 2 5020 5022
45 5020 ▶ if (depth > MERGE_MAX_DEPTH) throw MERGE_DEPTH_ERROR;
Branch: 2 branches
BranchTrueFalseTotal
if (depth > MERGE_MAX_DEPTH) throw MERGE_DEPTH_ERROR; 1 5019 5020
if (depth > MERGE_MAX_DEPTH) throw MERGE_DEPTH_ERROR; 1 0 1
46  
47 5019 STRLEN hvmax = HvMAX(source);
48 5019 HE** hvarr = HvARRAY(source);
49 5019 ▶ if (!hvarr) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hvarr) return; 1 5018 5019
50 20162 ▶ for (STRLEN i = 0; i <= hvmax; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN i = 0; i <= hvmax; ++i) { 20144 18 20162
51 const HE* entry;
52 20194 ▶ for (entry = hvarr[i]; entry; entry = HeNEXT(entry)) {
Branch: 1 branch
BranchTrueFalseTotal
for (entry = hvarr[i]; entry; entry = HeNEXT(entry)) { 5050 15144 20194
53 5050 HEK* hek = HeKEY_hek(entry);
54 5050 SV* valueSV = HeVAL(entry);
55 5050 ▶ if ((flags & MergeFlags::SKIP_UNDEF) && !SvOK(valueSV)) continue; // skip undefs
Branch: 2 branches
BranchTrueFalseTotal
if ((flags & MergeFlags::SKIP_UNDEF) && !SvOK(valueSV)) continue; // skip undefs 3 5047 5050
if ((flags & MergeFlags::SKIP_UNDEF) && !SvOK(valueSV)) continue; // skip undefs 1 2 3
56 5049 ▶ if ((flags & MergeFlags::DELETE_UNDEF) && !SvOK(valueSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((flags & MergeFlags::DELETE_UNDEF) && !SvOK(valueSV)) { 3 5046 5049
if ((flags & MergeFlags::DELETE_UNDEF) && !SvOK(valueSV)) { 2 1 3
57 2 hv_deletehek(dest, hek, G_DISCARD);
58 2 continue;
59 }
60 5047 ▶ if (MERGE_CAN_LAZY(flags, valueSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (MERGE_CAN_LAZY(flags, valueSV)) { 10 5037 5047
if (MERGE_CAN_LAZY(flags, valueSV)) { 6 4 10
61 6 SV** elemref = hv_fetchhek(dest, hek, 0);
62 6 ▶ if (elemref != NULL && SvOK(*elemref)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (elemref != NULL && SvOK(*elemref)) continue; 2 4 6
if (elemref != NULL && SvOK(*elemref)) continue; 2 0 2
63 }
64 5045 ▶ if (MERGE_CAN_ALIAS(flags, valueSV)) { // make aliases for simple values
Branch: 2 branches
BranchTrueFalseTotal
if (MERGE_CAN_ALIAS(flags, valueSV)) { // make aliases for simple values 5038 7 5045
if (MERGE_CAN_ALIAS(flags, valueSV)) { // make aliases for simple values 32 5006 5038
65 32 SvREFCNT_inc(valueSV);
66 32 hv_storehek(dest, hek, valueSV);
67 32 continue;
68 }
69 5013 SV* destSV = *(hv_fetchhek(dest, hek, 1));
70 5013 _elem_merge(aTHX_ destSV, valueSV, flags, depth);
71 }
72 }
73 }
74  
75 9 static void _array_merge (pTHX_ AV* dest, AV* source, IV flags, I32 depth) {
76 9 ▶ if (dest == source) return; // nothing to merge, avoid recursive cycle-reference self-merge
Branch: 1 branch
BranchTrueFalseTotal
if (dest == source) return; // nothing to merge, avoid recursive cycle-reference self-merge 0 9 9
77 9 ▶ if (depth > MERGE_MAX_DEPTH) throw MERGE_DEPTH_ERROR;
Branch: 2 branches
BranchTrueFalseTotal
if (depth > MERGE_MAX_DEPTH) throw MERGE_DEPTH_ERROR; 0 9 9
if (depth > MERGE_MAX_DEPTH) throw MERGE_DEPTH_ERROR; 0 0 0
78  
79 // we are using low-level code for AV for efficiency (it is 5-10x times faster)
80 9 ▶ if (SvREADONLY(dest)) Perl_croak_no_modify();
Branch: 1 branch
BranchTrueFalseTotal
if (SvREADONLY(dest)) Perl_croak_no_modify(); 0 9 9
81 9 SV** srclist = AvARRAY(source);
82 9 SSize_t srcfill = AvFILLp(source);
83  
84 9 ▶ if (flags & MergeFlags::ARRAY_CONCAT) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & MergeFlags::ARRAY_CONCAT) { 2 7 9
85 2 SSize_t savei = AvFILLp(dest) + 1;
86 2 av_extend(dest, savei + srcfill);
87 2 SV** dstlist = AvARRAY(dest);
88 2 ▶ if (flags & MergeFlags::COPY_SOURCE) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & MergeFlags::COPY_SOURCE) { 0 2 2
89 0 ▶ while (srcfill-- >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (srcfill-- >= 0) { 0 0 0
90 0 SV* elem = *srclist++;
91 0 ▶ dstlist[savei++] = elem == NULL ? newSV(0) : clone(elem, 0).detach();
Branch: 7 branches
BranchTrueFalseTotal
dstlist[savei++] = elem == NULL ? newSV(0) : clone(elem, 0).detach(); 0 0 0
dstlist[savei++] = elem == NULL ? newSV(0) : clone(elem, 0).detach(); 0 0 0
dstlist[savei++] = elem == NULL ? newSV(0) : clone(elem, 0).detach(); 0 0 0
dstlist[savei++] = elem == NULL ? newSV(0) : clone(elem, 0).detach(); 0 0 0
dstlist[savei++] = elem == NULL ? newSV(0) : clone(elem, 0).detach(); 0 0 0
dstlist[savei++] = elem == NULL ? newSV(0) : clone(elem, 0).detach(); 0 0 0
dstlist[savei++] = elem == NULL ? newSV(0) : clone(elem, 0).detach(); 0 0 0
92 }
93 } else {
94 8 ▶ while (srcfill-- >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (srcfill-- >= 0) { 6 2 8
95 6 SV* elem = *srclist++;
96 6 ▶ if (elem == NULL) dstlist[savei++] = newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (elem == NULL) dstlist[savei++] = newSV(0); 0 6 6
97 else {
98 6 SvREFCNT_inc_simple_void_NN(elem);
99 6 dstlist[savei++] = elem;
100 }
101 }
102 }
103 2 AvFILLp(dest) = savei - 1;
104 }
105 else {
106 7 av_extend(dest, srcfill);
107 7 SV** dstlist = AvARRAY(dest);
108 26 ▶ for (int i = 0; i <= srcfill; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i <= srcfill; ++i) { 19 7 26
109 19 SV* elem = *srclist++;
110 19 ▶ if (elem == NULL) continue; // skip empty slots
Branch: 1 branch
BranchTrueFalseTotal
if (elem == NULL) continue; // skip empty slots 0 19 19
111 19 ▶ if ((flags & MergeFlags::SKIP_UNDEF) && !SvOK(elem)) continue; // skip undefs
Branch: 2 branches
BranchTrueFalseTotal
if ((flags & MergeFlags::SKIP_UNDEF) && !SvOK(elem)) continue; // skip undefs 2 17 19
if ((flags & MergeFlags::SKIP_UNDEF) && !SvOK(elem)) continue; // skip undefs 1 1 2
112 18 ▶ if (MERGE_CAN_LAZY(flags, elem) && dstlist[i] && SvOK(dstlist[i])) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (MERGE_CAN_LAZY(flags, elem) && dstlist[i] && SvOK(dstlist[i])) continue; 6 12 18
if (MERGE_CAN_LAZY(flags, elem) && dstlist[i] && SvOK(dstlist[i])) continue; 4 2 6
if (MERGE_CAN_LAZY(flags, elem) && dstlist[i] && SvOK(dstlist[i])) continue; 4 0 4
if (MERGE_CAN_LAZY(flags, elem) && dstlist[i] && SvOK(dstlist[i])) continue; 4 0 4
113 14 ▶ if (MERGE_CAN_ALIAS(flags, elem)) { // hardcode for speed - make aliases for simple values
Branch: 2 branches
BranchTrueFalseTotal
if (MERGE_CAN_ALIAS(flags, elem)) { // hardcode for speed - make aliases for simple values 13 1 14
if (MERGE_CAN_ALIAS(flags, elem)) { // hardcode for speed - make aliases for simple values 11 2 13
114 11 SvREFCNT_inc_simple_void_NN(elem);
115 11 ▶ if (AvREAL(dest)) SvREFCNT_dec(dstlist[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (AvREAL(dest)) SvREFCNT_dec(dstlist[i]); 11 0 11
116 11 dstlist[i] = elem;
117 11 continue;
118 }
119 3 ▶ if (!dstlist[i]) dstlist[i] = newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!dstlist[i]) dstlist[i] = newSV(0); 0 3 3
120 3 _elem_merge(aTHX_ dstlist[i], elem, flags, depth);
121 }
122 7 ▶ if (AvFILLp(dest) < srcfill) AvFILLp(dest) = srcfill;
Branch: 1 branch
BranchTrueFalseTotal
if (AvFILLp(dest) < srcfill) AvFILLp(dest) = srcfill; 0 7 7
123 }
124 }
125  
126 20 Hash merge (Hash dest, const Hash& source, int flags) {
127 20 ▶ if (!dest) dest = Hash::create();
Branch: 2 branches
BranchTrueFalseTotal
if (!dest) dest = Hash::create(); 2 18 20
if (!dest) dest = Hash::create(); 2 0 2
128 18 ▶ else if (flags & MergeFlags::COPY_DEST) dest = clone(dest, 0);
Branch: 3 branches
BranchTrueFalseTotal
else if (flags & MergeFlags::COPY_DEST) dest = clone(dest, 0); 3 15 18
else if (flags & MergeFlags::COPY_DEST) dest = clone(dest, 0); 3 0 3
else if (flags & MergeFlags::COPY_DEST) dest = clone(dest, 0); 3 0 3
129 20 ▶ if (source) _hash_merge(aTHX_ dest, source, flags, 1);
Branch: 1 branch
BranchTrueFalseTotal
if (source) _hash_merge(aTHX_ dest, source, flags, 1); 17 3 20
130 19 return dest;
131 }
132  
133 0 Array merge (Array dest, const Array& source, int flags) {
134 0 ▶ if (!dest) dest = Array::create();
Branch: 2 branches
BranchTrueFalseTotal
if (!dest) dest = Array::create(); 0 0 0
if (!dest) dest = Array::create(); 0 0 0
135 0 ▶ else if (flags & MergeFlags::COPY_DEST) dest = clone(dest, 0);
Branch: 3 branches
BranchTrueFalseTotal
else if (flags & MergeFlags::COPY_DEST) dest = clone(dest, 0); 0 0 0
else if (flags & MergeFlags::COPY_DEST) dest = clone(dest, 0); 0 0 0
else if (flags & MergeFlags::COPY_DEST) dest = clone(dest, 0); 0 0 0
136 0 ▶ if (source) _array_merge(aTHX_ dest, source, flags | MergeFlags::ARRAY_MERGE, 1);
Branch: 1 branch
BranchTrueFalseTotal
if (source) _array_merge(aTHX_ dest, source, flags | MergeFlags::ARRAY_MERGE, 1); 0 0 0
137 0 return dest;
138 }
139  
140 8 Sv merge (Sv dest, const Sv& source, int flags) {
141 8 ▶ if ((flags & MergeFlags::COPY_ALL) && dest) dest = clone(dest, 0);
Branch: 4 branches
BranchTrueFalseTotal
if ((flags & MergeFlags::COPY_ALL) && dest) dest = clone(dest, 0); 1 7 8
if ((flags & MergeFlags::COPY_ALL) && dest) dest = clone(dest, 0); 1 0 1
if ((flags & MergeFlags::COPY_ALL) && dest) dest = clone(dest, 0); 1 7 8
if ((flags & MergeFlags::COPY_ALL) && dest) dest = clone(dest, 0); 1 0 1
142 8 ▶ _elem_merge(aTHX_ dest, source ? source.get() : &PL_sv_undef, flags, 0);
Branch: 1 branch
BranchTrueFalseTotal
_elem_merge(aTHX_ dest, source ? source.get() : &PL_sv_undef, flags, 0); 8 0 8
143 8 return dest;
144 }
145  
146 }
« Recursive_xsgen.cc ↑ Summary src/xs/clone.cc »