Skip to content

Commit bd158a3

Browse files
committed
Make enlarge a local function inside reserve
1 parent b9faefd commit bd158a3

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

src/dmd/backend/outbuf.d

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -82,46 +82,44 @@ 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)
94+
nbytes = 64;
95+
b.p = b.buf = cast(ubyte*) malloc(nbytes);
96+
b.pend = b.buf + nbytes;
97+
}
98+
else
99+
{
100+
const size_t used = b.p - b.buf;
101+
const size_t oldlen = b.pend - b.buf;
102+
// Ensure exponential growth, oldlen * 2 for small sizes, oldlen * 1.5 for big sizes
103+
const size_t minlen = oldlen + (oldlen >> (oldlen > 1024 * 64));
111104

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;
105+
size_t len = used + nbytes;
106+
if (len < minlen)
107+
len = minlen;
108+
// Round up to cache line size
109+
len = (len + 63) & ~63;
117110

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

120-
pend = buf + len;
121-
p = buf + used;
113+
b.pend = b.buf + len;
114+
b.p = b.buf + used;
115+
}
116+
if (!b.buf)
117+
err_nomem();
122118
}
123-
if (!buf)
124-
err_nomem();
119+
120+
// Keep small so it is inlined
121+
if (pend - p < nbytes)
122+
enlarge(this, nbytes);
125123
}
126124

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

0 commit comments

Comments
 (0)