Re: Why many programmers don't like GC?

2021-01-15 Thread welkam via Digitalmars-d-learn

On Friday, 15 January 2021 at 07:35:00 UTC, H. S. Teoh wrote:
(1) Refactored one function called from an inner loop to reuse 
a buffer instead of allocating a new one each time, thus 
eliminating a large amount of garbage from small allocations;

<...>
The result was about 40-50% reduction in runtime, which is 
close to about a 2x speedup.


I think this message needs to be signal boosted. Most of the time 
GC is not the problem. The problem is sloppy memory usage. If you 
allocate a lot of temporary objects your performance will suffer 
even if you use malloc and free. If you write code that tries to 
use stack allocation as much as possible, doesn't copy data 
around, reuses buffers then it will be faster than manual memory 
management that doesn't do that. And thats with a "slow" GC.


Re: Why many programmers don't like GC?

2021-01-15 Thread welkam via Digitalmars-d-learn

On Wednesday, 13 January 2021 at 18:58:56 UTC, Marcone wrote:
I've always heard programmers complain about Garbage Collector 
GC. But I never understood why they complain. What's bad about 
GC?


Most people get to know GC trough Java or C#. Those languages 
promote the use of OOP and they say that you dont need to worry 
about memory management. The result is that people write code 
that doesnt utilize CPU caches effectively and makes a lot of 
temporary allocations. For example people at Microsoft 
implemented json parser in C# and it would allocate 8GB of memory 
for every 1GB parsed. Add to that virtual machines and you find 
that programs written in those languages run like they are coded 
in molasse.


People with experience of those programs conclude that is all 
because of GC. And its simple explanation for simple people.


Re: Why many programmers don't like GC?

2021-01-15 Thread welkam via Digitalmars-d-learn

On Friday, 15 January 2021 at 15:18:31 UTC, IGotD- wrote:
I have a feeling that bump the pointer is not the complete 
algorithm that D uses because of that was the only one, D would 
waste a lot of memory.


Freeing memory is for loosers :D
https://issues.dlang.org/show_bug.cgi?id=21248

DMD allocates and never frees.


Re: Why many programmers don't like GC?

2021-01-15 Thread welkam via Digitalmars-d-learn
On Friday, 15 January 2021 at 14:35:55 UTC, Ola Fosheim Grøstad 
wrote:

On Friday, 15 January 2021 at 14:24:40 UTC, welkam wrote:
You can use GC with D compiler by passing -lowmem flag. I 
didnt measure but I heard it can increase compilation time by 
3x.


Thanks for the info. 3x is a lot
Take it with a grain of salt. I heard it long time ago so I might 
not remember correctly and I didnt measure it myself.



improved with precise collection

Precise GC is slower than default GC.

Making it use automatic garbage collection (of some form) would 
be an interesting benchmark.


-lowmem flag replaces all* allocations with GC allocations so you 
can benchmark that


On Friday, 15 January 2021 at 14:59:18 UTC, Ola Fosheim Grøstad 
wrote:

I think? Or maybe I am missing something?


A write barrier is a peace of code that is inserted before a 
write to an [object]. Imagine you have a class that has pointer 
to another class. If you want to change that pointer you need to 
tell the GC that you changed that pointer so GC can do its magic.

https://en.wikipedia.org/wiki/Write_barrier#In_Garbage_collection


3. Make slices and dynamic arrays RC.


Reference counting needs mutation. How do you define immutable RC 
slice that needs to mutate its reference count? Thats a unsolved 
problem in D.






Re: Why many programmers don't like GC?

2021-01-15 Thread welkam via Digitalmars-d-learn
On Thursday, 14 January 2021 at 18:51:16 UTC, Ola Fosheim Grøstad 
wrote:
One can follow the same kind of reasoning for D. It makes no 
sense for people who want to stay high level and do batch 
programming. Which is why this disconnect exists in the 
community... I think.


The reasoning of why we do not implement write barriers is that 
it will hurt low level programming. But I feel like if we drew a 
ven diagram of people who rely on GC and those who do a lot of 
writes trough a pointer we would get almost no overlap. In other 
words if D compiler had a switch that turned on write barriers 
and better GC I think many people would use it and find the trade 
offs acceptable.


Re: Why many programmers don't like GC?

2021-01-15 Thread welkam via Digitalmars-d-learn
On Friday, 15 January 2021 at 11:28:55 UTC, Ola Fosheim Grøstad 
wrote:

On Friday, 15 January 2021 at 11:11:14 UTC, Mike Parker wrote:
That's the whole point of being able to mix and match. Anyone 
avoiding the GC completely is missing it (unless they really, 
really, must be GC-less).


Has DMD switched to using the GC as the default?


