Add a .reserve property to the basic D array type to ease runtime-efficient programming. reserve allows to handle arrays, that have allocated more space for elements than they hold according to their length. This would remove the need for the outbuffer module and similar attemps to gain performance.
The cost of 4 byte per array may look prohibitive but it shouldn't compared to existing overheads of 12-16 byte (in typical C malloc systems).
The overhead of an D array (estimated 16-20 bytes) should be measured empirically before making this suggestion.
- cost: 4 bytes for each array used
- cost: checks when changing length whether this needs a reallocation .
- gain: if the check is negative (reserve > length + delta) no operation is necessary. This is a big advantage.
- cost: about 20-50 lines for the additional logic
- gain: drop of the outbuffer module in Phobos (300 lines)
- gain: simplifies any programming for efficient array size change (growth)
It is common knowledge, that for writing fast code you have to avoiding all unnecessary memory allocations, reallocations and deallocations. This conflicts with OO practice because the creation and destruction of objects (und their object components) is made so easy and often happens unknow by the user. So programmers wanting to optimize they OO programs have to be aware of what is going on hidden by the language.
The inevitable need for performance often surfaces in special language features for efficient building of string from many small part:
also does. It allows to manage allocated array size (.reserve) and number of elements (.length) separately.
A number of library functions could profit from the increased efficiency.
The reading of a file in a single junk is currently the fastest way to read files:
Using existing and reuseable memory memory buffer this could be done faster:
Using reserve the function bufferread has important advantages:
- typically it needn't do any momory allocations.
- it can give the correct file length back in buffer.length without affecting (treducing) the buffer size.
- it can increase the buffer size if there should be a need.
- it can use standard syntax ("~") for array operations internally in case that reading is not done in one operation.