On Tue, Apr 22, 2014 at 06:59:05PM +0000, monarch_dodra via Digitalmars-d wrote: > On Tuesday, 22 April 2014 at 18:47:16 UTC, Dmitry Olshansky wrote: > >22-Apr-2014 22:10, H. S. Teoh via Digitalmars-d пишет: [...] > >>The question is, is there a way to take a slice of the static array, > >>set the length to zero, and append to it with ~= such that when it > >>runs out of space in the static buffer, it will reallocate a longer > >>array on the GC heap? Or is this a bad idea? > >> > > > >Should be a canonical use case for ScopeBuffer > >https://github.com/D-Programming-Language/phobos/blob/master/std/internal/scopebuffer.d > > > >Except that it has crippled usability e.g. you need to call free > >manually. > > I've been working on a "ScopedAppender" that is *bit* slower than > ScopeBuffer, but can be used on any generic types, and is > nothrow/ctfe/pure/"sometimes safe". I'm giving it the "finishing > touches". > > But in the meantime, normal appender+clear can work: > > int[50] buf; > auto app = appender(buf[]); > app.clear(); > //app is ready for use. > > The "issue" though is that appender itself as a reference type, so > just declaring it allocates, which kind of gets in the way of setting > up a local scratch space to begin with.
Yeah, I'm already using appender for the general case of n items; right now I'm just trying to optimize for the very common case where there are only 1 or two items in the list by eliminating GC allocations for that case. The fact that appender allocates defeats the whole purpose. :-/ T -- BREAKFAST.COM halted...Cereal Port Not Responding. -- YHL