D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10527 - Eliding more postblit constructor calls
Summary: Eliding more postblit constructor calls
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-07-03 03:51 UTC by Tommi
Modified: 2022-04-12 12:16 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tommi 2013-07-03 03:51:18 UTC
Definitions:
1) 'Str' is a struct type which has a postblit constructor
2) 'var' is a variable whose unqualified type is Str
3) 'cpy' is a copy of the variable var

Proposed language change:
Whenever a copy is made from var to cpy (either by making a new variable or by passing var to a function by value) and the compiler can determine (either through analysing code or by deduction from a function signature) that during the lifetime of cpy, no mutation is done to var nor to any data reachable from it, the language should guarantee these two things:
1) cpy's postblit constructor is omitted when cpy is constructed
2) cpy's destructor is omitted when cpy goes out of scope

Examples:
Here are some examples, where the compiler can deduce that it's safe to omit the postblit and destructor of cpy by merely looking at the function signature:
1)
int foo(T, U)(const Str cpy, const T t, immutable U u) pure { }

2)
void foo(T, U)(immutable Str cpy, T t, U u) { }
Comment 1 Tommi 2013-07-03 04:12:00 UTC
(In reply to comment #0)
> [..] the language should guarantee these two things:
> 1) cpy's postblit constructor is omitted when cpy is constructed
> 2) cpy's destructor is omitted when cpy goes out of scope

Let's change this proposal so that the language would guarantee to do those optimisations only when it can deduce that it can be done from looking at function signatures. And the compiler can choose whether or not it wants to try to do more postblit/destructor elisions through static analysis.
Comment 2 Tommi 2013-07-03 06:55:55 UTC
(In reply to comment #0)
> 2) 'var' is a variable whose unqualified type is Str

By this I mean:
is(std.traits.Unqual!(typeof(var)) == Str)
Comment 3 RazvanN 2022-04-12 12:16:15 UTC
Postblit has design flaws so it should not be used anymore. The copy constructor should be used instead.