Re: use dll's

2019-03-16 Thread Michelle Long via Digitalmars-d-learn

On Saturday, 16 March 2019 at 15:13:15 UTC, ontrail wrote:

On Saturday, 16 March 2019 at 14:18:07 UTC, Andre Pany wrote:

On Saturday, 16 March 2019 at 12:53:49 UTC, ontrail wrote:

hi,
i created a program (windows) and 2 dll's.
how do i use the 2 d-language dll's in a d-language program 
with only one GC?


any help is appreciated.


It is explained here (section d code calling d code in dll)

https://wiki.dlang.org/Win32_DLLs_in_D

Kind regards
Andre


well thank you kindly.
what i don't understand yet, is there a way to have the GC in 
my program only once instead of several times with the dll's.


Why do you need to do this? It might be more effective to allow 
multiple GC's.


but surely you can set the GC of the dll to use that of the 
another. You will need to expose a method to set the GC from the 
dll..


void SetGC(GC gc);

type of thing...

Where GC is info regarding the GC to do so which I don't know 
much about. The latest dmd supports hooking ones own GC so it 
might be just a matter of using those functions to point to the 
correct gc.


https://dlang.org/library/core/memory/gc.html

https://forum.dlang.org/thread/mzrxzdmhwuwjiybcb...@forum.dlang.org


Re: use dll's

2019-03-16 Thread evilrat via Digitalmars-d-learn

On Saturday, 16 March 2019 at 15:13:15 UTC, ontrail wrote:

On Saturday, 16 March 2019 at 14:18:07 UTC, Andre Pany wrote:

On Saturday, 16 March 2019 at 12:53:49 UTC, ontrail wrote:

hi,
i created a program (windows) and 2 dll's.
how do i use the 2 d-language dll's in a d-language program 
with only one GC?


any help is appreciated.


It is explained here (section d code calling d code in dll)

https://wiki.dlang.org/Win32_DLLs_in_D

Kind regards
Andre


well thank you kindly.
what i don't understand yet, is there a way to have the GC in 
my program only once instead of several times with the dll's.


gc_setProxy is the way, read that article's section again.


Re: Block statements and memory management

2019-03-16 Thread Johan Engelen via Digitalmars-d-learn

On Saturday, 16 March 2019 at 03:47:43 UTC, Murilo wrote:
Does anyone know if when I create a variable inside a scope as 
in

{int a = 10;}
it disappears complete from the memory when the scope finishes? 
Or does it remain in some part of the memory? I am thinking of 
using scopes to make optimized programs that consume less 
memory.


Others have made good points in this thread, but what is missing 
is that indeed scopes _can_ be used beneficially to reduce memory 
footprint.


I recommend playing with this code on d.godbolt.org:
```
void func(ref int[10] a); // important detail: pointer

void foo()
{
{
int[10] a;
func(a);
} {
int[10] b;
func(b);
}
}
```

Because the variable is passed by reference (pointer), the 
optimizer cannot merge the storage space of `a` and `b` _unless_ 
scope information is taken into account. Without taking scope 
into account, the first `func` call could store the pointer to 
`a` somewhere for later use in the second `func` call for 
example. However, because of scope, using `a` after its scope has 
ended is UB, and thus variables `a` and `b` can be used.


GDC uses scope information for variable lifetime optimization, 
but LDC and DMD both do not.
For anyone interested in working on compilers: adding variable 
scope lifetime to LDC (not impossibly hard) would be a nice 
project and be very valuable.


-Johan





Re: use dll's

2019-03-16 Thread ontrail via Digitalmars-d-learn

On Saturday, 16 March 2019 at 14:18:07 UTC, Andre Pany wrote:

On Saturday, 16 March 2019 at 12:53:49 UTC, ontrail wrote:

hi,
i created a program (windows) and 2 dll's.
how do i use the 2 d-language dll's in a d-language program 
with only one GC?


any help is appreciated.


It is explained here (section d code calling d code in dll)

https://wiki.dlang.org/Win32_DLLs_in_D

