Re: static init c struct with array filed

2022-03-16 Thread Adam Ruppe via Digitalmars-d-learn

On Thursday, 17 March 2022 at 00:16:39 UTC, Mike Parker wrote:

On Wednesday, 16 March 2022 at 07:27:06 UTC, test wrote:

```c
struct Test {
int32_t a;
}
struct Test2 {
int32_t a;
Test arr[];
}
```

I need static const init Test2, then pass it to c library 
late(third library, can not change the type def).




Any time you see a '[]' in C, the equivalent declaration in D 
has to be a pointer.


Not always with structs... if it has a fixed size or an 
initializer in C, then you need to match the size in D.


Otherwise yeah use the pointer.



Re: static init c struct with array filed

2022-03-16 Thread Mike Parker via Digitalmars-d-learn

On Wednesday, 16 March 2022 at 07:27:06 UTC, test wrote:

```c
struct Test {
int32_t a;
}
struct Test2 {
int32_t a;
Test arr[];
}
```

I need static const init Test2, then pass it to c library 
late(third library, can not change the type def).




Any time you see a '[]' in C, the equivalent declaration in D has 
to be a pointer. Like Ali said, D's arrays are a pointer and 
length pair, so declaring the declaration of the `arr` member 
will not match the declaration on the C side.


Since you say you can't change the declaration on the C side, 
user1234's answer is the direction you want to go (though you 
don't need the `extern(C)` on the structs).






Re: Make shared static this() encoding table compilable

2022-03-16 Thread Salih Dincer via Digitalmars-d-learn

On Wednesday, 16 March 2022 at 18:40:35 UTC, zhad3 wrote:

On Tuesday, 15 March 2022 at 03:01:05 UTC, Salih Dincer wrote:


OMG, I gasp at my computer screen and waited for minutes. :)

When you edit the code at the back-end level, you can use 
system resources in the best way. I think you should start 
with 
[dlang.dmd.backend.aarray](https://github.com/dlang/dmd/blob/master/src/dmd/backend/aarray.d)


If we use the following codes with Ali's code by separate the 
keys and values, it compiles fast on DMD and works correctly:


[snip]

SDB@79


Thank you, but wouldn't using the DMD backend make it so that 
it won't compile with LDC? I am not that knowledgeable in 
compiler internals so I don't know.


No need for all files.  Just aarray.d is enough.

SDB@79


Re: Make shared static this() encoding table compilable

2022-03-16 Thread zhad3 via Digitalmars-d-learn

On Tuesday, 15 March 2022 at 03:01:05 UTC, Salih Dincer wrote:


OMG, I gasp at my computer screen and waited for minutes. :)

When you edit the code at the back-end level, you can use 
system resources in the best way. I think you should start with 
[dlang.dmd.backend.aarray](https://github.com/dlang/dmd/blob/master/src/dmd/backend/aarray.d)


If we use the following codes with Ali's code by separate the 
keys and values, it compiles fast on DMD and works correctly:


[snip]

SDB@79


Thank you, but wouldn't using the DMD backend make it so that it 
won't compile with LDC? I am not that knowledgeable in compiler 
internals so I don't know.


Re: Make shared static this() encoding table compilable

2022-03-16 Thread zhad3 via Digitalmars-d-learn

On Monday, 14 March 2022 at 22:20:42 UTC, rikki cattermole wrote:


The recommended solution by Unicode is to use Trie tables for 
Look Up Tables (LUTs).


https://en.wikipedia.org/wiki/Trie

You can generate these as read only global arrays and are very 
fast for this.


Thank your for this hint. I'll have to check whether this is 
applicable for this context. For example this lookup table 
appears to be a bijection.


Re: Make shared static this() encoding table compilable

2022-03-16 Thread zhad3 via Digitalmars-d-learn

On Monday, 14 March 2022 at 19:05:41 UTC, Ali Çehreli wrote:


Yes, better but not much: 37 seconds vs. 50+ seconds on my 
system.


Even though I am pretty sure the OP has access to the keys and 
the values separately, if it helps, I used the following code 
to separate the keys and values:


[snip]

shared static this() {
  cp949_table = make_cp949_table();
}

Yeah, dmd's -O performance with those tables is still very poor.

Ali


Thank you for this. This works although I could not reach the 
same speed as you. I wonder if people with less memory as me (16 
GB) will still be able to compile it.


DMD (DMD64 D Compiler v2.098.1)
```
$ time dub test --compiler=dmd --build=release

real2m11,438s
user2m11,134s
sys 0m0,153s
```
LDC (LDC - the LLVM D compiler (1.26.0): based on DMD v2.096.1 
and LLVM 7.0.1)

```
$ time dub test --compiler=ldc2 --build=release

real0m18,466s
user0m18,099s
sys 0m0,151s
```

But this is definitely better than failing with an error. I have 
not yet tried the -lowmem flag. I'll try that later.
I think I'll use your solution for now as that works and 
hopefully people won't have to recompile everything so often. 
Usually this should just get compiled once and be good with it :)


