If you could make any changes to D, what would they look like?

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Oct 27 19:53:52 UTC 2021

On Wed, Oct 27, 2021 at 07:26:57PM +0000, IGotD- via Digitalmars-d wrote:
> On Wednesday, 27 October 2021 at 19:19:31 UTC, Dukc wrote:
> > 
> > `pure` is also supposed to be an optimization aid.
> It would be interesting to see an example of how pure can help
> optimization, compared to if you wouldn't badge the function.

The following example supposedly shows the difference:

	int pureFunc() pure { return 1; }
	int impureFunc() { return 1; }
	void main() {
		writeln(pureFunc() + pureFunc()); // compiler may call pureFunc only once
		writeln(impureFunc() + impureFunc()); // compiler must call impureFunc twice

Unfortunately, a quick test with dmd did not show the expected
optimization.  Which confirms what I said before that pure's
optimization benefit is really rather marginal.

The bigger benefit is the mechanical verification that you didn't
accidentally depend on global state when you didn't want to, which can
help with improving code quality.


Which is worse: ignorance or apathy? Who knows? Who cares? -- Erich Schubert

More information about the Digitalmars-d mailing list