No. And it will never will. Currently DMD uses custom allocator 
for almost everything. It works as follows. Allocate a big 
chunk(1MB) of memory using malloc. Have a internal pointer that 
points to the beginning of unallocated memory. When someone ask 
for memory return that pointer and increment internal pointer 
with the 16 byte aligned size of allocation. Meaning the new 
pointer is pointing to unused memory and everything behind the 
pointer has been allocated. This simple allocation strategy is 
called bump the pointer and it improved DMD performance by ~70%.


You can use GC with D compiler by passing -lowmem flag. I didnt 
measure but I heard it can increase compilation time by 3x.


https://github.com/dlang/dmd/blob/master/src/dmd/root/rmem.d#L153


Re: Should a parser type be a struct or class?

2020-06-18 Thread welkam via Digitalmars-d-learn

Oh an also https://github.com/dlang/dmd/pull/9899


Re: Should a parser type be a struct or class?

2020-06-18 Thread welkam via Digitalmars-d-learn

On Wednesday, 17 June 2020 at 14:32:09 UTC, Adam D. Ruppe wrote:

On Wednesday, 17 June 2020 at 14:24:01 UTC, Stefan Koch wrote:

Parser in dmd does even inherit from Lexer.


why would a parser ever inherit from a lexer?


So you can write nextToken() instead of lexer.nextToken()


32 bit phobos

2020-06-09 Thread welkam via Digitalmars-d-learn
I tried to compile a 32 bit executable but my system does not 
have 32 bit phobos. How do I get 32 bit phobos? I am using 
Manjaro.


dmd -m32 source/test2.d
/usr/bin/ld: skipping incompatible 
/usr/lib/gcc/x86_64-pc-linux-gnu/10.1.0/../../../libphobos2.a 
when searching for -lphobos2
/usr/bin/ld: skipping incompatible /usr/lib/libphobos2.a when 
searching for -lphobos2

/usr/bin/ld: cannot find -lphobos2
collect2: error: ld returned 1 exit status
Error: linker exited with status 1




Re: I want Sublime 3 D auto import !

2020-06-02 Thread welkam via Digitalmars-d-learn

On Tuesday, 2 June 2020 at 06:00:10 UTC, Виталий Фадеев wrote:

On Monday, 1 June 2020 at 18:55:03 UTC, Paul Backus wrote:

On Monday, 1 June 2020 at 16:18:44 UTC, Виталий Фадеев wrote:


I do it!

https://github.com/vitalfadeev/SublimeDlangAutoImport


Cool. I dont use classe but I see how this kind of functionality 
might be useful for other symbols.


Re: Mir Slice Column or Row Major

2020-05-28 Thread welkam via Digitalmars-d-learn

On Wednesday, 27 May 2020 at 16:53:37 UTC, jmh530 wrote:
Not always true...many languages support column-major order 
(Fortran, most obviously).


if your column major matrix is implemented as
matrix[row_index][column_index]
then ok no puppies will be hurt. But I dont see much value in 
such implementations.


Re: Mir Slice Column or Row Major

2020-05-27 Thread welkam via Digitalmars-d-learn

On Wednesday, 27 May 2020 at 01:31:23 UTC, data pulverizer wrote:

column major


Cute puppies die when people access their arrays in column major.


Re: How to get the pointer of "this" ?

2020-05-25 Thread welkam via Digitalmars-d-learn

On Sunday, 24 May 2020 at 17:05:16 UTC, Vinod K Chandran wrote:

cast(DWORD_PTR) this);


Where is DWORD_PTR defined? I cant find it in docs. If its an 
alias of long then you have to cast to a pointer like this

cast(long*) this;
you need to specify that you want to cast to a pointer of type T. 
In this case T is long.


Re: How to allocate/free memory under @nogc

2020-05-22 Thread welkam via Digitalmars-d-learn

There is automem to help with manual memory management
https://code.dlang.org/packages/automem


Re: How to use this forum ?

2020-05-20 Thread welkam via Digitalmars-d-learn

On Wednesday, 20 May 2020 at 20:49:52 UTC, Vinod K Chandran wrote:

Hi all,
I have some questions about this forum.
1. How to edit a post ?
2. How to edit a reply ?
3. How to add some code(mostly D code) in posts & replies.
4. How to add an image in posts & replies.
5. Is there a feature to mark my post as "[SOLVED]" ?


This is not a forum but a frontend to a mailing list. Since you 
cant edit sent emails you cant do 1, 2 and 5. if you want to add 
code then post it in a message if its short. If not try using 
github gist.


Re: large Windows mingw64 project in C99 --- need ABI compatible D compiler

2020-05-20 Thread welkam via Digitalmars-d-learn

