relative benefit of .reserve and .length

Eto Demerzel via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 2 14:57:52 PDT 2016


On Monday, 2 May 2016 at 14:47:01 UTC, Steven Schveighoffer wrote:
> On 4/29/16 6:10 AM, sigod wrote:
>> On Thursday, 28 April 2016 at 14:08:26 UTC, Steven 
>> Schveighoffer wrote:
>>> On 4/28/16 8:56 AM, Jay Norwood wrote:
>>>> [...]
>>>
>>> .reserve should make an improvement for large amount of 
>>> appending,
>>> since you pre-allocate the data.
>>>
>>> [...]
>>
>> How about `assumeSafeAppend`? Does it have any positive impact 
>> on
>> performance?
>
> I don't know why you would call it. assumeSafeAppend is an 
> expensive no-op if you have appendable array.
>
> -Steve

For example:

	auto invalid_tokens = 
uninitializedArray!(string[])(result.failure);
	invalid_tokens.length = 0;

	foreach (index, ref token_result; result.results) {
		if (token_result.error == "NotRegistered") {
			invalid_tokens.assumeSafeAppend() ~= tokens[index];
		}
		else ...
	}

	// use invalid_tokens

It would've been almost perfect code if `assumeSafeAppend` wasn't 
very costly. Now I need to declare separate length for 
`invalid_tokens`, so I can assign tokens by index. Which I don't 
like because arrays have `length` built into them.


More information about the Digitalmars-d-learn mailing list