Commit 75a9bf9
committed
make SvREFCNT_dec() more efficient
Historically, SvREFCNT_dec was just
#define SvREFCNT_dec(sv) sv_free((SV*)(sv))
then in 5.10.0, for GCC, the macro was partially inlined, avoiding a
function call for the refcnt > 1 case. Recently, the macro was turned into
an inline function, providing the function-call avoidance to other
platforms too. However, the macro/inline-function is quite big, and
appears over 500 times in the core source. Its action is logically
equivalent to:
if (sv) {
if (SvREFCNT(sv) > 1)
SvREFCNT(sv)--;
else if (SvREFCNT == 1) {
// normal case
SvREFCNT(sv)--;
sv_free2(sv);
}
else {
// exceptional case
sv_free(sv);
}
}
Where sv_free2() handles the "normal" quick cases, while sv_free()
handles the odd cases (e,g. a ref count already at 0 during global
destruction).
This means we have to plant code that potentially calls two different
subs, over 500 times.
This commit changes SvREFCNT_dec and sv_free2() to look like:
PERL_STATIC_INLINE void
S_SvREFCNT_dec(pTHX_ SV *sv)
{
if (sv) {
U32 rc = SvREFCNT(sv);
if (rc > 1)
SvREFCNT(sv) = rc - 1;
else
Perl_sv_free2(aTHX_ sv, rc);
}
}
Perl_sv_free2(pTHX_ SV *const sv, const U32 rc)
{
if (rc == 1) {
SvREFCNT(sv) = 0;
... do sv_clear, del_SV etc ...
return
}
/* handle exceptional rc == 0 */
...
}
So for the normal cases (rc > 1, rc == 1) there is the same amount of
testing and function calls, but the second test has been moved inside
the sv_free2() function.
This makes the perl executable about 10-15K smaller, and apparently a bit
faster (modulo the fact that most benchmarks are just measuring noise).
The refcount is passed as a second arg to sv_free2(), as on platforms
that pass the first few args in registers, it saves reading sv->sv_refcnt
again.1 parent b492a59 commit 75a9bf9
4 files changed
+72
-66
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1294 | 1294 | | |
1295 | 1295 | | |
1296 | 1296 | | |
1297 | | - | |
1298 | | - | |
1299 | | - | |
| 1297 | + | |
1300 | 1298 | | |
1301 | 1299 | | |
1302 | 1300 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
64 | 63 | | |
65 | 64 | | |
66 | 65 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3938 | 3938 | | |
3939 | 3939 | | |
3940 | 3940 | | |
3941 | | - | |
| 3941 | + | |
3942 | 3942 | | |
3943 | 3943 | | |
3944 | 3944 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6549 | 6549 | | |
6550 | 6550 | | |
6551 | 6551 | | |
6552 | | - | |
6553 | | - | |
6554 | | - | |
6555 | | - | |
6556 | | - | |
6557 | | - | |
6558 | | - | |
6559 | | - | |
6560 | | - | |
6561 | | - | |
6562 | | - | |
6563 | | - | |
6564 | | - | |
6565 | | - | |
6566 | | - | |
6567 | | - | |
6568 | | - | |
6569 | | - | |
6570 | | - | |
6571 | | - | |
6572 | | - | |
6573 | | - | |
6574 | | - | |
6575 | | - | |
6576 | | - | |
6577 | | - | |
6578 | | - | |
6579 | | - | |
6580 | | - | |
6581 | | - | |
6582 | | - | |
6583 | | - | |
6584 | | - | |
6585 | | - | |
6586 | | - | |
6587 | | - | |
6588 | | - | |
6589 | | - | |
6590 | | - | |
6591 | | - | |
6592 | | - | |
6593 | | - | |
6594 | | - | |
| 6552 | + | |
6595 | 6553 | | |
6596 | 6554 | | |
| 6555 | + | |
| 6556 | + | |
| 6557 | + | |
| 6558 | + | |
6597 | 6559 | | |
6598 | | - | |
| 6560 | + | |
6599 | 6561 | | |
6600 | 6562 | | |
6601 | 6563 | | |
6602 | 6564 | | |
6603 | 6565 | | |
| 6566 | + | |
| 6567 | + | |
| 6568 | + | |
| 6569 | + | |
6604 | 6570 | | |
6605 | | - | |
6606 | | - | |
6607 | | - | |
6608 | | - | |
6609 | | - | |
6610 | | - | |
| 6571 | + | |
| 6572 | + | |
| 6573 | + | |
| 6574 | + | |
| 6575 | + | |
| 6576 | + | |
6611 | 6577 | | |
| 6578 | + | |
| 6579 | + | |
| 6580 | + | |
| 6581 | + | |
| 6582 | + | |
| 6583 | + | |
| 6584 | + | |
| 6585 | + | |
| 6586 | + | |
| 6587 | + | |
| 6588 | + | |
| 6589 | + | |
| 6590 | + | |
| 6591 | + | |
| 6592 | + | |
| 6593 | + | |
| 6594 | + | |
| 6595 | + | |
| 6596 | + | |
| 6597 | + | |
| 6598 | + | |
6612 | 6599 | | |
6613 | | - | |
6614 | | - | |
6615 | | - | |
| 6600 | + | |
| 6601 | + | |
| 6602 | + | |
6616 | 6603 | | |
6617 | | - | |
6618 | | - | |
6619 | | - | |
| 6604 | + | |
| 6605 | + | |
| 6606 | + | |
| 6607 | + | |
| 6608 | + | |
| 6609 | + | |
| 6610 | + | |
| 6611 | + | |
| 6612 | + | |
| 6613 | + | |
| 6614 | + | |
| 6615 | + | |
| 6616 | + | |
| 6617 | + | |
| 6618 | + | |
| 6619 | + | |
| 6620 | + | |
| 6621 | + | |
| 6622 | + | |
| 6623 | + | |
| 6624 | + | |
| 6625 | + | |
| 6626 | + | |
| 6627 | + | |
6620 | 6628 | | |
6621 | 6629 | | |
| 6630 | + | |
6622 | 6631 | | |
6623 | 6632 | | |
6624 | 6633 | | |
| |||
0 commit comments