Re: core.exception.InvalidMemoryOperationError

2014-07-10 Thread NCrashed via Digitalmars-d-learn
On Thursday, 10 July 2014 at 15:36:53 UTC, francesco cattoglio 
wrote:
A code I'm working on stops working and starts printing an 
infinite loop of

core.exception.InvalidMemoryOperationError
to the command line output. The code is quite complex and the 
bug seems to present itself almost in random situation so I 
would like to try to understand the issue better before looking 
for the wrong line of code hiding somewhere. I've read it might 
be that something is trying to allocate during a destructor 
call, but it sounds really strange to me that there's a 
neverending amount of exceptions being thrown. This is the 
first exception being thrown (nothing is thrown before the 
infinite loop begins).


Anyone has suggestions/ideas/heard of a similar stuff before?


I had the same issue with Derelict bindings. Bindings symbols 
could be already unloaded when a destructor tries to use them.


Re: Small part of a program : d and c versions performances diff.

2014-07-09 Thread NCrashed via Digitalmars-d-learn

On Wednesday, 9 July 2014 at 10:57:33 UTC, Larry wrote:

Hello,

I extracted a part of my code written in c.
it is deliberately useless here but I would understand the 
different technics to optimize such kind of code with gdc 
compiler.


it currently runs under a microsecond.

Constraint : the way the code is expressed cannot be changed 
much we need that double loop because there are other 
operations involved in the first loop scope.


main.c :
[code]
#include stdio.h
#include string.h
#include stdlib.h
#include jol.h
#include time.h
#include sys/time.h
int main(void)
{

struct timeval s,e;
gettimeofday(s,NULL);

int pol = 5;
tes(pol);


int arr[] = {9,16,458,2,68,5452,98,32,4,565,78,985,3215};
int len = 13-1;
int g = 0;

for (int x = 36; x = 0 ; --x ){
// some code here erased for the test
for(int y = len ; y = 0; --y){
//some other code here
++g;
arr[y] +=1;

}

}
gettimeofday(e,NULL);

printf(so ? %d %lu %d %d %d,g,e.tv_usec - s.tv_usec, 
arr[4],arr[9],pol);

return 0;
}
[/code]

jol.c
[code]
void tes(int * restrict a){

*a = 9;

}
[/code]

and jol.h

#ifndef JOL_H
#define JOL_H
void tes(int * restrict a);
#endif // JOL_H


Now, the D counterpart:

module main;

import std.stdio;
import std.datetime;
import jol;
int main(string[] args)
{


auto currentTime = Clock.currTime();

int pol = 5;
tes(pol);
pol = 8;

int arr[] = [9,16,458,2,68,5452,98,32,4,565,78,985,3215];
int len = 13-1;
int g = 0;

for (int x = 31; x = 0 ; --x ){

for(int y = len ; y = 0; --y){

++g;
arr[y] +=1;

}

}
auto currentTime2 = Clock.currTime();
writefln(Hello World %d %s %d %d\n,g, (currentTime2 - 
currentTime),arr[4],arr[9]);


return 0;
}

and

module jol;
final void tes(ref int a){

a = 9;

}


Ok, the compilation options :
gdc hello.d jol.d -O3 -frelease -ftree-loop-optimize

gcc -march=native -std=c11 -O2 main.c jol.c

Now the performance :
D : 12 µs
C :  1µs

Where does the diff comes from ? Is there a way to optimize the 
d version ?


Again, I am absolutely new to D and those are my very first 
line of code with it.


Thanks


Clock isn't an accurate benchmark instrument. Try 
std.datetime.benchmark:

```
module main;

import std.stdio;
import std.datetime;

void tes(ref int a)
{
a = 9;
}

int[] arr = [9,16,458,2,68,5452,98,32,4,565,78,985,3215];

void foo()
{
int pol = 5;
tes(pol);
pol = 8;
int g = 0;

foreach_reverse(x; 0..31)
{
foreach_reverse(ref a; arr)
{
++g;
a += 1;
}
}
}

void main()
{
auto res = benchmark!foo(1000); // take mean of 1000 launches
writeln(res[0].msecs,  , arr[4],  , arr[9]);
}
```

Dmd time: 1 us
Gcc time: = 1 us


Re: Introspecting a Module with Traits, allMembers

2014-07-09 Thread NCrashed via Digitalmars-d-learn

