Thread overview
Question about publishing a useful function I have written
Jul 14, 2020
Cecil Ward
Jul 14, 2020
Max Haughton
Jul 15, 2020
Cecil Ward
Jul 15, 2020
Andre Pany
Jul 15, 2020
9il
Jul 15, 2020
Cecil Ward
Jul 15, 2020
Jacob Carlborg
July 14, 2020
I have written something which may or may not be novel and I’m wondering about how to distribute it to as many users as possible, hoping others will find it useful. What’s the best way to publish a D routine ?

It is called
    void assume( bool condition ) nothrow nogc safe

for example:
    assume( x < 100 );
or
    assume( y != 0 );

It generates no code at all, but t does tell the gdc compiler that the given condition is true and so later code generation may assume that truth in all code generation. So for example, given the assumes above,
    if ( x==200)
is false and no code is generated for the if-true basic block.

It might look a bit like assert but it isn’t the same thing. I think assert is disabled in full release build and is only there in debug mode, is that correct? Also assert generates code in that it calls panic or hits a ud2 or whatever, whereas assume is also zero code.

Does anyone know if this has already been published by someone else?

Unfortunately at the moment it only works with gdc as it relies on functions that are gdc-specific for its implementation. However I could use conditional compilation to simply set up a null implementation for other compilers.

I’d like to get it implemented with ldc if I can.


If anyone would like to give assume a try with gdc, then shout and I’ll try and find some way of getting it to you easily.
July 14, 2020
On Tuesday, 14 July 2020 at 21:58:49 UTC, Cecil Ward wrote:
> I have written something which may or may not be novel and I’m wondering about how to distribute it to as many users as possible, hoping others will find it useful. What’s the best way to publish a D routine ?
>
> [...]

GitHub is the best place to publish code. Does GDC actually use the optimization? I tried something like that before but I couldn't seem to get it to work properly.


July 15, 2020
On Tuesday, 14 July 2020 at 21:58:49 UTC, Cecil Ward wrote:
>
>
> Does anyone know if this has already been published by someone else?
>

https://github.com/libmir/mir-core/blob/master/source/mir/utility.d#L29

We test LDC and DMC. CI needs an update to be actually tested with GDC.
July 15, 2020
On Tuesday, 14 July 2020 at 23:10:28 UTC, Max Haughton wrote:
> On Tuesday, 14 July 2020 at 21:58:49 UTC, Cecil Ward wrote:
>> I have written something which may or may not be novel and I’m wondering about how to distribute it to as many users as possible, hoping others will find it useful. What’s the best way to publish a D routine ?
>>
>> [...]
>
> GitHub is the best place to publish code. Does GDC actually use the optimization? I tried something like that before but I couldn't seem to get it to work properly.

On Tuesday, 14
]

I just tried an experiment. It seems that in release mode assert()s are realised as absolutely nothing at all, and so the _conditions_ in the asserts are not declared. So later generated code does not have the benefit of knowledge of asserted truth conditions in release mode. So in release mode, without these truth conditions being established, the code generated (apart from the asserts’ code) can be _worse than in debug mode_, which seems bizarre, but it’s true.

for example
    assert( x < 100 );
    …
    if ( x==200 )  // <— evaluates to false _at compile time_
         {
         // no code generated for this block in debug mode,
         // but is generated in release mode
         }
    …
    if ( x < 100 ) // <— no code generated for if-test as cond == true at compile-time
July 15, 2020
On Wednesday, 15 July 2020 at 02:25:42 UTC, 9il wrote:
> On Tuesday, 14 July 2020 at 21:58:49 UTC, Cecil Ward wrote:
>>
>>
>> Does anyone know if this has already been published by someone else?
>>
>
> https://github.com/libmir/mir-core/blob/master/source/mir/utility.d#L29
>
> We test LDC and DMC. CI needs an update to be actually tested with GDC.

Brilliant. Many thanks.
July 15, 2020
On 2020-07-14 23:58, Cecil Ward wrote:
> What’s the best way to publish a D routine ?

As others have already said, on GitHub. Then as a Dub package as well [1].

[1] https://code.dlang.org

-- 
/Jacob Carlborg
July 15, 2020
On Wednesday, 15 July 2020 at 09:31:27 UTC, Cecil Ward wrote:
> On Tuesday, 14 July 2020 at 23:10:28 UTC, Max Haughton wrote:
>> On Tuesday, 14 July 2020 at 21:58:49 UTC, Cecil Ward wrote:
>>> I have written something which may or may not be novel and I’m wondering about how to distribute it to as many users as possible, hoping others will find it useful. What’s the best way to publish a D routine ?
>>>
>>> [...]
>>
>> GitHub is the best place to publish code. Does GDC actually use the optimization? I tried something like that before but I couldn't seem to get it to work properly.
>
> On Tuesday, 14
> ]
>
> I just tried an experiment. It seems that in release mode assert()s are realised as absolutely nothing at all, and so the _conditions_ in the asserts are not declared. So later generated code does not have the benefit of knowledge of asserted truth conditions in release mode. So in release mode, without these truth conditions being established, the code generated (apart from the asserts’ code) can be _worse than in debug mode_, which seems bizarre, but it’s true.
>
> for example
>     assert( x < 100 );
> >     if ( x==200 )  // <— evaluates to false _at compile time_
>          {
>          // no code generated for this block in debug mode,
>          // but is generated in release mode
>          }
> >     if ( x < 100 ) // <— no code generated for if-test as cond == true at compile-time

This is by intention. While exceptions are used for resources like user input/file system/network,... you use asserts to validate your code.
When you publish your code to the world, the assertions have "done" their work and there is no need to include them in a release build.

Cases where you need asserts in release builds are an indicator, that you should have used exceptions instead.

Kind regards
Andre