Skip to content

Commit 87788cd

Browse files
committed
Time::HiRes.xs remove or collapse redundant EXTEND()s
-gettimeofday() EXTEND is only need if > 1 retval b/c pp_entersub promises @_ 1 slot, lift C stack memory var values to registers, this way if gettimeofday() is a static P5P written polyfill, and if the CC decides to inline it, the struct timeval Tp; C stack var will optimize away -setitimer() min 2 incoming args + PPCODE: is proof we have atleast 2 retval slots -getitimer() 1 in arg + PPCODE: is proof we have atleast 1 retval slot -utime() don't execute SvNV() over and over, don't exec sv_2io() 2x, add SvPV_const() for anti-de-COW future-proofing
1 parent 85750a8 commit 87788cd

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

dist/Time-HiRes/HiRes.xs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,17 +1313,21 @@ void
13131313
gettimeofday()
13141314
PREINIT:
13151315
struct timeval Tp;
1316-
PPCODE:
13171316
int status;
1317+
U8 is_G_LIST = GIMME_V == G_LIST;
1318+
PPCODE:
1319+
if (is_G_LIST)
1320+
EXTEND(sp, 2);
13181321
status = gettimeofday (&Tp, NULL);
13191322
if (status == 0) {
1320-
if (GIMME_V == G_LIST) {
1321-
EXTEND(sp, 2);
1322-
PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
1323-
PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
1323+
if (is_G_LIST) { /* copy to registers to prove sv_2mortal/newSViv */
1324+
IV sec = Tp.tv_sec; /* can't modify the values */
1325+
IV usec = Tp.tv_usec;
1326+
PUSHs(sv_2mortal(newSViv(sec)));
1327+
PUSHs(sv_2mortal(newSViv(usec)));
13241328
} else {
1325-
EXTEND(sp, 1);
1326-
PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6))));
1329+
NV nv = Tp.tv_sec + (Tp.tv_usec / NV_1E6);
1330+
PUSHs(sv_2mortal(newSVnv(nv)));
13271331
}
13281332
}
13291333

@@ -1373,10 +1377,8 @@ setitimer(which, seconds, interval = 0)
13731377
*/
13741378
GCC_DIAG_IGNORE_CPP_COMPAT_STMT;
13751379
if (setitimer(which, &newit, &oldit) == 0) {
1376-
EXTEND(sp, 1);
13771380
PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
13781381
if (GIMME_V == G_LIST) {
1379-
EXTEND(sp, 1);
13801382
PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
13811383
}
13821384
}
@@ -1393,7 +1395,6 @@ getitimer(which)
13931395
*/
13941396
GCC_DIAG_IGNORE_CPP_COMPAT_STMT;
13951397
if (getitimer(which, &nowit) == 0) {
1396-
EXTEND(sp, 1);
13971398
PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
13981399
if (GIMME_V == G_LIST) {
13991400
EXTEND(sp, 1);
@@ -1427,28 +1428,31 @@ PROTOTYPE: $$@
14271428
if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
14281429
utbufp = NULL;
14291430
else {
1430-
if (SvNV(accessed) < 0.0 || SvNV(modified) < 0.0)
1431+
NV modified_nv = SvNV(modified);
1432+
NV accessed_nv = SvNV(accessed);
1433+
if (accessed_nv < 0.0 || modified_nv < 0.0)
14311434
croak("%s(%" NVgf ", %" NVgf "%s", "Time::HiRes::utime",
1432-
SvNV(accessed), SvNV(modified),
1435+
accessed_nv, modified_nv,
14331436
"): negative time not invented yet");
14341437
Zero(&utbuf, sizeof utbuf, char);
14351438

1436-
utbuf[0].tv_sec = (Time_t)SvNV(accessed); /* time accessed */
1439+
utbuf[0].tv_sec = (Time_t)accessed_nv; /* time accessed */
14371440
utbuf[0].tv_nsec = (long)(
1438-
(SvNV(accessed) - (NV)utbuf[0].tv_sec)
1441+
(accessed_nv - (NV)utbuf[0].tv_sec)
14391442
* NV_1E9 + (NV)0.5);
14401443

1441-
utbuf[1].tv_sec = (Time_t)SvNV(modified); /* time modified */
1444+
utbuf[1].tv_sec = (Time_t)modified_nv; /* time modified */
14421445
utbuf[1].tv_nsec = (long)(
1443-
(SvNV(modified) - (NV)utbuf[1].tv_sec)
1446+
(modified_nv - (NV)utbuf[1].tv_sec)
14441447
* NV_1E9 + (NV)0.5);
14451448
}
14461449

14471450
while (items > 0) {
1451+
PerlIO * pio;
14481452
file = POPs; items--;
14491453

1450-
if (SvROK(file) && GvIO(SvRV(file)) && IoIFP(sv_2io(SvRV(file)))) {
1451-
int fd = PerlIO_fileno(IoIFP(sv_2io(file)));
1454+
if (SvROK(file) && GvIO(SvRV(file)) && (pio = IoIFP(sv_2io(SvRV(file))))) {
1455+
int fd = PerlIO_fileno(pio);
14521456
if (fd < 0) {
14531457
SETERRNO(EBADF,RMS_IFI);
14541458
} else {
@@ -1469,7 +1473,7 @@ PROTOTYPE: $$@
14691473
# ifdef HAS_UTIMENSAT
14701474
if (UTIMENSAT_AVAILABLE) {
14711475
STRLEN len;
1472-
char * name = SvPV(file, len);
1476+
const char * name = SvPV_const(file, len);
14731477
if (IS_SAFE_PATHNAME(name, len, "utime") &&
14741478
utimensat(AT_FDCWD, name, utbufp, 0) == 0) {
14751479

0 commit comments

Comments
 (0)