On Wednesday, 9 July 2014 at 20:07:57 UTC, NCrashed wrote:

Produces:
ir/iir.d(85): Error: argument has no members


If module name is ir.iir: pragma(msg, __traits(allMembers, 
ir.iir));


Re: [dmd 2.066-b1] std.range.array with shared objects and AA rehash

2014-07-08 Thread NCrashed via Digitalmars-d-learn

On Monday, 7 July 2014 at 15:34:33 UTC, Meta wrote:

On Monday, 7 July 2014 at 09:53:22 UTC, NCrashed wrote:
I am using ranges (wrapped in InputRangeObject for use in 
interfaces) of shared objects, with new beta some cases are 
broken:

```
import std.range;

class A {}

InputRange!(shared A) foo()
{
return [new A].inputRangeObject;
}

void bar()
{
auto res = foo.array;
}

void main() {}
```
Fails with:
```
source/app.d(7): Error: cannot implicitly convert expression 
(inputRangeObject([new A])) of type 
std.range.InputRangeObject!(A[]).InputRangeObject to 
std.range.InputRange!(shared(A)).InputRange
/usr/include/dmd/phobos/std/conv.d(3914): Error: cannot 
implicitly convert expression (arg) of type shared(A) to app.A
/usr/include/dmd/phobos/std/array.d(2476): Error: template 
instance 
std.conv.emplaceRef!(shared(A)).emplaceRef!(shared(A)) error 
instantiating
/usr/include/dmd/phobos/std/array.d(64):instantiated 
from here: put!(shared(A))
source/app.d(12):instantiated from here: 
array!(InputRange!(shared(A)))

```

And also AA starts behave strange in shared context:
```
shared string[][string] map;

void main()
{
map.rehash;
}
```
My AA is stored in shared class, the shared is inferred 
implicitly. Also following workaround works:

```
void main()
{
(cast(shared(string[])[string])map).rehash;
}
```

Is this behavior a bug, or it works as expected?


I don't know about your second problem, but the fix for your 
first problem is to construct a shared A. You're trying to 
create a normal A and have it implicitly casted to shared, 
which the compiler won't do.


InputRange!(shared A) foo()
{
//new shared A instead of new A
return [new shared A].inputRangeObject;
}


Oops, I forgot shared at new. But the major issue is that doesn't 
fix the problem:

```
import std.range;

class A {}

InputRange!(shared A) foo()
{
return [new shared A].inputRangeObject;
}

void bar()
{
auto res = foo.array;
}

void main() {}
```
Output:
```
/usr/include/dmd/phobos/std/conv.d(3914): Error: cannot 
implicitly convert expression (arg) of type shared(A) to app.A
/usr/include/dmd/phobos/std/array.d(2476): Error: template 
instance std.conv.emplaceRef!(shared(A)).emplaceRef!(shared(A)) 
error instantiating
/usr/include/dmd/phobos/std/array.d(64):instantiated from 
here: put!(shared(A))
source/app.d(12):instantiated from here: 
array!(InputRange!(shared(A)))

```


[dmd 2.066-b1] DList. Cannot remove from an un-initialized List

2014-07-08 Thread NCrashed via Digitalmars-d-learn

This was working under 2.065:
```
import std.container;

void main()
{
DList!int list;
list.clear();
}
```

Run-time assertion:
```
core.exception.AssertError@/usr/include/dmd/phobos/std/container/dlist.d(480): 
Cannot remove from an un-initialized List


/home/ncrashed/dev/d/dmd-test/dmd-test(pure nothrow @nogc @safe 
std.container.dlist.DList!(int).DList.Range 
std.container.dlist.DList!(int).DList.remove(std.container.dlist.DList!(int).DList.Range)+0x90) 
[0x441830]
/home/ncrashed/dev/d/dmd-test/dmd-test(pure nothrow @nogc @safe 
void std.container.dlist.DList!(int).DList.clear()+0x65) 
[0x4410dd]

/home/ncrashed/dev/d/dmd-test/dmd-test(_Dmain+0x1a) [0x43df3a]
/home/ncrashed/dev/d/dmd-test/dmd-test(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x28) 
[0x44aeac]
/home/ncrashed/dev/d/dmd-test/dmd-test(void 
rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate())+0x2d) 
[0x44adf1]
/home/ncrashed/dev/d/dmd-test/dmd-test(void 
rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).runAll()+0x2d) [0x44ae51]
/home/ncrashed/dev/d/dmd-test/dmd-test(void 
rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate())+0x2d) 
[0x44adf1]
/home/ncrashed/dev/d/dmd-test/dmd-test(_d_run_main+0x192) 
[0x44ad66]

/home/ncrashed/dev/d/dmd-test/dmd-test(main+0x25) [0x448355]
/lib64/libc.so.6(__libc_start_main+0xf5) [0x39e9021d65]

```

