Allocating a wstring on the stack (no GC)?

Jesse Phillips via Digitalmars-d digitalmars-d at puremagic.com
Wed May 7 22:45:03 PDT 2014


On Wednesday, 7 May 2014 at 18:26:08 UTC, Maxime 
Chevalier-Boisvert wrote:
> I have a very specific use case (JIT compiler) in which I have 
> a pre-allocated array of wchar string data stored somewhere in 
> memory. I'd like to be able to create a temporary D wstring 
> object to pass this as a "regular" string to other functions. 
> For performance reasons, it would be preferable not to 
> dynamically allocate or copy any data. Dynamically allocating 
> the strings tends to trigger the D GC which severely impacts 
> the performance.
>
> So, my question is, would it be possible for me to allocate a 
> wstring object on the stack, and manually set its string data 
> pointer and length? If so, how? Your wizardly help is much 
> appreciated.

As mentioned a slice is the stack allocated view which you 
desire, however your selection of terms, "wchar string" and 
"wstring" suggest to me that you'll also be dealing with a 
conversion from mutable to immutable. To put my expectations of 
these terms into types:

wchar string : wchar[]
wstring : immutable(wchar)[]

If I have understood this correctly then you'll also want to look 
toward std.exception.assumeUnique() which you can call on a slice 
(wchar[]). However, you should not use this if you will maintain 
a mutable reference to your data.


More information about the Digitalmars-d mailing list