On Wednesday, 20 May 2020 at 18:53:01 UTC, NonNull wrote:
Which D compiler should be used to be ABI compatible with 
mingw32? And which to be ABI compatible with mingw64?
I am not expert here but doesnt all C compilers have the same 
ABI? If yes then compile your code in 32 bits for 32 bit C obj 
files. If you are on 64 bit OS then D compilers will produce 64 
bit executables by default. To change that use 
https://dlang.org/dmd-linux.html#switch-m32






Re: Is it possible to write some class members in another module ?

2020-05-20 Thread welkam via Digitalmars-d-learn

On Wednesday, 20 May 2020 at 17:29:47 UTC, Vinod K Chandran wrote:
I am very sad that i delayed to start learning D only because 
of semicolons and curly braces.
Walter(creator of this language) said that redundant grammar is a 
good thing because it allows compiler to emit better error 
messages.


Re: Is it possible to write some class members in another module ?

2020-05-20 Thread welkam via Digitalmars-d-learn

On Wednesday, 20 May 2020 at 09:45:48 UTC, Vinod K Chandran wrote:

// Now, we need to use like this
auto form= new Window();
form.event.click = bla_bla;

// I really want to use like this
auto form= new Window();
form.click = bla_bla;
```


Then you want alias this.

class Window : Control{
   Events event;
   alias event this
   .
}

and now when you write
form.click = bla_bla;
Compiler first checks if form.click compiles and if it doesnt it 
then it tries form.event.click


Re: Beginner's Comparison Benchmark

2020-05-06 Thread welkam via Digitalmars-d-learn
On Tuesday, 5 May 2020 at 20:29:13 UTC, Steven Schveighoffer 
wrote:
the optimizer recognizes what you are doing and changes your 
code to:


writeln(1_000_000_001);

Oh yes a classic constant folding. The other thing to worry about 
is dead code elimination. Walter has a nice story where he sent 
his compiler for benchmarking and the compiler figured out that 
the the result of the calculation in benchmark is not used so it 
deleted the whole benchmark.


Re: How can I open a Binary EXE with Hexadecimal?

2020-05-02 Thread welkam via Digitalmars-d-learn

On Saturday, 2 May 2020 at 21:05:32 UTC, Baby Beaker wrote:
I need open a Binary EXE, it can be using "rb" mode and convert 
it to Hexadecimal for me make some changes and save as "rb" 
again. How can I make it? Thank you.


You dont convert binary data to hexadecimal. You display it as 
hexadecimal. If you want to print file contents to console in hex 
do something like this.


import std.stdio;
import std.algorithm;

auto fd = File("filename", "r"); //open file for reading
ubyte[] data; //a GC allocated buffer for data
fd.rawRead(data); //read the data

data[0..50].each!(n => stdout.writef("%X ",n));
/*take 50 elements from array and for each of them call lambda 
that converts ubyte to string in hex format and prints to the 
stdout*/


if you want to assign to this data you can do something like this

data[0] = Oxff;

And then write to file


Re: Newbie linker errors - still missing _fltused _tls_index _tls_used localtime tzset mainCRTStartup

2020-04-26 Thread welkam via Digitalmars-d-learn
If I remember correctly VS studio has different release and debug 
configurations for both compiler and linker. If release works but 
debug doesnt make sure that when you add things to one to also 
add to another


Re: Using shared memory and thread

2019-11-05 Thread welkam via Digitalmars-d-learn

On Monday, 4 November 2019 at 19:53:29 UTC, bioinfornatics wrote:
3/ variable flagged as `shared` does at mean the variable is 
put into L2 cache ?


First caching is controlled by CPU not programmer.
Second not all architectures share L2 between cores.
4 jaguar cores share L2 cache in consoles
https://images.anandtech.com/doci/6976/Screen%20Shot%202013-05-23%20at%2012.22.12%20AM_678x452.png

2 bulldozer cores share L2 cache in FX processors
https://s3.amazonaws.com/hs-wordpress/wp-content/uploads/2017/12/13035240/bulldozer_021.jpg

each zen core has its own L2 cache in Ryzen cpu's
https://images.anandtech.com/doci/10591/HC28.AMD.Mike%20Clark.final-page-014.jpg


Third your link to run.dlang is 404

While you cant control cache contents explicitly you can control 
it by changing memory access patterns. When you fetch a peace of 
data CPU will load 64 byte size bucket/chunk that contains your 
data into L1 cache. Since L1 cache size is limited CPU had to 
remove existing data from L1 based on its eviction policy. 
Evicted data goes into L2 cache. That is the way to control what 
is where.


By default all variables(except immutable or static) are thread 
local and can only be accessed by one thread. Shared keyword 
makes data accessible trough multiple threads





Re: Eliding of slice range checking

2019-10-25 Thread welkam via Digitalmars-d-learn

On Thursday, 24 October 2019 at 21:02:03 UTC, Per Nordlöw wrote:

On Thursday, 24 October 2019 at 18:37:05 UTC, welkam wrote:
I remember in some video Chandler Carruth said that value 
range propagation across function boundary was implemented in 
llvm but later removed because it produced no performance 
improvement for C and C++ code. I wonder how it fare when used 
on D code.


Interesting. What uses of VRP do you see in D?


Now that you asked I realized that it wont be that much useful 
because in D all arrays go together with their size so cross 
function VRP would do little.


Re: Eliding of slice range checking

2019-10-24 Thread welkam via Digitalmars-d-learn

On Wednesday, 23 October 2019 at 11:20:59 UTC, Per Nordlöw wrote:
Does DMD/LDC avoid range-checking in slice-expressions such as 
the one in my array-overload of `startsWith` defined as


bool startsWith(T)(scope const(T)[] haystack,
   scope const(T)[] needle)
{
if (haystack.length >= needle.length)
{
return haystack[0 .. needle.length] == needle; // is 
slice range checking avoid here?

}
return false;
}

///
@safe pure nothrow @nogc unittest
{
auto x = "beta version";
assert(x.startsWith("beta"));
}

when building in release mode?

I remember a DMD pull from Ian Buclaw that enabled some eliding 
but I don't remember if it includes the case above.


How can I investigate the codegen myself here?


I remember in some video Chandler Carruth said that value range 
propagation across function boundary was implemented in llvm but 
later removed because it produced no performance improvement for 
C and C++ code. I wonder how it fare when used on D code.


Re: Remove unwanted ' \r ' from an Array ... by reading a *.txt file.

2019-10-24 Thread welkam via Digitalmars-d-learn

On Thursday, 24 October 2019 at 16:49:09 UTC, Mil58 wrote:

On Thursday, 24 October 2019 at 16:21:47 UTC, welkam wrote:

On Thursday, 24 October 2019 at 15:27:05 UTC, Mil58 wrote:

[...]


void main() {
File("data.txt", "r+")
.byLineCopy()
.array()
.each!writeln;
}

byLineCopy removes the new lines. If in the future you would 
need the new line symbol call byLineCopy(Yes.keepTerminator)


Sorry but not working ... Surely i'm not able to insert and 
adapt to my own script !  :-(
Could you, please, modify with the right syntax at the right 
place ? - Thanks.
Ah sorry my bad. byLineCopy defaults to '\n' as line terminator 
so it worked on my system.


import std.algorithm;
File("data.txt", "r+")
.byLineCopy(No.keepTerminator, '\r')
.joiner(" - ")
.writeln;

this will print the thing you wanted. If you want to capture that 
value in a variable then


auto result = File("data.txt", "r+")
   .byLineCopy(No.keepTerminator, '\r')
   .joiner(" - ");


Re: [DTrace probe] is there a [portable] way to add section to elf executable?

2019-10-24 Thread welkam via Digitalmars-d-learn

On Wednesday, 23 October 2019 at 15:24:13 UTC, drug wrote:
I'd like to add (and modify) section to ELF executable to 
implement DTrace probes. DTrace does it in probe assembly:

```
__asm__ __volatile__ (
"990: nop
.pushsection .note.stapsdt,\"?\",\"note\"
.balign 4
.4byte 992f-991f, 994f-993f, 3
991: .asciz \"stapsdt\"
992: .balign 4
993: .8byte 990b
.8byte _.stapsdt.base
.8byte 0
.asciz \"myapp\"
.asciz \"func_call\"
.asciz \"%n[_SDT_S1]@%[_SDT_A1] %n[_SDT_S2]@%[_SDT_A2]\"
994: .balign 4
.popsection"
:: [_SDT_S1] "n" (SNIPPED_ONE),
   [_SDT_A1] "nor" ((a)),
   [_SDT_S2] "n" (SNIPPED_TWO),
   [_SDT_A2] "nor" ((b))
);
```

So I need a way to add no op operation and define some data in 
`.note.stapsdt` section. This page 
https://dlang.org/spec/iasm.html does not contain any 
information about sections


I hope you are not mixing up D SCRIPTING language that is used by 
Dtrace and D PROGRAMMING language.


Second thing is that ELF format is used by Linux and other 
operating systems use different executable formats (mach-0 for 
macOs and PE for windows). So by definition its not portable.


D's inline asm does not provide a way to modify *.obj files. It 
just insert your asm at that place.


Re: dub build doesn't work

2019-10-24 Thread welkam via Digitalmars-d-learn

On Thursday, 24 October 2019 at 01:17:24 UTC, OiseuKodeur wrote:
BTW if you prefer using optlink and the  digitalmars C 
runtime, you can instruct dub to do so with: --arch=x86


how can i add --arch=x86 flag to the dub.json so it do it 
automatically ?


"dflags": [
"--arch=x86"
]

This should work. Didnt tried though


Re: Converting a ulong to a byte array and constructing a ulong from it

2019-10-24 Thread welkam via Digitalmars-d-learn

On Thursday, 24 October 2019 at 14:08:36 UTC, 9898287 wrote:
Does this contain any undefined behavior? It is in C as far as 
I knew.


immutable int number = 1;
auto bad_idea = (cast(ubyte*) )[0 .. number.sizeof];
bad_idea[0] = 2;
writeln(number); //1
writeln(*(cast(int*)bad_idea.ptr)); //2

Cast is a powerful tool and with it you can destroy all type 
systems guarantees. In your case you will not have any problems 
just stay away from const and immutable


Re: Remove unwanted ' \r ' from an Array ... by reading a *.txt file.

2019-10-24 Thread welkam via Digitalmars-d-learn

On Thursday, 24 October 2019 at 15:27:05 UTC, Mil58 wrote:

Hi all It's me again ;-)
(because you're very strong in Dlang, here...)
In want a result as i write in a comment, with remove unwanted 
'\r' >>


import std.stdio;
import std.file;
import std.conv;
import std.process : executeShell;

/* Content of 'values.txt' >>
   abcd
   1234
   03b52h
*/

void main() {
auto fichier = File("values.txt", "r+");
string[] buffer;
  foreach (line ; File("values.txt").byLine)
buffer ~= line.to!string;

  writeln(buffer);
  // Result: ["abcd\r", "1234\r", "03b52h"]

  writeln(buffer[0], " - ", buffer[1], " - ", buffer[2]);
  // Result:  - 03b52h:-(

  // Instead, i would :  abcd - 1234 - 03b52h  !!!

executeShell("pause");
fichier.close();
}

Thanks a lot, by advance :-)


void main() {
File("data.txt", "r+")
.byLineCopy()
.array()
.each!writeln;
}

byLineCopy removes the new lines. If in the future you would need 
the new line symbol call byLineCopy(Yes.keepTerminator)


Re: gcc 9 vs. dmd?

2018-11-30 Thread welkam via Digitalmars-d-learn
On Friday, 30 November 2018 at 04:47:26 UTC, Andrew Pennebaker 
wrote:


gcc is currently required for dmd on FreeBSD, as dmd links to 
libstdc++.


Parts of dmd are still written in C++ but most of it was 
converted recently. More on that here:

"DMD backend now in D"
https://forum.dlang.org/thread/psaekt$tah$1...@digitalmars.com


Re: D is supposed to compile fast.

2018-11-26 Thread welkam via Digitalmars-d-learn

On Sunday, 25 November 2018 at 22:00:21 UTC, Chris Katko wrote:.


So 1) I have to compile manually, then link. Except that also 
runs the files every time even if they're up-to-date. Is that 
normal behavior for C/C++?


Well you dont have to use separate commands but yes compiling and 
linking are two steps and its normal behavior for all native 
languages. Each OS have their own linker and we dont control that.



Two questions/topics/issues:

---

#1 How to I only build files that are dirty? Do I actually need 
a build program like DUB, MAKE, or CMAKE to do that? (Can make, 
cmake be used?) How do they recognize files are out-dated if 
DMD can't? Is that just an industry-standard 
specialization/separation-of-responsibilities to not have the 
compiler auto-detect up-to-date builds?


Yes its separation of responsibilities and there are many tools 
to automate that. Take a look at rdmd


"rdmd recompiles files only on a needed basis, e.g. two 
invocations of rdmd in sequence without an intervening change to 
any relevant source file does not produce the executable again."


https://dlang.org/rdmd.html

If dub or rdmd doesnt satisfy your needs then you will need to 
learn other build system.




I have to tell you that, as an outsider (who is VERY interested 
in D), this is very frustrating. "Compile times are fast" != 
"build times" is a huge misconception that borders on being a 
clever lie or twisting of words. When people hear "compile 
times", they think "time to compile the whole project" not 
"time to compile a simple test case that doesn't use any 
typical D features--also, it's not linking." Second, as shown 
here, it's not fast even for compiling! Because the second you 
touch std.regex (which has NO WARNINGS in the documentation), 
you're greeted with another clever lie-by-omission: a 10x 
explosion of build time over some modules.


Yes D have some rough spots thats for sure. For compile times 
these include std.regex, std.format and heavy CTFE use. There is 
newCTFE engine that is faster but its not ready yet. That said D 
code compiles faster than C++ and Rust. To really put fast into 
perspective read this 
https://news.ycombinator.com/item?id=18442941



So my point is, I keep running into either misconceptions that 
conveniently make D look good, and other gotchas with NO 
DOCUMENTATION that make the language much slower to work with 
than expected.


People who advertise languages talk about positives and leave out 
negatives. One thing you need to know - D doesnt have huge 
sponsor and some places might be rough because of it. Currently 
core dev team focus more on stability and bug fixes than build 
speeds. I think you rather have compiler that works than compiler 
that crashes 1 sec faster.


Its not all negatives with regex. Current regex implementation is 
one of the fastest in the world. Thats classic D trade of - you 
spend compile time for better runtime.


I mean, can you think of any module in the Python/Javascript/C# 
standard library that simply including it will swell your 
program to the point it can't compile?


Have you tried C++ boost?





Re: D is supposed to compile fast.

2018-11-24 Thread welkam via Digitalmars-d-learn

On Friday, 23 November 2018 at 08:57:57 UTC, Chris Katko wrote:

D is supposed to compile fast.


You didnt read the fine print. It compiles simple code fast. Also 
compilation is separate step from linking and your program might 
spend half of "compilation" time in link phase.




Re: Why does nobody seem to think that `null` is a serious problem in D?

2018-11-21 Thread welkam via Digitalmars-d-learn

On Wednesday, 21 November 2018 at 09:20:01 UTC, NoMoreBugs wrote:
On Tuesday, 20 November 2018 at 15:46:35 UTC, Adam D. Ruppe 
wrote:

On Tuesday, 20 November 2018 at 13:27:28 UTC, welkam wrote:
Because the more you learn about D the less you want to use 
classes.


classes rock. You just initialize it. You're supposed to 
initialize *everything* anyway.


a fan of classes...on the D forum? I don't get it.

but of course you are right. classes do rock!

In fact, there is not a better programming construct that I am 
aware of, the provides a better 'explicit' mapping from 
external objects to program constructs.


Thank you Kristen and Ole-Johan.


One thing that bugs me in programming is that in different 
programming languages the same things are named differently and 
things that are named the same are different. For example D`s 
slices and C#`s spans are the same thing. C++ string_view might 
be considered the same also, but classes in C++ and Java are not 
the same thing.


