#include #include #include void baseline() { string x = "hi"; string y = x; // construct y in same context, no write yet => no copy yet assert( y.inner->Handle.s == x.inner->Handle.s); sout | y; // hi x = "bye"; y = x; // y in same context, no write yet => no copy yet assert( y.inner->Handle.s == x.inner->Handle.s); sout | y; // bye } void eagerCopy() { string x = "hi"; string_sharectx c = { NEW_SHARING }; string y = x; // construct y in different context => eager copy assert( y.inner->Handle.s != x.inner->Handle.s); sout | y; // hi x = "bye"; y = x; // y was already in different context => eager copy assert( y.inner->Handle.s != x.inner->Handle.s); sout | y; // bye } void soloAlloc() { string x = "hi"; string_sharectx c = { NO_SHARING }; string y = x; // y allocates into private pad, implying eager copy assert( y.inner->Handle.s != x.inner->Handle.s); assert( DEBUG_string_bytes_in_heap(y.inner->Handle.ulink) == y.inner->Handle.lnth ); // y is in a perfectly fitting heap sout | y; // hi x = "bye"; y = x; // into private y => eager copy assert( y.inner->Handle.s != x.inner->Handle.s); // assert( DEBUG_string_bytes_in_heap(y.inner->Handle.ulink) == y.inner->Handle.lnth ); // optimization required sout | y; // bye } int main() { baseline(); eagerCopy(); soloAlloc(); printf("done\n"); }