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

Dukc ajieskola at gmail.com
Thu Oct 28 15:18:54 UTC 2021


On Thursday, 28 October 2021 at 10:53:25 UTC, Dennis wrote:
> On Wednesday, 27 October 2021 at 19:53:52 UTC, H. S. Teoh wrote:
>> 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 function needs to be `nothrow` and compiled with `-O 
> -release`, because dmd needs to account for exceptions / 
> errors, and even then the optimization [shouldn't really be 
> done](https://issues.dlang.org/show_bug.cgi?id=22277). LDC 
> doesn't do anything with `pure` for optimizations, but since it 
> has cross module inlining, it doesn't need it.

I think that, in addition to bugs, there is one hurdle in letting 
the compiler to fully optimise based on `pure`: how do we 
implement a function that manually frees an array? If the 
signature is
```d
pure nothrow @nogc void free(int[])
```
, the compiler might elide consecutive frees to different arrays 
with the same content (so that any potential crash or infinite 
loop would happen in first `free`), if it can detect that the 
freed array does not alias to anything and any potential mutation 
of the array would have no effect.

>
>> 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.
>
> That, but it can also help proving a lack of aliasing. For 
> example:
> ```D
> int[] duplicate(scope int[] x) pure;
> ```
> The returned array can safely be converted to `immutable 
> int[]`. (N.b. this is not implemented yet)

Great, I can `malloc` immutable data with this feature without 
casting once implemented.

>
> ```D
> void foo(int[] x, int[] y) pure nothrow;
> ```
> Parameters `x` and `y` can freely be inferred `scope`, which is 
> useful since dmd's `scope` inference from function bodies is 
> not good.

If `-preview=dip1021` (`@live` checking) isn't enabled, that is.


More information about the Digitalmars-d mailing list