In D classes are reference type and unless you mark them as final 
they will have vtable. Lets face it most people dont mark their 
classes as final. What all this mean is that EVERY access to 
class member value goes trough indirection (additional cost) and 
EVERY method call goes trough 2 indirections (one to get vtable 
and second to call function(method) from vtable). Now Java also 
have indirect vtable calls but it also have optimization passes 
that convert methods to final if they are not overridden. If Java 
didnt do that it would run as slow as Ruby. AFAIK D doesnt have 
such optimization pass. On top of that some people want to check 
on EVERY dereference if pointer is not null. How slow you want 
your programs to run?


Thats negatives but what benefit classes give us?
First being reference type its easy to move them in memory. That 
would be nice for compacting GC but D doesnt have compacting GC.
Second they are useful for when you need to run code that some 
one else wrote for your project. Something like plugin system. 
[sarcasm]This is happening everyday[/sarcasm]

Third porting code from Java to D.

Everything else you can do with struct and other D features.


Re: Why does nobody seem to think that `null` is a serious problem in D?

2018-11-20 Thread welkam via Digitalmars-d-learn
On Monday, 19 November 2018 at 21:23:31 UTC, Jordi Gutiérrez 
Hermoso wrote:
Why does nobody seem to think that `null` is a serious problem 
in D?