Kind regards
Andre


well thank you kindly.
what i don't understand yet, is there a way to have the GC in my 
program only once instead of several times with the dll's.


Re: Can't make inout work.

2019-03-16 Thread Paul Backus via Digitalmars-d-learn

On Saturday, 16 March 2019 at 11:55:56 UTC, spir wrote:
I think (but may be wrong) that you don't need inout here, 
since a plain 'ref' will (and does) work. This is accepted by 
me (I added vars to make the code clearer to myself):


struct S(T) {
T value = T.init;
}

auto ref make(T)(ref T value) {
return S!T(value);
}

auto ref f(T)(ref S!T s) {
return make(s.value);
}

void main() {
class C {}
C c ;
auto s1 = S!C(c) ;
auto s2 = make!C(c) ;
auto s3 = f!C(s2) ;
}


This code fails to compile if you change `auto s2` to `const 
s2`--in other words, it has the same problem as the original 
example.


Re: use dll's

2019-03-16 Thread Andre Pany via Digitalmars-d-learn

On Saturday, 16 March 2019 at 12:53:49 UTC, ontrail wrote:

hi,
i created a program (windows) and 2 dll's.
how do i use the 2 d-language dll's in a d-language program 
with only one GC?


any help is appreciated.


It is explained here (section d code calling d code in dll)

https://wiki.dlang.org/Win32_DLLs_in_D

Kind regards
Andre


use dll's

2019-03-16 Thread ontrail via Digitalmars-d-learn

hi,
i created a program (windows) and 2 dll's.
how do i use the 2 d-language dll's in a d-language program with 
only one GC?


any help is appreciated.


Re: Block statements and memory management

2019-03-16 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Mar 16, 2019 at 01:21:02PM +0100, spir via Digitalmars-d-learn wrote:
> On 16/03/2019 11:19, Dennis via Digitalmars-d-learn wrote:
[...]
> > In any case, for better memory efficiency I'd consider looking at
> > reducing dynamic allocations such as new or malloc. Memory on the
> > stack is basically free compared to that, so even if adding lots of
> > braces to your code reduces stack memory, chances are it's a low
> > leverage point.
> > 
> > [1] https://en.wikipedia.org/wiki/Live_variable_analysis
> > [2] https://en.wikipedia.org/wiki/Data-flow_analysis
> 
> Just to add a bit on what has been said:
> * Register allocation (see wikipedia) is a well-researched area.
> * By coding that way, you force the compiler to optimise *a certain way*
> which may prevent it to perform other, more relevant optimisations.
> * You cannot beat the knowledge in that domain, it is simply too big
> and complex, just be confident.
[...]

And to add even more to that: before embarking on micro-optimizations of
this sort, always check with a profiler whether or not the bottleneck is
even in that part of the code. Often I find myself very surprised at
where the real bottleneck is, which is often nowhere near where I
thought it should be.  Also, check with a memory profiler to find out
where the real heavy memory usage points are.  It may not be where you
thought it was.

Generally speaking, in this day and age of highly-optimizing compilers,
premature optimization is the root of all evils, because it uglifies
your code and makes it hard to maintain for little or no gain, and
sometimes for *negative* gain, because by writing code in an unusual
way, you confuse the optimizer as to your real intent, thereby reducing
its effectiveness at producing optimized code. Don't optimize until you
have verified with a profiler where your bottlenecks are. It takes a lot
of time and effort to write code this way, so make it count by applying
it where it actually matters.

Of course, this assumes you use a compiler with a powerful-enough
optimizer.  I recommend ldc/gdc if performance is important to you. Dmd
compiles somewhat faster, but at the cost of poorer codegen.


T

-- 
The early bird gets the worm. Moral: ewww...


Re: Block statements and memory management

2019-03-16 Thread spir via Digitalmars-d-learn

On 16/03/2019 11:19, Dennis via Digitalmars-d-learn wrote:

On Saturday, 16 March 2019 at 03:47:43 UTC, Murilo wrote:

