Skip to content

Commit 0a1b7a2

Browse files
committed
Perl_more_bodies - figure out sizing from sv_type
`Perl_more_bodies` allocates and sets up a new arena for the likes of SV body, HE, and HVAUX structs. It has traditionally been called with three arguments: * the `svtype` * the size of the structs that the arena will contain * the size of the arena to allocate This commit changes the function definition such that it only takes a single argument: the `svtype`. From that, and with a bit of additional logic to account of HE and HVAUX using the indexes notionally for SVt_NULL and SVt_IV, `Perl_more_bodies` can figure out the sizing for itself. The rationale for this is that: * When an application is starting up, the need to call `Perl_more_bodies` is the unlikely case in each new SV allocation or upgrade. * When the application has reached a steady state, the function may not be called regardless of how many SVs are created or upgraded. With `Perl_newSV_type` being an inline function, there are a lot of potential callers to this function though, each of which will have two `mov` instructions for pushing the two size parameters onto the stack should the need to call `Perl_more_bodies` arise. Removing two instructions from each potential call site should help reduce binary size a little and avoid taking up unnecessary space in the CPU's instruction buffer.
1 parent df4834b commit 0a1b7a2

File tree

5 files changed

+31
-22
lines changed

5 files changed

+31
-22
lines changed

embed.fnc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,9 +2043,7 @@ p |int |mode_from_discipline \
20432043
|STRLEN len
20442044

20452045
: Used in sv.c and hv.c
2046-
Cop |void * |more_bodies |const svtype sv_type \
2047-
|const size_t body_size \
2048-
|const size_t arena_size
2046+
Cop |void * |more_bodies |const svtype sv_type
20492047
Cp |const char *|moreswitches \
20502048
|NN const char *s
20512049
Adp |void |mortal_destructor_sv \

hv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ S_new_he(pTHX)
159159
void ** const root = &PL_body_roots[HE_ARENA_ROOT_IX];
160160

161161
if (!*root)
162-
Perl_more_bodies(aTHX_ HE_ARENA_ROOT_IX, sizeof(HE), PERL_ARENA_SIZE);
162+
Perl_more_bodies(aTHX_ HE_ARENA_ROOT_IX);
163163
he = (HE*) *root;
164164
assert(he);
165165
*root = HeNEXT(he);

proto.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sv.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -856,10 +856,22 @@ available in hv.c. Similarly SVt_IV is re-used for HVAUX_ARENA_ROOT_IX.
856856

857857