Re: Make shared static this() encoding table compilable

2022-03-16 Thread zhad3 via Digitalmars-d-learn

On Monday, 14 March 2022 at 10:23:18 UTC, bauss wrote:

I think it's a memory issue and it's unlikely to be solved.

I saw a similar issue a while ago where it worked with 
everything but DMD.


Someone can correct me but if I remember correctly it's because 
DMD issues instructions for each value (or something like that) 
in the static array and thus runs out of memory before any 
optimization can happen or whatever, but LDC etc. doesn't have 
said issue.


I can't exactly remember how it is, but I think it's something 
along those lines.


I don't think there really is a workaround as of now and 
probably never will be.


Thank you, that's unfortunate if true. I don't know if the 
solution provided by Ali just mitigates the problem (in the sense 
that less memory is being used) but for now it works on my 
machine(tm).


Re: Make shared static this() encoding table compilable

2022-03-16 Thread zhad3 via Digitalmars-d-learn

On Monday, 14 March 2022 at 10:07:52 UTC, Basile B. wrote:

That's a compiler bug of type "ICE", the compiler crashes.
Try reducing to a simple module that does not use phobos and 
report to bugzilla.


Thank you and sorry for the late reply, I have been quite busy.
You can test this already with just the `table.d` file:

`dmd -release -O table.d` fails on my computer with `DMD64 D 
Compiler v2.098.1`.




Re: static init c struct with array filed

2022-03-16 Thread Ali Çehreli via Digitalmars-d-learn

On 3/16/22 10:03, user1234 wrote:
> On Wednesday, 16 March 2022 at 14:42:02 UTC, Era Scarecrow wrote:
>> On Wednesday, 16 March 2022 at 11:27:20 UTC, user1234 wrote:
>>> assuming the c library takes by reference
>>
>>  My experience all arrays are effectively just pointers
>
> I meant the function that takes the Test2 as parameter, but to be honest
> I dont get exactly what does OP want actually...

They want to pass Test2 to C and be able to use it. D ABI defines arrays 
(slices) as a pair of size_t and a pointer:


  https://dlang.org/spec/abi.html#arrays

I haven't passed such a struct to C but I believe the equivalent C 
struct can assume there are the following members in place of the array:


struct Test2 {
int32_t a;
size_t length;
Test * ptr;
}

However, the lifetime of ptr must be ensured on the D side because 
utless there is a reference to it on the D side, the GC can free the 
memory at any time and C side may access invalid memory.


That is explained here:

  https://dlang.org/spec/interfaceToC.html#storage_allocation

Ali



Re: static init c struct with array filed

2022-03-16 Thread user1234 via Digitalmars-d-learn

On Wednesday, 16 March 2022 at 14:42:02 UTC, Era Scarecrow wrote:

On Wednesday, 16 March 2022 at 11:27:20 UTC, user1234 wrote:

assuming the c library takes by reference


 My experience all arrays are effectively just pointers


I meant the function that takes the Test2 as parameter, but to be 
honest I dont get exactly what does OP want actually... that's 
true that passing a Test2 is somewhat strange as there's no info 
for the array length.




Re: static init c struct with array filed

2022-03-16 Thread Era Scarecrow via Digitalmars-d-learn

On Wednesday, 16 March 2022 at 11:27:20 UTC, user1234 wrote:

assuming the c library takes by reference


 My experience all arrays are effectively just pointers, and the 
brackets/length is only really applicable to stack allocated 
fixed length allocations. In my own C projects i always used 
pointers to pass arrays around.


 There's also static construction, though i don't see how that 
improves anything


https://dlang.org/spec/module.html#staticorder



Re: static init c struct with array filed

2022-03-16 Thread user1234 via Digitalmars-d-learn

On Wednesday, 16 March 2022 at 07:27:06 UTC, test wrote:

```c
struct Test {
int32_t a;
}
struct Test2 {
int32_t a;
Test arr[];
}
```

I need static const init Test2, then pass it to c library 
late(third library, can not change the type def).



I am not sure how to do it in D.


```d
extern(C) struct Test {
int a;
}

extern(C) struct Test2 {
 int a;
 Test* arr;
}


static const Test2 t2 = Test2(2, null);

void main(){
   to_c_library(&t2);
   to_c_library(new Test2(2, null));
   Test2 local;
   to_c_library(&local);
}
```

assuming the c library takes by reference


static init c struct with array filed

2022-03-16 Thread test via Digitalmars-d-learn

```c
struct Test {
int32_t a;
}
struct Test2 {
int32_t a;
Test arr[];
}
```

I need static const init Test2, then pass it to c library 
late(third library, can not change the type def).



I am not sure how to do it in D.