Does anyone know if when I create a variable inside a scope as in
{int a = 10;}
it disappears complete from the memory when the scope finishes? Or does it 
remain in some part of the memory? I am thinking of using scopes to make 
optimized programs that consume less memory.


In general, you want variables to have no larger scope than needed, so in large 
functions reducing the scope may be useful. When it comes to efficiency however, 
doing that is neither necessary nor sufficient for the compiler to re-use 
registers / stack space. I looked at the assembly output of DMD for this:

```
void func(int a);

void main()
{
     {
     int a = 2;
     func(a);
     }
     {
     int b = 3;
     func(b);
     }
}
```
Without optimizations (the -O flag), it stores a and b on different places in 
the stack.
With optimizations, the values of a and b (2 and 3) are simply loaded in the EDI 
register before the call.

Removing the braces doesn't change anything about that.
The compiler does live variable analysis [1] as well as data-flow analysis [2] 
to figure out that it's only needed to load the values 2 and 3 just before the 
function call. This is just a trivial example, but the same applies to larger 
functions.


In any case, for better memory efficiency I'd consider looking at reducing 
dynamic allocations such as new or malloc. Memory on the stack is basically free 
compared to that, so even if adding lots of braces to your code reduces stack 
memory, chances are it's a low leverage point.


[1] https://en.wikipedia.org/wiki/Live_variable_analysis
[2] https://en.wikipedia.org/wiki/Data-flow_analysis


Just to add a bit on what has been said:
* Register allocation (see wikipedia) is a well-researched area.
* By coding that way, you force the compiler to optimise *a certain way* which 
may prevent it to perform other, more relevant optimisations.
* You cannot beat the knowledge in that domain, it is simply too big and 
complex, just be confident.

diniz



Re: Can't make inout work.

2019-03-16 Thread spir via Digitalmars-d-learn

PS: the chapter of Ali Çehreli's book on func args is great:
http://ddili.org/ders/d.en/function_parameters.html
diniz


Re: Can't make inout work.

2019-03-16 Thread spir via Digitalmars-d-learn

On 16/03/2019 04:49, Paul Backus via Digitalmars-d-learn wrote:

On Friday, 15 March 2019 at 23:57:15 UTC, aliak wrote:

Anyone knows how to make this work?


You need an explicit `inout` on the return value of `make`:

auto ref make(T)(inout auto ref T value) {
     return inout(S!T)(value);
}


I think (but may be wrong) that you don't need inout here, since a plain 'ref' 
will (and does) work. This is accepted by me (I added vars to make the code 
clearer to myself):


struct S(T) {
T value = T.init;
}

auto ref make(T)(ref T value) {
return S!T(value);
}

auto ref f(T)(ref S!T s) {
return make(s.value);
}

void main() {
class C {}
C c ;
auto s1 = S!C(c) ;
auto s2 = make!C(c) ;
auto s3 = f!C(s2) ;
}



Re: how to complie a x64 dll wiht a x64 host exe? i need the command line code.

2019-03-16 Thread Andre Pany via Digitalmars-d-learn

On Saturday, 16 March 2019 at 10:46:32 UTC, ll wrote:

On Friday, 15 March 2019 at 15:08:48 UTC, Andre Pany wrote:

On Friday, 15 March 2019 at 09:22:35 UTC, ll wrote:

[...]


There is an open pr for dub. X64 will then automatically be 
used if your system is capable. For now you have to use 
argument -a x86_64.


In dub.json set the targetType to dynamicLibrary.

https://dub.pm/package-format-json.html#target-types

Kind regards
Andre


thank you for your reply. but I still have problems:
 1.what's the meaning of " -L/IMPLIB" in the sample dll command 
line"..\..\..\windows\bin\dmd -ofmydll.dll -L/IMPLIB mydll.d 
dll.d mydll.def..\..\..\windows\bin\dmd test.d mydll.lib" ? Can 
you break command into two segment, one is used to build dll 
and generate the lib,another is used to generate the host exe 
and use the lib?
2.in the the dub.json or in dub arguments, how to command the 
compiler to generate the lib file?


