Copy-On-Write (COW) Managed Containers?

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Tue Oct 20 14:56:44 UTC 2020


On Tuesday, 20 October 2020 at 12:42:26 UTC, Per Nordlöw wrote:
> What are your thoughts on the pros and cons with copy-on-write 
> (COW) managed allocations in D, typically for containers such 
> as `std.container.Array`?
>
> Has it been considered for use in a standard 
> containers/collections library in D?
>
> Why not?
>
> The only example I've found is C++ is std::string which in some 
> versions of STL seems to make use of COW.

Copy-on-write only makes sense if you intend to make copies 
without modifying them. That is rather unlikely for arrays. When 
you create a copy you usually do it with the intent of modifying 
the copy.

I think this behaviour for std::string came about because C++ 
didn't get std::string_view until recently. I think it is a flaw.

If you use reference counting throughout like Swift/Objective-C, 
then I guess you could do it. But it isn't really suitable for 
low level programming. It is a high level feature.



More information about the Digitalmars-d mailing list