Does DList requires any explicit initialization now or it is a 
regression?


Re: [dmd 2.066-b1] DList. Cannot remove from an un-initialized List

2014-07-08 Thread NCrashed via Digitalmars-d-learn

On Tuesday, 8 July 2014 at 13:06:34 UTC, NCrashed wrote:

This was working under 2.065:
```
import std.container;

void main()
{
DList!int list;
list.clear();
}
```

Run-time assertion:
```
core.exception.AssertError@/usr/include/dmd/phobos/std/container/dlist.d(480): 
Cannot remove from an un-initialized List


/home/ncrashed/dev/d/dmd-test/dmd-test(pure nothrow @nogc @safe 
std.container.dlist.DList!(int).DList.Range 
std.container.dlist.DList!(int).DList.remove(std.container.dlist.DList!(int).DList.Range)+0x90) 
[0x441830]
/home/ncrashed/dev/d/dmd-test/dmd-test(pure nothrow @nogc @safe 
void std.container.dlist.DList!(int).DList.clear()+0x65) 
[0x4410dd]

/home/ncrashed/dev/d/dmd-test/dmd-test(_Dmain+0x1a) [0x43df3a]
/home/ncrashed/dev/d/dmd-test/dmd-test(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x28) 
[0x44aeac]
/home/ncrashed/dev/d/dmd-test/dmd-test(void 
rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate())+0x2d) 
[0x44adf1]
/home/ncrashed/dev/d/dmd-test/dmd-test(void 
rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).runAll()+0x2d) [0x44ae51]
/home/ncrashed/dev/d/dmd-test/dmd-test(void 
rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate())+0x2d) 
[0x44adf1]
/home/ncrashed/dev/d/dmd-test/dmd-test(_d_run_main+0x192) 
[0x44ad66]

/home/ncrashed/dev/d/dmd-test/dmd-test(main+0x25) [0x448355]
/lib64/libc.so.6(__libc_start_main+0xf5) [0x39e9021d65]

```

Does DList requires any explicit initialization now or it is a 
regression?


And some addition:
```
import std.container;

void main()
{
DList!int list;
list.insert = 42;
list.clear(); list.clear();
}
```

Output:
```
core.exception.AssertError@/usr/include/dmd/phobos/std/container/dlist.d(481): 
Remove: Range is empty


/home/ncrashed/dev/d/dmd-test/dmd-test(pure nothrow @nogc @safe 
std.container.dlist.DList!(int).DList.Range 
std.container.dlist.DList!(int).DList.remove(std.container.dlist.DList!(int).DList.Range)+0xcc) 
[0x4419b4]
/home/ncrashed/dev/d/dmd-test/dmd-test(pure nothrow @nogc @safe 
void std.container.dlist.DList!(int).DList.clear()+0x65) 
[0x441225]

/home/ncrashed/dev/d/dmd-test/dmd-test(_Dmain+0x31) [0x43e081]
/home/ncrashed/dev/d/dmd-test/dmd-test(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x28) 
[0x44b0cc]
/home/ncrashed/dev/d/dmd-test/dmd-test(void 
rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate())+0x2d) 
[0x44b011]
/home/ncrashed/dev/d/dmd-test/dmd-test(void 
rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).runAll()+0x2d) [0x44b071]
/home/ncrashed/dev/d/dmd-test/dmd-test(void 
rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate())+0x2d) 
[0x44b011]
/home/ncrashed/dev/d/dmd-test/dmd-test(_d_run_main+0x192) 
[0x44af86]

/home/ncrashed/dev/d/dmd-test/dmd-test(main+0x25) [0x448575]
/lib64/libc.so.6(__libc_start_main+0xf5) [0x39e9021d65]
```


Re: [dmd 2.066-b1] std.range.array with shared objects and AA rehash