Because the more you learn about D the less you want to use 
classes. I view class as compatibility feature when you want to 
port Java code to D. For regular code just use structs. For 
inheritance you could use alias, for polymorphism - templates 
etc. If you want to write OOP code you dont have to always start 
with keyword class.


And since you dont use classes you dont view null as a high 
priority problem.


Re: What is best way to read and interpret binary files?

2018-11-20 Thread welkam via Digitalmars-d-learn
On Tuesday, 20 November 2018 at 12:01:49 UTC, Stanislav Blinov 
wrote:

On Tuesday, 20 November 2018 at 11:54:59 UTC, welkam wrote:
On Monday, 19 November 2018 at 22:14:25 UTC, Neia Neutuladh 
wrote:


Nothing stops you from writing:

SomeStruct myStruct;
fd.rawRead((cast(ubyte*))[0..SomeStruct.sizeof]);

Standard caveats about byte order and alignment.


Never would I thought about casting struct to static array. If 
I understood correctly you cast myStruct pointer to ubyte 
pointer and then construct static array on stack with 
tmpArray.ptr = (ubyte pointer) and tmpArray.sizeof = 
SomeStruct.sizeof


Almost correct, except it's not a static array, it's just a 
slice, i.e. ubyte[].


I guess it came from inseparability with C where you want to 
slice C arrays? Thats useful to know


