[Issue 21410] New: idup of an array of immutable elements should be a no-op
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Nov 21 03:32:57 UTC 2020
https://issues.dlang.org/show_bug.cgi?id=21410
Issue ID: 21410
Summary: idup of an array of immutable elements should be a
no-op
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P5
Component: druntime
Assignee: nobody at puremagic.com
Reporter: dlang-bugzilla at thecybershadow.net
string s = "foo";
assert(s.idup.ptr == s.ptr); // Fail
If the data is immutable, it can already be assumed that it has infinite
lifetime, so unless something is breaking the type system, it doesn't make
sense to create another copy of it.
The motivation is generic code such as the following:
int[string] dict;
void save(C : char)(C[] key, int value)
{
if (auto p = key in dict)
*p = value;
else
dict[key.idup] = value;
}
This way, save can be called with either a char[] or string, and it will only
copy the key argument if it needs to. Such a distinction can occur when e.g.
deserializing data from memory (where it can be immutable), or chunk-wise from
a stream (where the data's lifetime is only until the next chunk is read).
--
More information about the Digitalmars-d-bugs
mailing list