line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#include |
2
|
|
|
|
|
|
|
#include |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
#define MERGE_CAN_ALIAS(flags, value) (!(flags & MERGE_COPY_SOURCE) && !SvROK(value)) |
5
|
|
|
|
|
|
|
#define MERGE_CAN_LAZY(flags, value) ((flags & MERGE_LAZY) && !SvROK(value)) |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
namespace xs { namespace lib { |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
static void _hash_merge (pTHX_ HV* dest, HV* source, IV flags); |
10
|
|
|
|
|
|
|
static void _array_merge (pTHX_ AV* dest, AV* source, IV flags); |
11
|
|
|
|
|
|
|
|
12
|
24
|
|
|
|
|
|
static inline void _elem_merge (pTHX_ SV* dest, SV* source, IV flags) { |
13
|
24
|
100
|
|
|
|
|
if (SvROK(source)) { |
14
|
18
|
|
|
|
|
|
uint8_t type = SvTYPE(SvRV(source)); |
15
|
18
|
100
|
|
|
|
|
if (type == SVt_PVHV && dest != NULL && SvROK(dest) && SvTYPE(SvRV(dest)) == type) { |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
16
|
5
|
|
|
|
|
|
_hash_merge(aTHX_ (HV*) SvRV(dest), (HV*) SvRV(source), flags); |
17
|
5
|
|
|
|
|
|
return; |
18
|
|
|
|
|
|
|
} |
19
|
13
|
100
|
|
|
|
|
else if (type == SVt_PVAV && (flags & MERGE_ARRAY_CM) && dest != NULL && SvROK(dest) && SvTYPE(SvRV(dest)) == type) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
20
|
9
|
|
|
|
|
|
_array_merge(aTHX_ (AV*) SvRV(dest), (AV*) SvRV(source), flags); |
21
|
9
|
|
|
|
|
|
return; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
4
|
100
|
|
|
|
|
if ((flags & MERGE_LAZY) && SvOK(dest)) return; |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
4
|
100
|
|
|
|
|
if (flags & MERGE_COPY_SOURCE) { // deep copy reference value |
27
|
3
|
|
|
|
|
|
SV* copy = newRV_noinc(clone(aTHX_ SvRV(source), false)); |
28
|
3
|
50
|
|
|
|
|
SvSetSV_nosteal(dest, copy); |
29
|
3
|
|
|
|
|
|
SvREFCNT_dec(copy); |
30
|
3
|
|
|
|
|
|
return; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
1
|
50
|
|
|
|
|
SvSetSV_nosteal(dest, source); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
else { |
36
|
6
|
100
|
|
|
|
|
if ((flags & MERGE_LAZY) && SvOK(dest)) return; |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
37
|
5
|
50
|
|
|
|
|
SvSetSV_nosteal(dest, source); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
19
|
|
|
|
|
|
static void _hash_merge (pTHX_ HV* dest, HV* source, IV flags) { |
42
|
19
|
|
|
|
|
|
STRLEN hvmax = HvMAX(source); |
43
|
19
|
|
|
|
|
|
HE** hvarr = HvARRAY(source); |
44
|
19
|
100
|
|
|
|
|
if (!hvarr) return; |
45
|
162
|
100
|
|
|
|
|
for (STRLEN i = 0; i <= hvmax; ++i) { |
46
|
|
|
|
|
|
|
const HE* entry; |
47
|
194
|
100
|
|
|
|
|
for (entry = hvarr[i]; entry; entry = HeNEXT(entry)) { |
48
|
50
|
|
|
|
|
|
HEK* hek = HeKEY_hek(entry); |
49
|
50
|
|
|
|
|
|
SV* valueSV = HeVAL(entry); |
50
|
50
|
100
|
|
|
|
|
if ((flags & MERGE_SKIP_UNDEF) && !SvOK(valueSV)) continue; // skip undefs |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
51
|
49
|
100
|
|
|
|
|
if ((flags & MERGE_DELETE_UNDEF) && !SvOK(valueSV)) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
52
|
2
|
|
|
|
|
|
hv_deletehek(dest, hek, G_DISCARD); |
53
|
2
|
|
|
|
|
|
continue; |
54
|
|
|
|
|
|
|
} |
55
|
47
|
100
|
|
|
|
|
if (MERGE_CAN_LAZY(flags, valueSV)) { |
|
|
100
|
|
|
|
|
|
56
|
6
|
|
|
|
|
|
SV** elemref = hv_fetchhek(dest, hek, 0); |
57
|
6
|
100
|
|
|
|
|
if (elemref != NULL && SvOK(*elemref)) continue; |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
58
|
|
|
|
|
|
|
} |
59
|
45
|
100
|
|
|
|
|
if (MERGE_CAN_ALIAS(flags, valueSV)) { // make aliases for simple values |
|
|
100
|
|
|
|
|
|
60
|
32
|
|
|
|
|
|
SvREFCNT_inc(valueSV); |
61
|
32
|
|
|
|
|
|
hv_storehek(dest, hek, valueSV); |
62
|
32
|
|
|
|
|
|
continue; |
63
|
|
|
|
|
|
|
} |
64
|
13
|
|
|
|
|
|
SV* destSV = *(hv_fetchhek(dest, hek, 1)); |
65
|
13
|
|
|
|
|
|
_elem_merge(aTHX_ destSV, valueSV, flags); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
9
|
|
|
|
|
|
static void _array_merge (pTHX_ AV* dest, AV* source, IV flags) { |
71
|
|
|
|
|
|
|
// we are using low-level code for AV for efficiency (it is 5-10x times faster) |
72
|
9
|
50
|
|
|
|
|
if (SvREADONLY(dest)) Perl_croak_no_modify(); |
73
|
9
|
|
|
|
|
|
SV** srclist = AvARRAY(source); |
74
|
9
|
|
|
|
|
|
SSize_t srcfill = AvFILLp(source); |
75
|
|
|
|
|
|
|
|
76
|
9
|
100
|
|
|
|
|
if (flags & MERGE_ARRAY_CONCAT) { |
77
|
2
|
|
|
|
|
|
SSize_t savei = AvFILLp(dest) + 1; |
78
|
2
|
|
|
|
|
|
av_extend(dest, savei + srcfill); |
79
|
2
|
|
|
|
|
|
SV** dstlist = AvARRAY(dest); |
80
|
2
|
50
|
|
|
|
|
if (flags & MERGE_COPY_SOURCE) { |
81
|
0
|
0
|
|
|
|
|
while (srcfill-- >= 0) { |
82
|
0
|
|
|
|
|
|
SV* elem = *srclist++; |
83
|
0
|
0
|
|
|
|
|
dstlist[savei++] = elem == NULL ? newSV(0) : clone(aTHX_ elem, false); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} else { |
86
|
8
|
100
|
|
|
|
|
while (srcfill-- >= 0) { |
87
|
6
|
|
|
|
|
|
SV* elem = *srclist++; |
88
|
6
|
50
|
|
|
|
|
if (elem == NULL) dstlist[savei++] = newSV(0); |
89
|
|
|
|
|
|
|
else { |
90
|
6
|
|
|
|
|
|
SvREFCNT_inc_simple_void_NN(elem); |
91
|
6
|
|
|
|
|
|
dstlist[savei++] = elem; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
2
|
|
|
|
|
|
AvFILLp(dest) = savei - 1; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
else { |
98
|
7
|
|
|
|
|
|
av_extend(dest, srcfill); |
99
|
7
|
|
|
|
|
|
SV** dstlist = AvARRAY(dest); |
100
|
26
|
100
|
|
|
|
|
for (int i = 0; i <= srcfill; ++i) { |
101
|
19
|
|
|
|
|
|
SV* elem = *srclist++; |
102
|
19
|
50
|
|
|
|
|
if (elem == NULL) continue; // skip empty slots |
103
|
19
|
100
|
|
|
|
|
if ((flags & MERGE_SKIP_UNDEF) && !SvOK(elem)) continue; // skip undefs |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
104
|
18
|
100
|
|
|
|
|
if (MERGE_CAN_LAZY(flags, elem) && dstlist[i] && SvOK(dstlist[i])) continue; |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
105
|
14
|
100
|
|
|
|
|
if (MERGE_CAN_ALIAS(flags, elem)) { // hardcode for speed - make aliases for simple values |
|
|
100
|
|
|
|
|
|
106
|
11
|
|
|
|
|
|
SvREFCNT_inc_simple_void_NN(elem); |
107
|
11
|
50
|
|
|
|
|
if (AvREAL(dest)) SvREFCNT_dec(dstlist[i]); |
108
|
11
|
|
|
|
|
|
dstlist[i] = elem; |
109
|
11
|
|
|
|
|
|
continue; |
110
|
|
|
|
|
|
|
} |
111
|
3
|
50
|
|
|
|
|
if (!dstlist[i]) dstlist[i] = newSV(0); |
112
|
3
|
|
|
|
|
|
_elem_merge(aTHX_ dstlist[i], elem, flags); |
113
|
|
|
|
|
|
|
} |
114
|
7
|
50
|
|
|
|
|
if (AvFILLp(dest) < srcfill) AvFILLp(dest) = srcfill; |
115
|
|
|
|
|
|
|
} |
116
|
9
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
17
|
|
|
|
|
|
HV* hash_merge (pTHX_ HV* dest, HV* source, IV flags) { |
119
|
17
|
100
|
|
|
|
|
if (!dest) dest = newHV(); |
120
|
15
|
100
|
|
|
|
|
else if (flags & MERGE_COPY_DEST) dest = (HV*)clone(aTHX_ (SV*)dest, false); |
121
|
17
|
100
|
|
|
|
|
if (source) _hash_merge(aTHX_ dest, source, flags); |
122
|
17
|
|
|
|
|
|
return dest; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
8
|
|
|
|
|
|
SV* merge (pTHX_ SV* dest, SV* source, IV flags) { |
126
|
8
|
100
|
|
|
|
|
if ((flags & MERGE_COPY) && dest) dest = clone(aTHX_ dest, false); |
|
|
50
|
|
|
|
|
|
127
|
8
|
50
|
|
|
|
|
if (!source) source = &PL_sv_undef; |
128
|
8
|
|
|
|
|
|
_elem_merge(aTHX_ dest, source, flags); |
129
|
8
|
|
|
|
|
|
return dest; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
44
|
50
|
|
|
|
|
}} |
|
|
50
|
|
|
|
|
|