Re: What is best way to read and interpret binary files?

2018-11-20 Thread welkam via Digitalmars-d-learn

On Monday, 19 November 2018 at 22:14:25 UTC, Neia Neutuladh wrote:


Nothing stops you from writing:

SomeStruct myStruct;
fd.rawRead((cast(ubyte*))[0..SomeStruct.sizeof]);

Standard caveats about byte order and alignment.


Never would I thought about casting struct to static array. If I 
understood correctly you cast myStruct pointer to ubyte pointer 
and then construct static array on stack with tmpArray.ptr = 
(ubyte pointer) and tmpArray.sizeof = SomeStruct.sizeof


What I figured out when I woke up is that I never needed c style 
arrays. What I could do is to allocate enough data for all file 
in ubyte array and just use slices to read data by chunks and 
cast them into necessary structs.


Thanks Neia Neutuladh and H. S. Teoh for giving me some pointers
https://www.explainxkcd.com/wiki/index.php/138:_Pointers


What is best way to read and interpret binary files?

2018-11-19 Thread welkam via Digitalmars-d-learn
So my question is in subject/title. I want to parse binary file 
into D structs and cant really find any good way of doing it. 
What I try to do now is something like this


byte[4] fake_integer;
auto fd = File("binary.data", "r");
fd.rawRead(fake_integer);
int real_integer = *(cast(int*)  fake_integer.ptr);

