Bug 21410 – idup of an array of immutable elements should be a no-op
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P5
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-11-21T03:32:57Z
Last change time
2021-12-13T14:10:57Z
Keywords
pull
Assigned to
No Owner
Creator
Vladimir Panteleev
Comments
Comment #0 by dlang-bugzilla — 2020-11-21T03:32:57Z
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).
Comment #1 by dlang-bot — 2020-11-21T03:38:28Z
@CyberShadow created dlang/druntime pull request #3281 "Fix Issue 21410 - idup of an array of immutable elements should be a no-op" fixing this issue:
- Fix Issue 21410 - idup of an array of immutable elements should be a no-op
https://github.com/dlang/druntime/pull/3281
Comment #2 by dlang-bugzilla — 2021-12-13T14:10:57Z