Skip to content

Commit de06ff5

Browse files
erichermantsee
authored andcommitted
Speed up assigning an IV to a previously cleared SV
This change simply avoids calling sv_upgrade (which will do a ton of work to determine source and destination types) in a hot code path where at that very location, we've already just done all the same leg work to figure out source and destination types of the SV type conversion. In this particularly common and simple variant (SVt_NULL to SVt_IV), it's easy to do much better than sv_upgrade. Micro-benchmark used to validate this: $ dumbbench -i50 --pin-frequency -- \ ./perl -Ilib -e 'for my $x (1..1000){my @A = (1..2000);}' Before: Rounded run time per iteration: 2.4311e-01 +/- 1.4e-04 (0.1%) After: Rounded run time per iteration: 1.99354e-01 +/- 5.5e-05 (0.0%) That's 18% faster. While the micro-benchmark is very heavy on exercising this code path, it's a common code path. This is expected to have some real-world impact. There are likely more, similar optimizations that could have similar impact. NB: A similar change to newSViv also speeds up newSViv significantly, but newSViv is hardly used in the core. On the other hand, XS modules exercise it a lot. The Sereal decoder would be significantly, positively affected by doing a similar change there.
1 parent dc6369e commit de06ff5

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

sv.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4307,7 +4307,13 @@ Perl_sv_setsv_flags(pTHX_ SV *dstr, SV* sstr, const I32 flags)
43074307
if (SvIOK(sstr)) {
43084308
switch (dtype) {
43094309
case SVt_NULL:
4310-
sv_upgrade(dstr, SVt_IV);
4310+
/* For performance, we inline promoting to type SVt_IV. */
4311+
/* We're starting from SVt_NULL, so provided that's
4312+
* actual 0, we don't have to unset any SV type flags
4313+
* to promote to SVt_IV. */
4314+
assert(SVt_NULL == 0);
4315+
SET_SVANY_FOR_BODYLESS_IV(dstr);
4316+
SvFLAGS(dstr) |= SVt_IV;
43114317
break;
43124318
case SVt_NV:
43134319
case SVt_PV:

0 commit comments

Comments
 (0)