What I ideally want is to have some kind of c style array and 
just cast it into struct or take existing struct and populate 
fields one by one with data from file. Is there a D way of doing 
it or should I call core.stdc.stdio functions instead?


Re: Can opApply be made @nogc?

2018-10-21 Thread welkam via Digitalmars-d-learn

DIP 1000 says:

Delegates currently defensively allocate closures with the GC. 
Few actually escape, and with scope only those that actually 
escape need to have the closures allocated.


https://github.com/dlang/DIPs/blob/master/DIPs/DIP1000.md#benefits


Re: What does -vtls compiler flag does and ...

2018-10-04 Thread welkam via Digitalmars-d-learn
On Wednesday, 3 October 2018 at 21:50:49 UTC, Stanislav Blinov 
wrote:

Thread-local storage is memory allocated for each thread.
Only static non-immutable variables go there. Regular variables 
on the stack aren't explicitly placed in any TLS, they're, 
well, on the stack as it is.


Oh so its like thread local globals sort of. My reasoning was 
that stack is a form of storage and is thread local so...


Thanks for responses it became more clear what this flag does.

Quick tests with different compilers.

DMD from Arch repo compiles hello world (dmd -c main.d) in:
~0.1s
DMD compiled with DMD and stock flags
~0.12s
DMD compiled with ldc with -o2
~0.35s


DMD code gen is not that bad


Re: What does -vtls compiler flag does and ...

2018-10-03 Thread welkam via Digitalmars-d-learn

string a = "test";
is a variable that mutates so it should be thread local. Also ASM 
output shows that these variables are not optimized away so 
compiler should output something but it doesnt. Or I dont 
understand thread local storage.


Re: What does -vtls compiler flag does and ...

2018-10-03 Thread welkam via Digitalmars-d-learn
On Wednesday, 3 October 2018 at 20:58:01 UTC, Stanislav Blinov 
wrote:
No, all *static non-immutable* variables are thread-local by 
default, not just "all variables".

I misspoke but this should write something
https://run.dlang.io/is/3u1wFp

If you look at asm output there are "call _d_arraycatT@PLT32" 
instruction so those variables weren't optimized away and yet no 
output to stdout.


If remove that flag ldc now compiles dmd but lacks optimizations 
so more work needs to be done.


What does -vtls compiler flag does and ...

