Skip to content

Commit 916215b

Browse files
committed
Make enlarge a local function inside reserve
1 parent 3d1d44d commit 916215b

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

src/dmd/backend/outbuf.d

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -82,46 +82,45 @@ 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-
}
85+
pragma(inline, true); // inline fast path
8986

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
87+
// non-inline function for the heavy/infrequent reallocation case
88+
@trusted static void enlarge(ref Outbuffer b, size_t nbytes)
10689
{
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));
90+
pragma(inline, false); // do not inline slow path
11191

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;
92+
if (b.buf is null)
93+
{
94+
// Special-case the overwhelmingly most frequent situation
95+
if (nbytes < 64) nbytes = 64;
96+
b.p = b.buf = cast(ubyte*) malloc(nbytes);
97+
b.pend = b.buf + nbytes;
98+
}
99+
else
100+
{
101+
const size_t used = b.p - b.buf;
102+
const size_t oldlen = b.pend - b.buf;
103+
// Ensure exponential growth, oldlen * 2 for small sizes, oldlen * 1.5 for big sizes
104+
const size_t minlen = oldlen + (oldlen >> (oldlen > 1024 * 64));
105+
106+
size_t len = used + nbytes;
107+
if (len < minlen)
108+
len = minlen;
109+
// Round up to cache line size
110+
len = (len + 63) & ~63;
117111

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

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

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

0 commit comments

Comments
 (0)