2014-07-08 Thread NCrashed via Digitalmars-d-learn

On Tuesday, 8 July 2014 at 13:07:57 UTC, Meta wrote:

On Tuesday, 8 July 2014 at 12:42:44 UTC, NCrashed wrote:
Oops, I forgot shared at new. But the major issue is that 
doesn't fix the problem:

```
import std.range;

class A {}

InputRange!(shared A) foo()
{
return [new shared A].inputRangeObject;
}

void bar()
{
auto res = foo.array;
}

void main() {}
```
Output:
```
/usr/include/dmd/phobos/std/conv.d(3914): Error: cannot 
implicitly convert expression (arg) of type shared(A) to app.A
/usr/include/dmd/phobos/std/array.d(2476): Error: template 
instance 
std.conv.emplaceRef!(shared(A)).emplaceRef!(shared(A)) error 
instantiating
/usr/include/dmd/phobos/std/array.d(64):instantiated 
from here: put!(shared(A))
source/app.d(12):instantiated from here: 
array!(InputRange!(shared(A)))

```


Hmmm, it worked when I compiled it on Dpaste, so you must be 
using a different compiler. There were some changes to shared 
in the current 2.066 beta, so that may be the cause. Somebody 
more experienced with shared than me will have to answer.


I appreciate you help, the error smells like a bug. That compiles 
on dmd 2.065, but I am testing new dmd 2.066-b1 to not suddenly 
get dozens of such errors when it becomes a release.


Re: Tuple and tie?

2014-07-08 Thread NCrashed . via Digitalmars-d-learn
Try this:
```
import std.typecons;
import std.typetuple;
import std.math;
import std.stdio;

auto tie(StoreElements...)(ref StoreElements stores)
{
alias Elements = staticMap!(Unqual, StoreElements);
 template toPointer(T)
{
alias toPointer = T*;
}
 struct Holder
{
alias StoreElementsPtrs = staticMap!(toPointer, StoreElements);
StoreElementsPtrs storePtrs;
 this(ref StoreElements stores)
{
foreach(i, _; StoreElements)
{
storePtrs[i] = stores[i];
}
}
 void opAssign(Tuple!Elements values)
{
foreach(i, _; Elements)
{
*storePtrs[i] = values[i];
}
}
}
 return Holder(stores);
}

Tuple!(float, float) sinCos(float n)
{
return tuple(cast(float)sin(n), cast(float)cos(n)); //please note
cast(float)!
}

void main()
{
float s,c;
tie(s,c) = sinCos(3.0f);
writeln(s,  , c);
}
```


2014-07-08 22:55 GMT+04:00 Meta via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com:

 On Tuesday, 8 July 2014 at 18:45:21 UTC, Remo wrote:

 On Tuesday, 8 July 2014 at 18:29:40 UTC, Meta wrote:

 On Tuesday, 8 July 2014 at 17:42:00 UTC, Remo wrote:


 How to make something that work like std::tie in D2 ?

 Tuple!(float, float) sinCos(float n) {
 return tuple(cast(float)sin(n), cast(float)cos(n)); //please note
 cast(float)!
 }

 int main(string[] argv) {
 float s,c;
 tie!(s,c) = sinCos(3.0f);
 }


 alias tie = TypeTuple;

 int main(string[] argv)
 {
float s,c;
tie!(s,c) = sinCos(3.0f);
 }


 Thanks !
 This is easier as I was thinking :)
 Now I only need to be sure that this do not have unwanted side effects.


 It's fine. There's not much to TypeTuple; it's defined like this:

 alias TypeTuple(TList...) = alias TypeTuple = TList;

 Where TList is any number of... things. Types, variables, values, template
 names, etc. Therefore, `TypeTuple!(s, c)` at a low level it's more or less
 like the following:

 float s, c;

 s = sinCos(3.0f)[0];
 c = sinCos(3.0f)[1];

 Not that sinCos is called twice. It's only called once, and the result is
 distributed across s and c.



Re: Tuple and tie?

2014-07-08 Thread NCrashed via Digitalmars-d-learn

On Tuesday, 8 July 2014 at 17:42:00 UTC, Remo wrote:


How to make something that work like std::tie in D2 ?

Tuple!(float, float) sinCos(float n) {
  return tuple(cast(float)sin(n), cast(float)cos(n)); //please 
note cast(float)!

}