2018-10-03 Thread welkam via Digitalmars-d-learn
I was playing around with dmd`s make file trying to see if I can 
compile dmd with different compilers and different compilation 
flags. By playing around I found that some dmd files are compiled 
with -vtls flag unconditionally and that ldc do not support this 
flag. First I dont know what -vtls flag does so I looked at 
documentation.


DMD documentation says:
-vtls
List all variables going into thread local storage

GDC documentation says:
-fd-vtls
Print information about all variables going into thread local 
storage to stdout.


This doesnt help much. I still have no idea what it actually 
does. Aren't all variables thread local unless explicitly 
specified? So I tried to compile simple example and dmd printed 
nothing. GDC printed this:

gdc: error: unrecognized command line option ‘-fd-vtls’

One gdc wiki(https://wiki.dlang.org/Using_GDC) is out of date and 
second I still have no clue what is does.


This flag blocks compilation and I want to just remove it but 
since I dont know what it is used for I hesitate. If my attempts 
were to be successful in compiling dmd with other compilers I 
would make pull request but now I have more question than 
answers. What this flag does? Why some files compiled with it and 
other dont? Why it is added to all compilation targets 
unconditionally?


Setting up DMD on windows

2018-02-03 Thread welkam via Digitalmars-d-learn
Tried to use DMD compiler that I built from source by following 
these instructions https://wiki.dlang.org/Building_under_Windows


They are outdated but I managed to compile it but I get this 
error when I tried to compile some code.


dmdm -run Main.d
C:\D\dmd2\windows\32\..\..\src\druntime\import\core\sys\windows\winbase.d(1828):
 Error:
object.Error@(0): Access Violation

0x0064FED6
0x006502BE
0x006499CF
0x006488ED



dmdm is just alias dmdm=C:\D\dmd2\windows\32\dmd.exe $*

the compiler version is dmd-2.078.0

Could some one help fix this?


Re: How to proceed with learning to code Windows desktop applications?

2018-01-29 Thread welkam via Digitalmars-d-learn

On Monday, 29 January 2018 at 22:55:12 UTC, I Lindström wrote:


The other way I've been thinking is to do the thing 
browser-based, but for some reason that doesn't feel right.


Well it didnt felt wrong for Microsoft to use modified internet 
explorer to make calculator. You can read more on universal 
windows apps and XAML


To learn GUI development first find GUI framework like GTK, QT 
etc. then watch and/or read tutorials on how to use them. For D 
language a list of libraries could be found here

https://wiki.dlang.org/GUI_Libraries


Re: Looks like wrong error message

2018-01-28 Thread welkam via Digitalmars-d-learn
On Sunday, 28 January 2018 at 20:42:52 UTC, Jonathan M Davis 
wrote:


There is nothing incorrect about the error message. The 
compiler looked at all of the functions in the overload set, 
and it found none that matched. The reason that it found none 
that matched was because it couldn't find any function template 
where those arguments passed the template constraint, and it 
tells you so.


So, there is no bug here in the sense that the compiler is 
telling you the wrong thing. You can certainly open a bug 
report arguing that the error messages isn't human-friendly 
enough and suggest alternatives, and something along those 
lines may be implemented, but the message isn't actually wrong. 
So, feel free to open a bug report.


However, you're not going to get an error message that says 
anything like "the arguments aren't the same type." The 
compiler doesn't understand what the template constraint means 
in "human terms." It just knows whether it's true or false, and 
in this case, if you provide arguments that don't have a common 
type that they implicitly convert to, then the template 
constraint will fail. But ultimately, you're going to have to 
read the template constraint and figure out why the arguments 
are failing.


- Jonathan M Davis


I would not complain if there were multiple functions just like 
error said. But the set contains only one making it not a set 
anymore and it says nothing about constraints.


I downloaded dmd source. Might figure out something myself 
tomorrow


Looks like wrong error message

2018-01-28 Thread welkam via Digitalmars-d-learn
Error says that it cant deduce function from argument types but 
in reality it fails to meet function template constraints. In 
this case !is(CommonType!(staticMap!(ElementType, 
staticMap!(Unqual, Ranges))) == void)


In human terms it means that arguments are not the same type. Is 
this require bug report or it works as expected?


Code:
void main() {
import std.range : chain;
string s = "string";
auto arrayOfStrings = ["string"];
arrayOfStrings.chain(s);
}

error:
Main.d(9): Error: template std.range.chain cannot deduce function 
from argument types !()(string[], string), candidates are:
C:\D\dmd2\windows\bin\..\..\src\phobos\std\range\package.d(894):  
  std.range.chain(Ranges...)(Ranges rs) if (Ranges.length > 0 
&& allSatisfy!(isInputRange, staticMap!(Unqual, Ranges)) && 
!is(CommonType!(staticMap!(ElementType, staticMap!(Unqual, 
Ranges))) == void))




Re: D generates large assembly for simple function

2018-01-28 Thread welkam via Digitalmars-d-learn

On Sunday, 28 January 2018 at 14:33:04 UTC, Johan Engelen wrote:


Careful with these comparisons guys. Know what you are looking 
at.


Wise words


Re: Writing a screen saver in D what libraries will help?

2015-10-12 Thread welkam via Digitalmars-d-learn

On Monday, 12 October 2015 at 19:16:10 UTC, Gary Willoughby wrote:
If I was writing a screensaver in D what libraries are 
available for opening a window and drawing sprites, etc on it. 
GPU accelerated if possible.


I'm using Ubuntu 14.04 and latest DMD compiler.


You will need to interface to C libraries like SDL. There are 
bidings for it on DUB called derelict-sdl2


Re: How To: Passing curried functions around

2015-09-06 Thread welkam via Digitalmars-d-learn

Now its clearer to me. You want delegates
http://wiki.dlang.org/Function_literals




Re: How to use ranges?

2015-08-23 Thread welkam via Digitalmars-d-learn

There was a talk on ranges in Dconf 2015
https://www.youtube.com/watch?v=A8Btr8TPJ8clist=PLEDeq48KhndP-mlE-0Bfb_qPIMA4RrrKoindex=10