858858
void *
859-
Perl_more_bodies (pTHX_ const svtype sv_type, const size_t body_size,
860-
const size_t arena_size)
859+
Perl_more_bodies (pTHX_ const svtype sv_type)
861860
{
862861
void ** const root = &PL_body_roots[sv_type];
862+
863+
const struct body_details *type_details =
864+
(sv_type > SVt_IV)
865+
? bodies_by_type + sv_type
866+
: (sv_type == SVt_NULL)
867+
? NULL
868+
: &fake_hv_with_aux
869+
;
870+
871+
const size_t body_size = (type_details) ? type_details->body_size
872+
: sizeof(HE);
873+
const size_t arena_size = (type_details) ? type_details->arena_size
874+
: PERL_ARENA_SIZE;
863875
struct arena_desc *adesc;
864876
struct arena_set *aroot = (struct arena_set *) PL_body_arenas;
865877
unsigned int curr;
@@ -1291,7 +1303,7 @@ Perl_hv_auxalloc(pTHX_ HV *hv) {
12911303
#ifdef PURIFY
12921304
new_body = new_NOARENAZ(&fake_hv_with_aux);
12931305
#else
1294-
new_body_from_arena(new_body, HVAUX_ARENA_ROOT_IX, fake_hv_with_aux);
1306+
new_body_from_arena(new_body, HVAUX_ARENA_ROOT_IX);
12951307
#endif
12961308

12971309
old_body = SvANY(hv);
@@ -14799,7 +14811,7 @@ S_sv_dup_common(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
1479914811
#ifdef PURIFY
1480014812
new_body = new_NOARENA(sv_type_details);
1480114813
#else
14802-
new_body_from_arena(new_body, HVAUX_ARENA_ROOT_IX, fake_hv_with_aux);
14814+
new_body_from_arena(new_body, HVAUX_ARENA_ROOT_IX);
1480314815
#endif
1480414816
goto have_body;
1480514817
}

sv_inline.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ ALIGNED_TYPE(XPVOBJ);
180180
STRUCT_OFFSET(type, last_member) \
181181
+ sizeof (((type*)SvANY((const SV *)0))->last_member)
182182

183+
static const struct body_details fake_hv_with_aux =
184+
/* The SVt_IV arena is used for (larger) PVHV bodies. */
185+
{ sizeof(ALIGNED_TYPE_NAME(XPVHV_WITH_AUX)),
186+
copy_length(XPVHV, xhv_max),
187+
0,
188+
SVt_PVHV, TRUE, NONV, HASARENA,
189+
FIT_ARENA(0, sizeof(ALIGNED_TYPE_NAME(XPVHV_WITH_AUX))) };
190+
183191
static const struct body_details bodies_by_type[] = {
184192
/* HEs use this offset for their arena. */
185193
{ 0, 0, 0, SVt_NULL, FALSE, NONV, NOARENA, 0 },
@@ -328,21 +336,19 @@ static const struct body_details bodies_by_type[] = {
328336
#ifndef PURIFY
329337

330338
/* grab a new thing from the arena's free list, allocating more if necessary. */
331-
#define new_body_from_arena(xpv, root_index, type_meta) \
339+
#define new_body_from_arena(xpv, root_index) \
332340
STMT_START { \
333341
void ** const r3wt = &PL_body_roots[root_index]; \
334342
xpv = (PTR_TBL_ENT_t*) (*((void **)(r3wt)) \
335-
? *((void **)(r3wt)) : Perl_more_bodies(aTHX_ root_index, \
336-
type_meta.body_size,\
337-
type_meta.arena_size)); \
343+
? *((void **)(r3wt)) : Perl_more_bodies(aTHX_ root_index)); \
338344
*(r3wt) = *(void**)(xpv); \
339345
} STMT_END
340346

341347
PERL_STATIC_INLINE void *
342348
S_new_body(pTHX_ const svtype sv_type)
343349
{
344350
void *xpv;
345-
new_body_from_arena(xpv, sv_type, bodies_by_type[sv_type]);
351+
new_body_from_arena(xpv, sv_type);
346352
return xpv;
347353
}
348354

@@ -351,14 +357,6 @@ S_new_body(pTHX_ const svtype sv_type)
351357
static const struct body_details fake_rv =
352358
{ 0, 0, 0, SVt_IV, FALSE, NONV, NOARENA, 0 };
353359

354-
static const struct body_details fake_hv_with_aux =
355-
/* The SVt_IV arena is used for (larger) PVHV bodies. */
356-
{ sizeof(ALIGNED_TYPE_NAME(XPVHV_WITH_AUX)),
357-
copy_length(XPVHV, xhv_max),
358-
0,
359-
SVt_PVHV, TRUE, NONV, HASARENA,
360-
FIT_ARENA(0, sizeof(ALIGNED_TYPE_NAME(XPVHV_WITH_AUX))) };
361-
362360
/*
363361
=for apidoc newSV_type
364362
@@ -381,6 +379,7 @@ Perl_newSV_type(pTHX_ const svtype type)
381379

382380
SvFLAGS(sv) &= ~SVTYPEMASK;
383381
SvFLAGS(sv) |= type;
382+
// SvFLAGS(sv) = type;
384383

385384
switch (type) {
386385
case SVt_NULL:

0 commit comments

Comments
 (0)