Skip to content

Commit

Permalink
[pmc] change StringBuilder overallocation
Browse files Browse the repository at this point in the history
by 1.5, not 2. And for >8192 onto the next block size.
The optimal overallocation is the golden ration 1.618, but we prefer int ops.
A difference is not measurable.
  • Loading branch information
Reini Urban committed Jan 23, 2015
1 parent c44d180 commit c868e4e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions ChangeLog
@@ -1,5 +1,6 @@
2014-02-17 release 7.1.0
- Core
+ StringBuilder optimizations. GH #1123
- Build
+ Optimize away ExtUtils::Command on posix systems. #1177
+ Fix cpu config values for gcc_cmpxchg to include atomic/gcc_x86.o on amd64.
Expand Down
20 changes: 15 additions & 5 deletions src/pmc/stringbuilder.pmc
@@ -1,5 +1,5 @@
/*
Copyright (C) 2010-2014, Parrot Foundation.
Copyright (C) 2010-2015, Parrot Foundation.

=head1 NAME

Expand Down Expand Up @@ -488,7 +488,8 @@ Returns length of currently built string.

=item C<static size_t calculate_capacity(PARROT_INTERP, size_t needed)>

Calculate capacity for string. We allocate double the amount needed.
Calculate capacity for string. We overallocate by 2 for smaller buffers
resp. onto the next block size.

=cut

Expand All @@ -501,9 +502,18 @@ calculate_capacity(SHIM_INTERP, size_t needed)
{
ASSERT_ARGS(calculate_capacity)

needed *= 2;
/* round up to 16 */
needed = (needed + 15) & ~15;
if (needed < 8192) {
#if 1
needed += needed/2; /* overallocate by 1.5 */
#else
needed *= 2; /* overallocate by 2 */
#endif
needed = (needed + 15) & ~15; /* round up to 16 */
}
else {
needed &= ~0xfff;
needed += 4096; /* next block */
}

return needed;
}
Expand Down

0 comments on commit c868e4e

Please sign in to comment.