Skip to content

Commit 52d3576

Browse files
committed
Make enlarge a local function inside reserve
1 parent 3d1d44d commit 52d3576

1 file changed

Lines changed: 32 additions & 35 deletions

File tree

src/dmd/backend/outbuf.d

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -82,46 +82,43 @@ struct Outbuffer
8282
*/
8383
void reserve(size_t nbytes)
8484
{
85-
// Keep small so it is inlined
86-
if (pend - p < nbytes)
87-
enlarge(nbytes);
88-
}
89-
90-
// Reserve nbytes in buffer
91-
@trusted
92-
private void enlarge(size_t nbytes)
93-
{
94-
pragma(inline, false); // do not inline slow path
95-
96-
debug assert(nbytes > pend - p, "You should call this function from reserve() only.");
97-
98-
if (buf is null)
99-
{
100-
// Special-case the overwhelmingly most frequent situation
101-
if (nbytes < 64) nbytes = 64;
102-
p = buf = cast(ubyte*) malloc(nbytes);
103-
pend = buf + nbytes;
104-
}
105-
else
85+
// non-inline function for the heavy/infrequent reallocation case
86+
@trusted static void enlarge(ref Outbuffer b, size_t nbytes)
10687
{
107-
const size_t used = p - buf;
108-
const size_t oldlen = pend - buf;
109-
// Ensure exponential growth, oldlen * 2 for small sizes, oldlen * 1.5 for big sizes
110-
const size_t minlen = oldlen + (oldlen >> (oldlen > 1024 * 64));
88+
pragma(inline, false); // do not inline slow path
89+
90+
if (b.buf is null)
91+
{
92+
// Special-case the overwhelmingly most frequent situation
93+
if (nbytes < 64) nbytes = 64;
94+
b.p = b.buf = cast(ubyte*) malloc(nbytes);
95+
b.pend = b.buf + nbytes;
96+
}
97+
else
98+
{
99+
const size_t used = b.p - b.buf;
100+
const size_t oldlen = b.pend - b.buf;
101+
// Ensure exponential growth, oldlen * 2 for small sizes, oldlen * 1.5 for big sizes
102+
const size_t minlen = oldlen + (oldlen >> (oldlen > 1024 * 64));
111103

112-
size_t len = used + nbytes;
113-
if (len < minlen)
114-
len = minlen;
115-
// Round up to cache line size
116-
len = (len + 63) & ~63;
104+
size_t len = used + nbytes;
105+
if (len < minlen)
106+
len = minlen;
107+
// Round up to cache line size
108+
len = (len + 63) & ~63;
117109

118-
buf = cast(ubyte*) realloc(buf, len);
110+
b.buf = cast(ubyte*) realloc(b.buf, len);
119111

120-
pend = buf + len;
121-
p = buf + used;
112+
b.pend = b.buf + len;
113+
b.p = b.buf + used;
114+
}
115+
if (!b.buf)
116+
err_nomem();
122117
}
123-
if (!buf)
124-
err_nomem();
118+
119+
// Keep small so it is inlined
120+
if (pend - p < nbytes)
121+
enlarge(this, nbytes);
125122
}
126123

127124
// Write n zeros; return pointer to start of zeros

0 commit comments

Comments
 (0)