I do not remember the details but
There is also the wiki page
https://wiki.dlang.org/Win32_DLLs_in_D

You can also set targetType to
staticLibrary in dub.json to get a lib file.

Kind regards
Andre




Re: how to complie a x64 dll wiht a x64 host exe? i need the command line code.

2019-03-16 Thread ll via Digitalmars-d-learn

On Friday, 15 March 2019 at 15:08:48 UTC, Andre Pany wrote:

On Friday, 15 March 2019 at 09:22:35 UTC, ll wrote:
i see a simple dll creation in sample fold, but i am confused 
with the command line and i donnot know how to complie a x64 
dll wiht a x64 host exe. i think need a clear command line 
arguments.


There is an open pr for dub. X64 will then automatically be 
used if your system is capable. For now you have to use 
argument -a x86_64.


In dub.json set the targetType to dynamicLibrary.

https://dub.pm/package-format-json.html#target-types

Kind regards
Andre


thank you for your reply. but I still have problems:
 1.what's the meaning of " -L/IMPLIB" in the sample dll command 
line"..\..\..\windows\bin\dmd -ofmydll.dll -L/IMPLIB mydll.d 
dll.d mydll.def..\..\..\windows\bin\dmd test.d mydll.lib" ? Can 
you break command into two segment, one is used to build dll and 
generate the lib,another is used to generate the host exe and use 
the lib?
2.in the the dub.json or in dub arguments, how to command the 
compiler to generate the lib file?


Re: Block statements and memory management

2019-03-16 Thread Dennis via Digitalmars-d-learn

On Saturday, 16 March 2019 at 03:47:43 UTC, Murilo wrote:
Does anyone know if when I create a variable inside a scope as 
in

{int a = 10;}
it disappears complete from the memory when the scope finishes? 
Or does it remain in some part of the memory? I am thinking of 
using scopes to make optimized programs that consume less 
memory.


In general, you want variables to have no larger scope than 
needed, so in large functions reducing the scope may be useful. 
When it comes to efficiency however, doing that is neither 
necessary nor sufficient for the compiler to re-use registers / 
stack space. I looked at the assembly output of DMD for this:

```
void func(int a);

void main()
{
{
int a = 2;
func(a);
}
{
int b = 3;
func(b);
}
}
```
Without optimizations (the -O flag), it stores a and b on 
different places in the stack.
With optimizations, the values of a and b (2 and 3) are simply 
loaded in the EDI register before the call.

Removing the braces doesn't change anything about that.
The compiler does live variable analysis [1] as well as data-flow 
analysis [2] to figure out that it's only needed to load the 
values 2 and 3 just before the function call. This is just a 
trivial example, but the same applies to larger functions.


In any case, for better memory efficiency I'd consider looking at 
reducing dynamic allocations such as new or malloc. Memory on the 
stack is basically free compared to that, so even if adding lots 
of braces to your code reduces stack memory, chances are it's a 
low leverage point.


[1] https://en.wikipedia.org/wiki/Live_variable_analysis
[2] https://en.wikipedia.org/wiki/Data-flow_analysis


Re: what am I missing here with that working dir?

2019-03-16 Thread FreeSlave via Digitalmars-d-learn

On Friday, 15 March 2019 at 21:48:50 UTC, DFTW wrote:

What am I missing here?


Maybe the terminal and your utility you run wkhtmltopdf from have 
different environment?


Re: Block statements and memory management

2019-03-16 Thread Meta via Digitalmars-d-learn

On Saturday, 16 March 2019 at 03:47:43 UTC, Murilo wrote:
Does anyone know if when I create a variable inside a scope as 
in

{int a = 10;}
it disappears complete from the memory when the scope finishes? 
Or does it remain in some part of the memory? I am thinking of 
using scopes to make optimized programs that consume less 
memory.


I'd recommend against these sorts of micro-optimizations. 
Compilers are every good at doing this kind of thing manually so 
you don't have to worry about it and can concentrate on the 
actual logic of your program.