int main(string[] argv) {
 float s,c;
 tie!(s,c) = sinCos(3.0f);
}


Try the following approach:
```
import std.typecons;
import std.typetuple;
import std.math;
import std.stdio;

auto tie(StoreElements...)(ref StoreElements stores)
{
alias Elements = staticMap!(Unqual, StoreElements);

template toPointer(T)
{
alias toPointer = T*;
}

struct Holder
{
alias StoreElementsPtrs = staticMap!(toPointer, StoreElements);
StoreElementsPtrs storePtrs;

this(ref StoreElements stores)
{
foreach(i, _; StoreElements)
{
storePtrs[i] = stores[i];
}
}

void opAssign(Tuple!Elements values)
{
foreach(i, _; Elements)
{
*storePtrs[i] = values[i];
}
}
}

return Holder(stores);
}

Tuple!(float, float) sinCos(float n)
{
	return tuple(cast(float)sin(n), cast(float)cos(n)); //please 
note cast(float)!

}

void main()
{
float s,c;
tie(s,c) = sinCos(3.0f);
writeln(s,  , c);
}
```


Re: Tuple and tie?

2014-07-08 Thread NCrashed via Digitalmars-d-learn

Sorry, some glitch with email answer.

The main difference between my solution and TypeTuple is that you 
can pass anonymous struct between other functions to use it later:

```
void foo(T)(T holder)
{
holder = sinCos(3.0f);
}

void main()
{
float s,c;
foo(tie(s,c));
writeln(s,  , c);
}
```


[dmd 2.066-b1] std.range.array with shared objects and AA rehash

2014-07-07 Thread NCrashed via Digitalmars-d-learn
I am using ranges (wrapped in InputRangeObject for use in 
interfaces) of shared objects, with new beta some cases are 
broken:

```
import std.range;

class A {}

InputRange!(shared A) foo()
{
return [new A].inputRangeObject;
}

void bar()
{
auto res = foo.array;
}

void main() {}
```
Fails with:
```
source/app.d(7): Error: cannot implicitly convert expression 
(inputRangeObject([new A])) of type 
std.range.InputRangeObject!(A[]).InputRangeObject to 
std.range.InputRange!(shared(A)).InputRange
/usr/include/dmd/phobos/std/conv.d(3914): Error: cannot 
implicitly convert expression (arg) of type shared(A) to app.A
/usr/include/dmd/phobos/std/array.d(2476): Error: template 
instance std.conv.emplaceRef!(shared(A)).emplaceRef!(shared(A)) 
error instantiating
/usr/include/dmd/phobos/std/array.d(64):instantiated from 
here: put!(shared(A))
source/app.d(12):instantiated from here: 
array!(InputRange!(shared(A)))

```

And also AA starts behave strange in shared context:
```
shared string[][string] map;

void main()
{
map.rehash;
}
```
My AA is stored in shared class, the shared is inferred 
implicitly. Also following workaround works:

```
void main()
{
(cast(shared(string[])[string])map).rehash;
}
```

Is this behavior a bug, or it works as expected?


[dmd 2.066] Is scope with nothrow regression?

2014-07-06 Thread NCrashed via Digitalmars-d-learn


```
void bar()
{
throw new Exception();
}

void foo() nothrow
{
scope(failure) {}
bar();
}

void main() {}
```
Doesn't compile with 2.066:
```
source/app.d(9): Error: 'app.bar' is not nothrow
source/app.d(6): Error: function 'app.foo' is nothrow yet may 
throw

```


Re: [dmd 2.066] Is scope with nothrow regression?

2014-07-06 Thread NCrashed via Digitalmars-d-learn

On Sunday, 6 July 2014 at 13:04:35 UTC, Marc Schütz wrote:

On Sunday, 6 July 2014 at 12:31:42 UTC, NCrashed wrote:


```
void bar()
{
throw new Exception();
}

void foo() nothrow
{
scope(failure) {}
bar();
}

void main() {}
```
Doesn't compile with 2.066:
```
source/app.d(9): Error: 'app.bar' is not nothrow
source/app.d(6): Error: function 'app.foo' is nothrow yet may 
throw

```


This is not an error. `scope(failure)` doesn't swallow the 
exceptions it catches, it rethrows them when it's done.


Thank you, I was expecting different behavior for a long time and 
now will use try-catch instead.