Skip to content

Commit d5cf193

Browse files
committed
sv_upgrade: c99 compound literals to initialize xpvav/xpvhv
For both the AV and HV cases, the new body is Zero()ed but then various struct members are set to non-zero values. Now that we support parts of c99, it seems more efficient to use compound literals to initailize the struct members. With gcc v8.3.0, the compiled function is smaller by 25 instructions. sv.o is slightly smaller, but the final perl binary size is unchanged.
1 parent 5d7bd18 commit d5cf193

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

sv.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,26 +1359,33 @@ Perl_sv_upgrade(pTHX_ SV *const sv, svtype new_type)
13591359
assert(new_type_details->arena_size);
13601360
/* This points to the start of the allocated area. */
13611361
new_body = S_new_body(aTHX_ new_type);
1362-
Zero(new_body, new_type_details->body_size, char);
1363-
new_body = ((char *)new_body) - new_type_details->offset;
1362+
/* xpvav and xpvhv have no offset, so no need to adjust new_body */
1363+
assert(!(new_type_details->offset));
13641364
#else
13651365
/* We always allocated the full length item with PURIFY. To do this
13661366
we fake things so that arena is false for all 16 types.. */
13671367
new_body = new_NOARENAZ(new_type_details);
13681368
#endif
13691369
SvANY(sv) = new_body;
13701370
if (new_type == SVt_PVAV) {
1371-
AvMAX(sv) = -1;
1372-
AvFILLp(sv) = -1;
1371+
*((XPVAV*) SvANY(sv)) = (XPVAV) {
1372+
.xmg_stash = NULL, {.xmg_magic = NULL}, .xav_fill = -1,
1373+
.xav_max = -1, .xav_alloc = 0
1374+
};
1375+
13731376
AvREAL_only(sv);
13741377
} else {
1378+
*((XPVHV*) SvANY(sv)) = (XPVHV) {
1379+
.xmg_stash = NULL, {.xmg_magic = NULL}, .xhv_keys = 0,
1380+
/* start with PERL_HASH_DEFAULT_HvMAX+1 buckets: */
1381+
.xhv_max = PERL_HASH_DEFAULT_HvMAX
1382+
};
1383+
13751384
assert(!SvOK(sv));
13761385
SvOK_off(sv);
13771386
#ifndef NODEFAULT_SHAREKEYS
13781387
HvSHAREKEYS_on(sv); /* key-sharing on by default */
13791388
#endif
1380-
/* start with PERL_HASH_DEFAULT_HvMAX+1 buckets: */
1381-
HvMAX(sv) = PERL_HASH_DEFAULT_HvMAX;
13821389
}
13831390

13841391
/* SVt_NULL isn't the only thing upgraded to AV or HV.

0 commit comments

Comments
 (0)