Skip to content

Commit 06dbfff

Browse files
richardleachdemerphq
authored andcommitted
PERL_STRLEN_NEW_MIN - increase to multiple of pointer sizes
Major malloc implementations, including the popular dlmalloc derivatives all return chunks of memory that are a multiple of the platform's pointer size. Perl's traditional default string allocation of 10 bytes will almost certainly result in a larger allocation than requested. Consequently, the interpreter may try to Renew() an allocation to increase the PV buffer size when it does not actually need to do so. This commit increases the default string size to the nearest pointer multiple. (12 bytes for 32-bit pointers, 16 bytes for 64-bit pointers). This is almost certainly unnecessarily small for 64-bit platforms, since most common malloc implementations seem to return 3*pointer size (i.e. 24 bytes) as the smallest allocation. However, 16 bytes was chosen to prevent an increase in memory usage in memory-constrained platforms which might have a smaller minimum memory allocation.
1 parent dbf3614 commit 06dbfff

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

perl.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,11 +1603,20 @@ Use L</UV> to declare variables of the maximum usable size on this platform.
16031603

16041604
/* Functions like Perl_sv_grow mandate a minimum string size.
16051605
* This was 10 bytes for a long time, the rationale for which seems lost
1606-
* to the mists of time. However, since this does not correlate to what
1607-
* modern malloc implementations will actually return, it can be revised
1608-
* to be more appropriate. */
1606+
* to the mists of time. However, this does not correlate to what modern
1607+
* malloc implementations will actually return, in particular the fact
1608+
* that chunks are almost certainly some multiple of pointer size. The
1609+
* default has therefore been revised to a more useful approximation.
1610+
* Notes: The following is specifically conservative for 64 bit, since
1611+
* most dlmalloc derivatives seem to serve a 3xPTRSIZE minimum chunk,
1612+
* so the below perhaps should be:
1613+
* ((PTRSIZE == 4) ? 12 : 24)
1614+
* Configure probes for malloc_good_size, malloc_actual_size etc.
1615+
* could be revised to record the actual minimum chunk size, to which
1616+
* PERL_STRLEN_NEW_MIN could then be set.
1617+
*/
16091618
#ifndef PERL_STRLEN_NEW_MIN
1610-
#define PERL_STRLEN_NEW_MIN 10
1619+
#define PERL_STRLEN_NEW_MIN ((PTRSIZE == 4) ? 12 : 16)
16111620
#endif
16121621

16131622
/* Round all values passed to malloc up, by default to a multiple of

0 commit comments

Comments
 (0)