line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#include "merge.h" |
2
|
|
|
|
|
|
|
#include |
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
|
100
|
|
|
|
|
if (SvROK(source)) { |
16
|
5018
|
|
|
|
|
|
uint8_t type = SvTYPE(SvRV(source)); |
17
|
5018
|
100
|
|
|
|
|
if (type == SVt_PVHV && dest != NULL && SvROK(dest) && SvTYPE(SvRV(dest)) == type) { |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
18
|
5005
|
|
|
|
|
|
_hash_merge(aTHX_ (HV*) SvRV(dest), (HV*) SvRV(source), flags, depth+1); |
19
|
5
|
|
|
|
|
|
return; |
20
|
|
|
|
|
|
|
} |
21
|
13
|
100
|
|
|
|
|
else if (type == SVt_PVAV && (flags & (MergeFlags::ARRAY_MERGE|MergeFlags::ARRAY_CONCAT)) && dest != NULL && SvROK(dest) && SvTYPE(SvRV(dest)) == type) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
22
|
9
|
|
|
|
|
|
_array_merge(aTHX_ (AV*) SvRV(dest), (AV*) SvRV(source), flags, depth+1); |
23
|
9
|
|
|
|
|
|
return; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
4
|
100
|
|
|
|
|
if ((flags & MergeFlags::LAZY) && SvOK(dest)) return; |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
4
|
100
|
|
|
|
|
if (flags & MergeFlags::COPY_SOURCE) { // deep copy reference value |
29
|
3
|
50
|
|
|
|
|
SV* copy = newRV_noinc(clone(SvRV(source), 0).detach()); |
|
|
50
|
|
|
|
|
|
30
|
3
|
50
|
|
|
|
|
SvSetSV_nosteal(dest, copy); |
|
|
50
|
|
|
|
|
|
31
|
3
|
50
|
|
|
|
|
SvREFCNT_dec(copy); |
32
|
3
|
|
|
|
|
|
return; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
1
|
50
|
|
|
|
|
SvSetSV_nosteal(dest, source); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
else { |
38
|
6
|
100
|
|
|
|
|
if ((flags & MergeFlags::LAZY) && SvOK(dest)) return; |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
39
|
24
|
50
|
|
|
|
|
SvSetSV_nosteal(dest, source); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
5022
|
|
|
|
|
|
static void _hash_merge (pTHX_ HV* dest, HV* source, IV flags, I32 depth) { |
44
|
5022
|
100
|
|
|
|
|
if (dest == source) return; // nothing to merge, avoid recursive cycle-reference self-merge |
45
|
10020
|
100
|
|
|
|
|
if (depth > MERGE_MAX_DEPTH) throw MERGE_DEPTH_ERROR; |
|
|
50
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
5019
|
|
|
|
|
|
STRLEN hvmax = HvMAX(source); |
48
|
5019
|
|
|
|
|
|
HE** hvarr = HvARRAY(source); |
49
|
5019
|
100
|
|
|
|
|
if (!hvarr) return; |
50
|
15165
|
100
|
|
|
|
|
for (STRLEN i = 0; i <= hvmax; ++i) { |
51
|
|
|
|
|
|
|
const HE* entry; |
52
|
15194
|
100
|
|
|
|
|
for (entry = hvarr[i]; entry; entry = HeNEXT(entry)) { |
53
|
5050
|
|
|
|
|
|
HEK* hek = HeKEY_hek(entry); |
54
|
5050
|
|
|
|
|
|
SV* valueSV = HeVAL(entry); |
55
|
5086
|
100
|
|
|
|
|
if ((flags & MergeFlags::SKIP_UNDEF) && !SvOK(valueSV)) continue; // skip undefs |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
56
|
5049
|
100
|
|
|
|
|
if ((flags & MergeFlags::DELETE_UNDEF) && !SvOK(valueSV)) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
57
|
2
|
50
|
|
|
|
|
hv_deletehek(dest, hek, G_DISCARD); |
58
|
2
|
|
|
|
|
|
continue; |
59
|
|
|
|
|
|
|
} |
60
|
5047
|
100
|
|
|
|
|
if (MERGE_CAN_LAZY(flags, valueSV)) { |
|
|
100
|
|
|
|
|
|
61
|
6
|
50
|
|
|
|
|
SV** elemref = hv_fetchhek(dest, hek, 0); |
62
|
6
|
100
|
|
|
|
|
if (elemref != NULL && SvOK(*elemref)) continue; |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
63
|
|
|
|
|
|
|
} |
64
|
5045
|
100
|
|
|
|
|
if (MERGE_CAN_ALIAS(flags, valueSV)) { // make aliases for simple values |
|
|
100
|
|
|
|
|
|
65
|
32
|
|
|
|
|
|
SvREFCNT_inc(valueSV); |
66
|
32
|
50
|
|
|
|
|
hv_storehek(dest, hek, valueSV); |
67
|
32
|
|
|
|
|
|
continue; |
68
|
|
|
|
|
|
|
} |
69
|
5013
|
50
|
|
|
|
|
SV* destSV = *(hv_fetchhek(dest, hek, 1)); |
70
|
5013
|
100
|
|
|
|
|
_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
|
50
|
|
|
|
|
if (dest == source) return; // nothing to merge, avoid recursive cycle-reference self-merge |
77
|
9
|
50
|
|
|
|
|
if (depth > MERGE_MAX_DEPTH) throw MERGE_DEPTH_ERROR; |
|
|
0
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
// we are using low-level code for AV for efficiency (it is 5-10x times faster) |
80
|
9
|
50
|
|
|
|
|
if (SvREADONLY(dest)) Perl_croak_no_modify(); |
81
|
9
|
|
|
|
|
|
SV** srclist = AvARRAY(source); |
82
|
9
|
|
|
|
|
|
SSize_t srcfill = AvFILLp(source); |
83
|
|
|
|
|
|
|
|
84
|
9
|
100
|
|
|
|
|
if (flags & MergeFlags::ARRAY_CONCAT) { |
85
|
2
|
|
|
|
|
|
SSize_t savei = AvFILLp(dest) + 1; |
86
|
2
|
|
|
|
|
|
av_extend(dest, savei + srcfill); |
87
|
2
|
|
|
|
|
|
SV** dstlist = AvARRAY(dest); |
88
|
2
|
50
|
|
|
|
|
if (flags & MergeFlags::COPY_SOURCE) { |
89
|
0
|
0
|
|
|
|
|
while (srcfill-- >= 0) { |
90
|
0
|
|
|
|
|
|
SV* elem = *srclist++; |
91
|
0
|
0
|
|
|
|
|
dstlist[savei++] = elem == NULL ? newSV(0) : clone(elem, 0).detach(); |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} else { |
94
|
8
|
100
|
|
|
|
|
while (srcfill-- >= 0) { |
95
|
6
|
|
|
|
|
|
SV* elem = *srclist++; |
96
|
6
|
50
|
|
|
|
|
if (elem == NULL) dstlist[savei++] = newSV(0); |
|
|
0
|
|
|
|
|
|
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
|
100
|
|
|
|
|
for (int i = 0; i <= srcfill; ++i) { |
109
|
19
|
|
|
|
|
|
SV* elem = *srclist++; |
110
|
35
|
50
|
|
|
|
|
if (elem == NULL) continue; // skip empty slots |
111
|
19
|
100
|
|
|
|
|
if ((flags & MergeFlags::SKIP_UNDEF) && !SvOK(elem)) continue; // skip undefs |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
112
|
18
|
100
|
|
|
|
|
if (MERGE_CAN_LAZY(flags, elem) && dstlist[i] && SvOK(dstlist[i])) continue; |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
113
|
14
|
100
|
|
|
|
|
if (MERGE_CAN_ALIAS(flags, elem)) { // hardcode for speed - make aliases for simple values |
|
|
100
|
|
|
|
|
|
114
|
11
|
|
|
|
|
|
SvREFCNT_inc_simple_void_NN(elem); |
115
|
11
|
50
|
|
|
|
|
if (AvREAL(dest)) SvREFCNT_dec(dstlist[i]); |
|
|
50
|
|
|
|
|
|
116
|
11
|
|
|
|
|
|
dstlist[i] = elem; |
117
|
11
|
|
|
|
|
|
continue; |
118
|
|
|
|
|
|
|
} |
119
|
3
|
50
|
|
|
|
|
if (!dstlist[i]) dstlist[i] = newSV(0); |
|
|
0
|
|
|
|
|
|
120
|
3
|
50
|
|
|
|
|
_elem_merge(aTHX_ dstlist[i], elem, flags, depth); |
121
|
|
|
|
|
|
|
} |
122
|
9
|
50
|
|
|
|
|
if (AvFILLp(dest) < srcfill) AvFILLp(dest) = srcfill; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
20
|
|
|
|
|
|
Hash merge (Hash dest, const Hash& source, int flags) { |
127
|
20
|
100
|
|
|
|
|
if (!dest) dest = Hash::create(); |
128
|
18
|
100
|
|
|
|
|
else if (flags & MergeFlags::COPY_DEST) dest = clone(dest, 0); |
|
|
50
|
|
|
|
|
|
129
|
20
|
100
|
|
|
|
|
if (source) _hash_merge(aTHX_ dest, source, flags, 1); |
130
|
19
|
|
|
|
|
|
return dest; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
Array merge (Array dest, const Array& source, int flags) { |
134
|
0
|
0
|
|
|
|
|
if (!dest) dest = Array::create(); |
135
|
0
|
0
|
|
|
|
|
else if (flags & MergeFlags::COPY_DEST) dest = clone(dest, 0); |
|
|
0
|
|
|
|
|
|
136
|
0
|
0
|
|
|
|
|
if (source) _array_merge(aTHX_ dest, source, flags | MergeFlags::ARRAY_MERGE, 1); |
137
|
0
|
|
|
|
|
|
return dest; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
8
|
|
|
|
|
|
Sv merge (Sv dest, const Sv& source, int flags) { |
141
|
8
|
100
|
|
|
|
|
if ((flags & MergeFlags::COPY_ALL) && dest) dest = clone(dest, 0); |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
142
|
8
|
50
|
|
|
|
|
_elem_merge(aTHX_ dest, source ? source.get() : &PL_sv_undef, flags, 0); |
143
|
8
|
|
|
|
|
|
return dest; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
} |