D-Programming-Language/phobos — pull request #1410 by monarchdodra (2013-07-12 17:16:50+00:00)
Title: Appender.reserve: Overzealous and wrong

This contains two simple fixes:

== 1 ==
`Appender.reserve` is overzealous. Given a request of a capacity of `size`, it basically took the request, made *that* grow, and then reserved that. This results in things like:
```D
auto app = Appender!(int[]);
app.reserve(65_000);
writeln(app.capacity); //Prints abous 107_000 on my machine. Pointless.
```

The fix is for to make `ensureAddable`'s `newCapacity` to be based on the *old* capacity. Then, the final capacity is simply the max between the re-evaluated old=>new capacity, and the requested capacity.

== 2 ==
There was a little bug in reserve, where it called `ensureAddable(requestedSize - currentCapacity)` as opposed to `ensureAddable(requestedSize - currentSize)`. This for example, things like this failed:

A case that failed:
```
Appender with 32_000 element, and 64_000 capacity.
Request 80_000 capacity
=> ensureAddable(48_000)
=> do nothing
=> resulting capacity is actually smaller than 80_000