Re: How can I make this work?

2021-02-28 Thread Max Haughton via Digitalmars-d-learn

On Sunday, 28 February 2021 at 09:18:56 UTC, Rumbu wrote:

On Sunday, 28 February 2021 at 09:04:49 UTC, Rumbu wrote:

On Sunday, 28 February 2021 at 07:05:27 UTC, Jack wrote:
I'm using a windows callback function where the user-defined 
value is passed thought a LPARAM argument type. I'd like to 
pass my D array then access it from that callback function. 
How is the casting from LPARAM to my type array done in that 
case?


for example, I need something like this to work:

int[] arr = [1, 2, 3];
long l = cast(long) cast(void*) arr.ptr;
int[] a = cast(int[]) cast(void*) l;


LPARAM is not long on 32 bits, it's int. Use LPARAM instead of 
long.


And you are passing only the address of the first element this 
way, loosing the array/slice length. This should work, but keep 
in mind that you have no warranty that the array stays in 
memory and it is not garbage collected.


int[] arr = [1, 2, 3];
LPARAM l = cast(LPARAM)cast(void*)
int[] a = *cast(int[]*)(cast(void*)l);


Do the windows APIs expect the length in memory rather than as a 
parameter?


Also Rumbu can you check your email - I may have emailed you on 
an old email address by accident, but it's about the blog and it 
will be from mh240@...


Re: Problem Computing Dot Product with mir

2021-02-22 Thread Max Haughton via Digitalmars-d-learn

On Monday, 22 February 2021 at 07:14:26 UTC, 9il wrote:
On Sunday, 21 February 2021 at 16:18:05 UTC, Kyle Ingraham 
wrote:
I am trying to convert sRGB pixel values to XYZ with mir using 
the following guide: 
http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html


[...]


mir-glas is deprecated experimental project. It is worth use 
mir-blas or lubeck instead. There is also naming issue. In the 
classic BLAS naming dot refers a function that accept two 1D 
vectors.


Deprecated as in formally dead or postponed?


Re: Mixed language projects (D and C++)

2021-02-19 Thread Max Haughton via Digitalmars-d-learn

On Friday, 19 February 2021 at 10:18:28 UTC, Preetpal wrote:

On Friday, 19 February 2021 at 10:01:36 UTC, Max Haughton wrote:

On Friday, 19 February 2021 at 09:44:15 UTC, Preetpal wrote:
I want to reuse existing C++ code in a new project that I am 
writing in D and I want to use D in an existing C++ code base 
(it is not large). I do not anticipate interop being an issue.


[...]


C++ interop is used every day. The LLVM D compiler, ldc, uses 
it to talk to LLVM efficiently.


There are good resources on it on this very website


I am looking for suggestions on what build system to use. I 
took a look at the LDC project but it looks they are writing 
their own CMake scripts in the repository itself to add support 
for D to CMake. I was hoping there might be something that 
others were using with out of box support for D.


I would keep it simple and use the dub pregenerate step to run 
any old C++ build process then link as usual.


Re: Mixed language projects (D and C++)

2021-02-19 Thread Max Haughton via Digitalmars-d-learn

On Friday, 19 February 2021 at 09:44:15 UTC, Preetpal wrote:
I want to reuse existing C++ code in a new project that I am 
writing in D and I want to use D in an existing C++ code base 
(it is not large). I do not anticipate interop being an issue.


[...]


C++ interop is used every day. The LLVM D compiler, ldc, uses it 
to talk to LLVM efficiently.


There are good resources on it on this very website


Re: Profiling

2021-02-10 Thread Max Haughton via Digitalmars-d-learn
On Wednesday, 10 February 2021 at 13:31:09 UTC, Guillaume Piolat 
wrote:

On Wednesday, 10 February 2021 at 11:52:51 UTC, JG wrote:

[...]


Here is what I use for sampling profiler:

(On Windows)

Build with LDC, x86_64, with dub -b release-debug in order to 
have debug info.

Run your program into:
- Intel Amplifier (free with System Studio)
- AMD CodeXL (more lightweight, and very good)
- Very Sleepy

(On Mac)

Build with dub -b release-debug
Run your program with Instruments.app which you can find in 
your Xcode.app


(On Linux)
I don't know.


Though most of the time to validate the optimization a 
comparison program that runs two siilar programs and computer 
the speed difference can be needed.


All Intel tools I'm aware of support (and are free on) Linux. 
Also, it's just called vTune now and it's been put under the 
"oneAPI" banner.


Re: GC.addRange in pure function

2021-02-09 Thread Max Haughton via Digitalmars-d-learn

On Tuesday, 9 February 2021 at 19:53:27 UTC, Temtaime wrote:

On Sunday, 7 February 2021 at 14:13:18 UTC, vitamin wrote:
Why using 'new' is allowed in pure functions but calling 
GC.addRange or GC.removeRange isn't allowed?


pure is broken. Just don't [use it]



[Citation needed]


Re: My simple internet client made in Dlang.

2021-02-04 Thread Max Haughton via Digitalmars-d-learn

On Thursday, 4 February 2021 at 20:54:15 UTC, Ali Çehreli wrote:

On 2/3/21 8:44 AM, Marcone wrote:

[...]


I think the following would be improvements:

>[...]

I don't know the protocol but obviously 8192 must be sufficient.

[...]


Less calls to std.conv can also mean less surface area for 
exceptions to throw from


Re: Can change vtbl record at runtime ?

2021-02-03 Thread Max Haughton via Digitalmars-d-learn
On Wednesday, 3 February 2021 at 05:30:37 UTC, Виталий Фадеев 
wrote:

Reason:
Reuse component,
bind custom callback without creating new class.

Concept example:
class SaveFilePopup
{
void onSuccess() { /* default operations */ }
}

auto saveFile = new SaveFilePopup();
saveFile.onSuccess = { /* New operations */ }

Delegate:
may be... but, for speed reason, is possible to set the 
default code at compile-time ?


class X
{
   void delegate() onSuccess = { /* default code */ };
}

Context:
GUI, components, callbacks

Possible to change the vtbl record at runtime ?
Has functional for update vtbl records ?


Do you mean "Can I set onSuccess" at runtime? The virtual tables 
are relied upon by the compiler so I wouldn't play with them.


Re: Refactoring tools

2021-02-03 Thread Max Haughton via Digitalmars-d-learn

On Wednesday, 3 February 2021 at 07:20:06 UTC, Imperatorn wrote:

As the title says:

1. Are there any (automated) refactoring tools for D?
2. If not, why? (Is D still too small?)


D is also designed to not need as much refactoring as other 
languages, so even for our size there isn't a huge amount of 
demand.


Previously the actual implementation would've been hard but now 
we have the compiler working fairly well as a library it's just a 
question of profitability (be that money, fun, or social kudos).


Re: Minimize GC memory footprint

2021-01-31 Thread Max Haughton via Digitalmars-d-learn

On Saturday, 30 January 2021 at 16:42:35 UTC, frame wrote:
Is there a way to force the GC to re-use memory in already 
existing pools?


I set maxPoolSize:1 to gain pools that can be quicker released 
after there no longer in use. This already reduces memory usage 
to 1:3. Sadly the application creates multiple pools that are 
not necessary in my POV - just fragmented temporary slice data 
like from format(). What can I do to optimize?


I can't tell you much about the inner workings of the GC, but 
maybe take a look at experimental.allocator and see if anything 
there can help you (specifically that you understand your program 
better than the GC)


Re: Why many programmers don't like GC?

2021-01-15 Thread Max Haughton via Digitalmars-d-learn

On Friday, 15 January 2021 at 21:49:07 UTC, H. S. Teoh wrote:
On Fri, Jan 15, 2021 at 09:04:13PM +, welkam via 
Digitalmars-d-learn wrote:

[...]


As the joke goes, "you can write assembly code in any 
language". :-D  If you code in a sloppy way, it doesn't matter 
what language you write in, your program will still suck.  No 
amount of compiler magic will be able to help you.  The 
solution is not to blame this or that, it's to learn how to use 
what the language offers you effectively.




[...]


And with D, it's actually easy to do this, because D gives you 
tools like slices and by-value structs.  Having slices backed 
by the GC is actually a very powerful combination that people 
seem to overlook: it means you can freely refer to data by 
slicing the buffer.  Strings being slices, as opposed to 
null-terminated, is a big part of this.  In C, you cannot 
assume anything about how the memory of a buffer is managed 
(unless you allocated it yourself); as a result, in typical C 
code strcpy's, strdup's are everywhere.  Want a substring?  You 
can't null-terminate the parent string without affecting code 
that still depends on it; solution? strdup.  Want to store a 
string in some persistent data structure?  You can't be sure 
the pointer will still be valid (or that the contents pointed 
to won't change); solution? strdup, or strcpy.  Want to parse a 
string into words?  Either you modify it in-place (e.g. 
strtok), invalidating any other references to it, or you have 
to make new allocations of every segment.  GC or no GC, this 
will not lead to a good place, performance-wise.


I could not have written fastcsv if I had to work under the 
constraints of C's null-terminated strings under manual memory 
management.  Well, I *could*, but it would have taken 10x the 
amount of effort, and the API would be 5x uglier due to the 
memory management paraphrenalia required to do this correctly 
in C.  And to support lazy range-based iteration would require 
a whole new set of API's in C just for that purpose.  In D, I 
can simply take slices of the input -- eliminating a whole 
bunch of copying.  And backed by the GC -- so the code doesn't 
have to be cluttered with memory management paraphrenalia, but 
can have a simple, easy-to-use API compatible across a large 
range of use cases. Lazy iteration comes "for free", no need to 
introduce an entire new API. It's a win-win.


All that's really needed is for people to be willing to drop 
their C/C++/Java coding habits, and write D the way it's meant 
to be written: with preference for stack-allocated structs and 
by-value semantics, using class objects only for more 
persistent data. Use slices for maximum buffer reuse, avoid 
needless copying. Use compile-time introspection to generate 
code statically where possible instead of needlessly 
recomputing stuff at runtime.  Don't fear the GC; embrace it 
and use it to your advantage.  If it becomes a bottleneck, 
refactor that part of the code.  No need to rewrite the entire 
project the painful way; most of the time GC performance issues 
are localised and have relatively simple fixes.



T


I agree that the GC is useful, but it is a serious hindrance on 
the language not having an alternative other than really bad 
smart pointers (well written but hard to know their overhead) and 
malloc and free. I don't mind using the GC for my own stuff, but 
it's too difficult to avoid it at the moment for the times when 
it gets in the way.


I think the way forward is some robust move semantics and 
analysis like Rust. I suppose ideally we would have some kind of 
hidden ARC behind the scenes but I don't know how that would play 
with structs.


One more cynical argument for having a modern alternative is that 
it's a huge hindrance on the languages "cool"Ness in the next 
generation of programmers and awareness is everything (most 
people won't have heard of D)


Re: DConf talk : Exceptions will disappear in the future?

2021-01-05 Thread Max Haughton via Digitalmars-d-learn
On Tuesday, 5 January 2021 at 19:42:40 UTC, Ola Fosheim Grøstad 
wrote:

On Tuesday, 5 January 2021 at 18:23:25 UTC, sighoya wrote:
No error handling model was the HIT and will never be, 
therefore I would recommend to leave things as they are and to 
develop alternatives and not to replace existing ones.


Or implement C++ exceptions, so that D can catch C++ exceptions 
transparently (ldc catch clang++ exceptions and gdc catch g++ 
exceptions).


Walter already got quite a lot of the way there on that. There 
are some PRs on dmd about it but it's not in a state worth 
documenting yet if it's still there (the tests are still there so 
I assume it still works)


Re: Get the code of any D-entity as string?

2020-12-26 Thread Max Haughton via Digitalmars-d-learn

On Friday, 25 December 2020 at 21:25:40 UTC, sighoya wrote:

I've read a bit in Dlang traits.

It now has the ability to retrieve all method signatures of an 
overload set.

Big plus from me.

Is generally possible to get the declaration of a 
type/module/value as string in traits?


I didn't have any concrete use case for it, but it would 
essentially allow us to reflect over any code (we may have to 
respect privacy, of course).


On top of that, people could write their own token or ast 
frameworks as library solutions.


Further, most of the trait functionality given in the trait 
library could be simply implemented as a library solution.


I don't know D enough to be sure it isn't yet possible. 
Theoretically, we could read all files in a source folder, but 
I want also to read declarations from types outside my source 
folders, e.g. from those located in dyn libs.


Not possible although implementing as a __trait would be about 15 
lines I think.


I think read only AST access in some form would actually be quite 
nice, if not for dmd's AST being fuckugly (the hierarchy is fine 
but it's more gcc than clang)


Re: If statements and unused template parameters in Phobos documentation

2020-12-20 Thread Max Haughton via Digitalmars-d-learn

On Sunday, 20 December 2020 at 13:51:08 UTC, Rekel wrote:
I found a lot of the Phobos documentation to contain template 
arguments and if statements that made no sense to me, for 
example:


```
 uint readf(alias format, A...) (
 auto ref A args
)
if (isSomeString!(typeof(format)));

uint readf(A...) (
 scope const(char)[] format,
 auto ref A args
);
``` https://dlang.org/library/std/stdio/file.readf.html

From stdio.readf & stdio.File.readf. I'm assuming this is some 
kind of template, but often it seems there are more parameters 
in the first '()' part than are ever given. Am I missing 
something? Additionally, what is that if statement for? It 
precedes nothing.


The if is a template constraint.


Re: low-latency GC

2020-12-06 Thread Max Haughton via Digitalmars-d-learn
On Sunday, 6 December 2020 at 11:35:17 UTC, Ola Fosheim Grostad 
wrote:

On Sunday, 6 December 2020 at 11:27:39 UTC, Max Haughton wrote:

[...]


No, unique doesnt need indirection, neither does ARC, we put 
the ref count at a negative offset.


shared_ptr is a fat pointer with the ref count as a separate 
object to support existing C libraries, and make weak_ptr easy 
to implement. But no need for indirection.



[...]


I think you need a new IR, but it does not have to be used for 
code gen, it can point back to the ast nodes that represent ARC 
pointer assignments.


One could probably translate the one used in Rust, even.


https://gcc.godbolt.org/z/bnbMeY


Re: low-latency GC

2020-12-06 Thread Max Haughton via Digitalmars-d-learn
On Sunday, 6 December 2020 at 11:07:50 UTC, Ola Fosheim Grostad 
wrote:

On Sunday, 6 December 2020 at 10:44:39 UTC, Max Haughton wrote:
On Sunday, 6 December 2020 at 05:29:37 UTC, Ola Fosheim 
Grostad wrote:
It has to be either some kind of heavily customisable small GC 
(i.e. with our resources the GC cannot please everyone), or 
arc. The GC as it is just hurts the language.


Realistically, we probably need some kind of working group or 
at least serious discussion to really narrow down where to go 
in the future. The GC as it is now must go, we need borrowing 
to work with more than just pointers, etc.


The issue is that it can't just be done incrementally, it 
needs to be specified beforehand.


ARC can be done incrementally, we can do it as a library first 
and use a modified version existing GC for detecting failed 
borrows at runtime during testing.


But all libraries that use owning pointers need ownership to be 
made explicit.


A static borrow checker an ARC optimizer needs a high level IR 
though. A lot of work though.


ARC with a library will have overhead unless the compiler/ABI is 
changed e.g. unique_ptr in C++ has an indirection.


The AST effectively is a high-level IR. Not a good one, but good 
enough. The system Walter has built shows the means are there in 
the compiler already.


As things are at the moment, the annotations we have for pointers 
like scope go a long way, but the language doesn't deal with 
things like borrowing structs (and the contents of structs i.e. 
making a safe vector) properly yet. That is what needs thinking 
about.


Re: low-latency GC

2020-12-06 Thread Max Haughton via Digitalmars-d-learn
On Sunday, 6 December 2020 at 05:29:37 UTC, Ola Fosheim Grostad 
wrote:

On Sunday, 6 December 2020 at 05:16:26 UTC, Bruce Carneal wrote:
How difficult would it be to add a, selectable, low-latency GC 
to dlang?


Is it closer to "we cant get there from here" or "no big deal 
if you already have the low-latency GC in hand"?


I've heard Walter mention performance issues (write barriers 
IIRC).  I'm also interested in the GC-flavor performance trade 
offs but here I'm just asking about feasibility.


The only reasonable option for D is single threaded GC or ARC.


It has to be either some kind of heavily customisable small GC 
(i.e. with our resources the GC cannot please everyone), or arc. 
The GC as it is just hurts the language.


Realistically, we probably need some kind of working group or at 
least serious discussion to really narrow down where to go in the 
future. The GC as it is now must go, we need borrowing to work 
with more than just pointers, etc.


The issue is that it can't just be done incrementally, it needs 
to be specified beforehand.




Re: Doubts about the performance of array concatenation

2020-12-01 Thread Max Haughton via Digitalmars-d-learn

On Wednesday, 2 December 2020 at 00:08:55 UTC, ddcovery wrote:

On Tuesday, 1 December 2020 at 23:43:31 UTC, Max Haughton wrote:

On Tuesday, 1 December 2020 at 22:49:55 UTC, ddcovery wrote:
Yesterday I really shocked when, comparing one algorithm 
written in javascript and the equivalent in D, javascript 
performed better!!!


[...]


Use ldc, rdmd can invoke it for you. DMD's optimizer is not 
even close to as advanced as a modern JavaScript engine's - 
that's why we use it for fast build times instead of 
performance.


Impressive performance increase:

$ ldc2 -O -release  --run sorted.d
# D
1.0M: 810 ms
1.5M: 1324 ms
3.0M: 2783 ms
6.0M: 5727 ms

Thanks Max


I've written a github issue on your repository with flags to get 
the most out of LLVM.


Re: Dude about ~ array concatenation performance

2020-12-01 Thread Max Haughton via Digitalmars-d-learn

On Tuesday, 1 December 2020 at 22:49:55 UTC, ddcovery wrote:
Yesterday I really shocked when, comparing one algorithm 
written in javascript and the equivalent in D, javascript 
performed better!!!


[...]


Use ldc, rdmd can invoke it for you. DMD's optimizer is not even 
close to as advanced as a modern JavaScript engine's - that's why 
we use it for fast build times instead of performance.


Re: How to unit-test a phobos module?

2020-11-25 Thread Max Haughton via Digitalmars-d-learn

On Wednesday, 25 November 2020 at 21:36:36 UTC, Q. Schroll wrote:
On Wednesday, 25 November 2020 at 21:16:15 UTC, Steven 
Schveighoffer wrote:

I typically do:

make -f posix.mak std/.test

-Steve


For some reason, [1] says `make.exe` would be installed by the 
DMD installer, but I found none. It explicitly says not to use 
GNU make. (I'm on Windows.)


[1] https://wiki.dlang.org/Building_under_Windows


The digital mars make is actually distributed with the c compiler.

Developing on Windows is such a complete pain e.g. setting it up 
so it can find the linker and cl. For that reason I currently 
just do all dmd/phobos/druntime hacking inside WSL because that 
way I can just press build (dmd has build.d which is 
cross-platform but druntime and phobos both aren't fun to build 
on windows unless you've done it recently)


Re: How to construct a tree data structure with differently static nodes types

2020-11-22 Thread Max Haughton via Digitalmars-d-learn
On Monday, 23 November 2020 at 01:20:04 UTC, data pulverizer 
wrote:

Hi all,

I am trying to construct a tree data structure composed of 
differently (statically) typed nodes. The basic case is a 
binary tree. So you have a node like:


```
struct Node(T)
{
  T value;
  Node* next;
  Node* prev;
}

void main()
{
  auto x = Node!(int)(2);
  auto y = Node!(double)(3.2);
  x.next =  //gives error
}
```
Error: cannot implicitly convert expression & y of type 
Node!double* to Node!int*


So implicity Node!(T) will produce an object with prev, and 
next type Node!(T)*. But once I give them different types:


```
struct Node(T, P, N)
{
  T value;
  Node!(P...)* prev;
  Node!(N...)* next;
}
```

I can no longer specify the types at all, they become 
circularly referenced. Would appreciate the solution to this.


Many thanks.


If you want to keep things simple, use OOP (classes).

If you need to use structs, the "sumtype" may be just what you 
need (it's a bit more lightweight than std.algebraic in the 
standard library). If you want to implement this yourself then 
you need to write something called a tagged union.


Re: Request our suggestio: better way to insert data from Array!string[string] into a database table

2020-11-16 Thread Max Haughton via Digitalmars-d-learn

On Monday, 16 November 2020 at 17:44:08 UTC, Vino wrote:

Hi All,

  Request your suggestion, we have a program which call's an 
api, the output of the api is parsed using json parser and the 
result is stored in an array(Array!string[string] data), then 
these stored result are inserted into MySQL table, for 
inserting the data into the table we use the below code, this 
code is a small code which just contains 2 data items 
(Type,Hostname) and we have similar api's which  contains 15-20 
data items, hence request your suggestion on is there any 
better way than the below code, using the below logic the 
foreach line is will run into multiple lines eg:


[...]


What are you looking to improve? Do you want to make the code 
prettier or faster?


It doesn't look too bad to my eye although my personal style 
would be to unpack t and h inside the foreach loop.


Re: presence of function template prevents diagnostic

2020-11-16 Thread Max Haughton via Digitalmars-d-learn
On Monday, 16 November 2020 at 17:03:32 UTC, Steven Schveighoffer 
wrote:

On 11/14/20 5:44 PM, kdevel wrote:


$ dmd -version=X -i foo
$ ./foo
void A.bar(int s)

Is the latter behavior intended or a bug?


That seems like a bug. It shouldn't be less ambiguous because 
you *added* an overload that can also handle it...


-Steve


It could be technically kosher because of template lookup but I'm 
not sure if the behaviour is actually specified in the standard. 
Probably should be a bug.


Re: Why was new(size_t s) { } deprecated in favor of an external allocator?

2020-10-15 Thread Max Haughton via Digitalmars-d-learn
On Thursday, 15 October 2020 at 06:39:00 UTC, Patrick Schluter 
wrote:
On Wednesday, 14 October 2020 at 20:32:51 UTC, Max Haughton 
wrote:

On Wednesday, 14 October 2020 at 20:27:10 UTC, Jack wrote:

What was the reasoning behind this decision?


Andrei's std::allocator talk from a few years ago at cppcon 
covers this (amongst other things)


Yes, and what did he say?
You seriously don't expect people to search for a random talk 
from a random event from a random year?


It's the first result when you search for "Andrei std::allocator"


Re: Why was new(size_t s) { } deprecated in favor of an external allocator?

2020-10-14 Thread Max Haughton via Digitalmars-d-learn

On Wednesday, 14 October 2020 at 20:27:10 UTC, Jack wrote:

What was the reasoning behind this decision?


Andrei's std::allocator talk from a few years ago at cppcon 
covers this (amongst other things)


Re: Range format specifiers in other languages?

2020-10-11 Thread Max Haughton via Digitalmars-d-learn

On Sunday, 11 October 2020 at 23:57:31 UTC, Ali Çehreli wrote:

I find D's %( and %) range format specifiers very useful:

import std.stdio;
import std.range;

void main() {
  5.iota.writefln!"%(%s, %)";  // Prints 0, 1, 2, 3, 4
}

Are there similar features in other languages?

Thank you,
Ali


I think rust can do something similar with struct pretty 
printing. The syntax has curly braces in it but I can't recall it 
right now.


Possibly worth showing off (especially given that some people at 
first don't even know the templated format string exists)


Re: Taking arguments by value or by reference

2020-10-04 Thread Max Haughton via Digitalmars-d-learn

On Sunday, 4 October 2020 at 14:26:43 UTC, Anonymouse wrote:

On Saturday, 3 October 2020 at 23:47:32 UTC, Max Haughton wrote:
The guiding principle to your function parameters should be 
correctness - if I am passing a big struct around, if I want 
to take ownership of it I probably want to take it by value 
but if I want to modify it I should take it by reference (or 
by pointer but don't overcomplicate, notice in the previous 
example they lower to the same thing). If I just want to look 
at it, it should be taken by const ref if possible (D const 
isn't the same as C++ const, this may catch you out).


Const-correctness is a rule to live by especially with an big 
unwieldy struct.


I would avoid the new in for now, but I would go with const 
ref from what you've described so far.


I mostly really only want a read-only view of the struct, and 
whether a copy was done or not is academic. However, profiling 
showed (what I interpret as) a lot of copying being done in 
release builds specifically.


https://i.imgur.com/JJzh4Zc.jpg

Naturally a situation where I need ref I'd use ref, and in the 
rare cases where it actually helps to have a mutable copy 
directly I take it mutable. But if I understand what you're 
saying, and ignoring --preview=in, you'd recommend I use const 
ref where I would otherwise use const?


Is there some criteria I can go by when making this decision, 
or does it always reduce to looking at the disassembly?


This is skill you only really hone with experience, but it's not 
too bad once you're used to it.


For a big struct, I would just stick to expressing what you want 
it to *do* rather than how you want it to perform. If you want to 
take ownership you basically have to take by value, but if you 
(as you said) want a read only view definitely const ref. If I 
was reading your code, ref immediately tells me not to think 
about ownership and const ref immediately tells me you just want 
to look at the goods.


One thing I haven't mentioned so far is that not all types have 
non-trivial semantics when it comes to passing them around by 
value, so if you are writing generic code it is often best to 
avoid these.


Re: Taking arguments by value or by reference

2020-10-03 Thread Max Haughton via Digitalmars-d-learn

On Saturday, 3 October 2020 at 23:00:46 UTC, Anonymouse wrote:
I'm passing structs around (collections of strings) whose 
.sizeof returns 432.


The readme for 2.094.0 includes the following:

This release reworks the meaning of in to properly support all 
those use cases. in parameters will now be passed by reference 
when optimal, [...]


* Otherwise, if the type's size requires it, it will be passed 
by reference.
Currently, types which are over twice the machine word size 
will be passed by
reference, however this is controlled by the backend and can 
be changed based

on the platform's ABI.


However, I asked in #d a while ago and was told to always pass 
by value until it breaks, and only then resort to ref.


[18:32:16]  at what point should I start passing my 
structs by ref rather than by value? some are nested in 
others, so sizeofs range between 120 and 620UL

[18:33:43]  when you start getting stack overflows
[18:39:09]  so if I don't need ref for the references, 
there's no inherent merit to it unless I get in trouble 
without it?

[18:39:20]  pretty much
[18:40:16]  in many cases the copying is merely 
theoretical and doesn't actually happen when optimized


I've so far just been using const parameters. What should I be 
using?


Firstly, the new in semantics are very new and possibly subtly 
broken (take a look at the current thread in general).


Secondly, as to the more specific question of how to pass a big 
struct around it may be helpful to look at this quick godbolt 
example (https://d.godbolt.org/z/nPvTWz). Pay attention to the 
instructions writing to stack memory (or not). A struct that big 
will be passed around on the stack, whether it gets copied or not 
depends on the semantics of the struct etc.


The guiding principle to your function parameters should be 
correctness - if I am passing a big struct around, if I want to 
take ownership of it I probably want to take it by value but if I 
want to modify it I should take it by reference (or by pointer 
but don't overcomplicate, notice in the previous example they 
lower to the same thing). If I just want to look at it, it should 
be taken by const ref if possible (D const isn't the same as C++ 
const, this may catch you out).


Const-correctness is a rule to live by especially with an big 
unwieldy struct.


I would avoid the new in for now, but I would go with const ref 
from what you've described so far.




Re: Question about publishing a useful function I have written

2020-07-14 Thread Max Haughton via Digitalmars-d-learn

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.





Re: Passing iterators into functions

2020-06-24 Thread Max Haughton via Digitalmars-d-learn

On Thursday, 25 June 2020 at 03:35:00 UTC, repr-man wrote:

I have the code:

int[5] a = [0, 1, 2, 3, 4];
int[5] b = [5, 6, 7, 8, 9];
auto x = chain(a[], b[]).chunks(5);
writeln(x);

It produces a range of slices as is expected: [[0, 1, 2, 3, 4], 
[5, 6, 7, 8, 9]]


However, when I define a function as follows and pass in the 
result of the chain iterator:


auto func(R)(R r, size_t width)
if(isRandomAccessRange!R)
{
return r.chunks(width);
}

void main()
{
int[5] a = [0, 1, 2, 3, 4];
int[5] b = [5, 6, 7, 8, 9];
auto x = func!(int[])(chain(a[], b[]), 5);
writeln(x);
}

It gives me an error along the lines of:
Error: func!(int[]).func(int[] r, ulong width) is not callable 
using argument types (Result, int)
   cannot pass argument chain(a[], b[]) of type Result to 
parameter int[] r


I was hoping it would return the same result as the first 
program.


This seems to have to do with the fact that all iterators 
return their own unique type.  Could someone help me understand 
the reason behind this design and how to remedy my situation?


Chain returns a range not an int[]. You need to either convert 
the range to an array via .array or allow the compiler to infer 
the type of the parameter of func (You'll need to import 
std.range to have the range interface available)


mhh


Re: dmd memory usage

2019-11-18 Thread Max Haughton via Digitalmars-d-learn
On Monday, 18 November 2019 at 00:20:12 UTC, Steven Schveighoffer 
wrote:
I'm fighting some out of memory problems using DMD and some 
super-template heavy code.


I have ideas on how to improve the situation, but it involves 
redesigning a large portion of the design. I want to do it 
incrementally, but I need to see things improving.


Is there a straightforward way to figure out how much memory 
the compiler uses during compilation? I though maybe 
/usr/bin/time, but I feel like I don't trust the output to be 
the true max resident size to be what I'm looking for (or that 
it's 100% accurate). Is there a sure-fire way to have DMD print 
it's footprint?


-Steve


Massif is good for this. ms_print will give you a graphical 
summary, and the data is human and machine readable.


The only setback being that massif can make the execution slower 
however I can't give exact numbers but it can be terrible.


Re: Execute certain Tests?

2019-11-02 Thread Max Haughton via Digitalmars-d-learn

On Saturday, 2 November 2019 at 17:27:14 UTC, Martin Brezel wrote:

Is there a trick to execute only the test, defined in one file?
Or the Tests of a certain Module?

Or in general: How to avoid to execute all the tests, when 
running "dub test"?

It doesn't has to be dub, though.


Not by default, if you know the language well then you can use 
UDAs to write a fancy test runner in ten minutes, if not then use 
Attila's unit-threaded (or similar)



Max


Re: On D's garbage collection

2019-10-08 Thread Max Haughton via Digitalmars-d-learn

On Tuesday, 8 October 2019 at 16:28:51 UTC, Marcel wrote:
I'm been thinking about using D in conjunction with C11 to 
develop a set of applications with hard real-time requirements. 
While initially the goal was to use C++ instead, it has become 
clear that D's introspection facilities will offer significant 
advantages. Unfortunately, the project will heavily rely on 
custom memory allocators written in C, so the presence of 
garbage collection in the language is a problem. While I'm 
aware that the nogc attribute exists, I haven't actually seen a 
way to apply it to a whole project. Is this possible?


Do you want to write D code that just doesn't use the GC or the 
whole runtime?


If the former then use @nogc at the entry point of your D code 
(This means that - say - main cannot call anything non-@nogc and 
therefore guarantees the program is @nogc), if the latter then 
use -betterC


IMO, if the interface to your memory allocators is stable then 
just link with them and write the whole thing in D (Interfacing 
with C is a solved problem but C is just awful compared to the 
features you get for free in D)


Re: how to definition a non-const pointer that point a const var.

2019-08-24 Thread Max Haughton via Digitalmars-d-learn
On Saturday, 24 August 2019 at 05:03:43 UTC, Jonathan M Davis 
wrote:
On Friday, August 23, 2019 10:14:56 PM MDT lili via 
Digitalmars-d-learn wrote:

Hi:
   In C we can definition const int *ncp_to_cv;
or int * const cp_to_ncv;
   How to do this in D.


D uses parens to restrict how much of the type is const.

const int* - const pointer to const int
const(int*) - const pointer to const int
const(int)* - mutable pointer to const int

Similarly,

const(int*)* - mutable pointer to const pointer to const int
const(int)** - mutable pointer to mutable pointer to const int

D's const is transitive, so it's not possible to have a const 
pointer to a mutable type.


- Jonathan M Davis


As to const pointers to mutable types, it can be done in a 
library (Final in std.typecons). I don't know what the overhead 
is but I imagine it wraps it in a struct




Re: How should I sort a doubly linked list the D way?

2019-08-13 Thread Max Haughton via Digitalmars-d-learn

On Tuesday, 13 August 2019 at 18:54:58 UTC, H. S. Teoh wrote:
On Tue, Aug 13, 2019 at 11:28:35AM -0700, Ali Çehreli via 
Digitalmars-d-learn wrote: [...]
Summary: Ditch the linked list and put the elements into an 
array. :)

[...]

+1.  The linked list may have been faster 20 years ago, before 
the advent of modern CPUs with caching hierarchies and memory 
access predictors.


These days, with CPU multi-level caches and memory access 
predictors, in-place arrays are often the best option for 
performance, up to a certain size.  In general, avoid 
indirection where possible, in order to avoid defeating the 
memory access predictor and reduce the number of cache misses.



T


I saw a Bjarne Stroustrup talk where he benchmarked that the for 
n > 1, std::vector was a lot faster than a linked list for all 
supported operations. I don't know how clever the caching 
strategies are on a modern processor (Pointer chasing), but to my 
knowledge the only way of getting a cache efficient linked list 
would be to effectively have a very contiguous allocator (Which 
obviously defeats the purpose of using a list in the first place)


Found it: https://www.youtube.com/watch?v=YQs6IC-vgmo


Re: Desktop app with vibe.d

2019-08-12 Thread Max Haughton via Digitalmars-d-learn

On Monday, 12 August 2019 at 10:41:57 UTC, GreatSam4sure wrote:
Pls I want to know if it is possible to build desktop app with 
vibe.d just like nodejs. I am not satisfy with the GUI of Dlang 
such as dlangui and gtkd. I don't think they have good styling 
capabilities like HTML and CSS.


I will be happy if I can build an app in D with fanciful ui. I 
will also be happy if you know any other way to build a 
fanciful ui in D like adobe flex, javafx, etc.


vibe.d is a backend framework so unless you ran it and had the 
user connect via a web browser I don't think so.


Honestly, avoid web technology on the desktop like the plague. 
It's slow and memory hogging.


Re: Question about ubyte x overflow, any safe way?

2019-08-04 Thread Max Haughton via Digitalmars-d-learn

On Sunday, 4 August 2019 at 18:22:30 UTC, matheus wrote:

On Sunday, 4 August 2019 at 18:15:30 UTC, Max Haughton wrote:
What do you want to do? If you just want to count to 255 then 
use a foreach


This was just an example, what I'd like in this code is either: 
Get an error (exception) when overflow or even an warning (Only 
if "some" flag was active).


If you want to prevent overflow you must either use BigInt or 
wrap ubyte in a struct that doesn't allow overflow


Could you please elaborate about this struct wrapping? Do you 
mean manually check on change?


Matheus.


Std.experimental.checkedint maybe exactly what you are looking for




Re: Question about ubyte x overflow, any safe way?

2019-08-04 Thread Max Haughton via Digitalmars-d-learn

On Sunday, 4 August 2019 at 18:12:48 UTC, matheus wrote:

Hi,

The snippet below will produce an "infinite loop" because 
obviously "ubyte u" will overflow after 255:


import std.stdio;
void main(){
ubyte u = 250;
for(;u<256;++u){
writeln(u);
}
}

Question: Is there a way (Flag) to prevent this?

Matheus.


What do you want to do? If you just want to count to 255 then use 
a foreach



If you want to prevent overflow you must either use BigInt or 
wrap ubyte in a struct that doesn't allow overflow


Re: Calling / running / executing .d script from another .d script

2019-07-28 Thread Max Haughton via Digitalmars-d-learn

On Sunday, 28 July 2019 at 12:56:12 UTC, BoQsc wrote:
Right now, I'm thinking what is correct way to run another .d 
script from a .d script. Do you have any suggestions?


You'd need to bring a compiler with you and then build it into a 
shared library (then dlopen it).


To do this you'd need a clear API defined in the file to be 
compiled, so you can call it. If you want to use druntime in said 
file you need to use loadLibrary in core instead of dlopen


Re: Is betterC affect to compile time?

2019-07-26 Thread Max Haughton via Digitalmars-d-learn

On Thursday, 25 July 2019 at 14:20:03 UTC, Ali Çehreli wrote:

On 07/25/2019 05:46 AM, Oleg B wrote:
On Thursday, 25 July 2019 at 12:34:15 UTC, rikki cattermole 
wrote:

Those restrictions don't stop at runtime.


It's vary sad.

What reason for such restrictions? It's fundamental idea or 
temporary implementation?


It looks like a bug to me.

Ali


If the spec is to be believed then it is.

I filed a bugzilla, https://issues.dlang.org/show_bug.cgi?id=20086


Re: Is there a way to bypass the file and line into D assert function ?

2019-07-19 Thread Max Haughton via Digitalmars-d-learn

On Friday, 19 July 2019 at 15:30:25 UTC, Newbie2019 wrote:

for example:

void ASSERT(string fmt, string file = __FILE_FULL_PATH__, 
size_t line = __LINE__, T...) (bool c, scope T a)  @nogc {

   assert(c, string, file, line);
}

but i get this error:

error.d(39): Error: found file when expecting )
error.d(39): Error: found ) when expecting ; following statement
error.d(39): Deprecation: use { } for an empty statement, not ;

I want d to print the error message with some format 
information, and show the right file and line for the original 
location.


Is it doable ?


Isn't assert a template (file and line) rather than a plain 
function call?


Worst comes to worst, you can provide your own _d_assert(?) and 
override object.d then just call the C assert


Re: How to use std.windows.registry, there are no documentations.

2019-07-11 Thread Max Haughton via Digitalmars-d-learn

On Thursday, 11 July 2019 at 08:53:35 UTC, BoQsc wrote:

https://dlang.org/phobos/std_windows_registry.html
https://github.com/dlang/phobos/blob/master/std/windows/registry.d

Can someone provide some examples on how to:
set, change, receive something from the Windows registry using 
Phobos std.windows.registry library?


I didn't know that existed, but the source code is documented but 
not in the style that the doc generated recognizes as far as I 
can tell.


Re: Memory allocation failed in CT

2019-07-09 Thread Max Haughton via Digitalmars-d-learn
Is this a 64 or 32 bit compiler? Also could you post the source 
code if possible?


You could try "--DRT-gcopt=profile:1" druntime flag to see if the 
compiler is running out of memory for real


Re: Memory allocation failed in CT

2019-07-09 Thread Max Haughton via Digitalmars-d-learn

On Tuesday, 9 July 2019 at 17:48:52 UTC, Andrey wrote:

Hello,
I have got a problem with compile-time calulations.
I have some code generator that should create some long string 
of code during CT and after generation I mixin it. If I run it 
normally - in run time - then there is no error and I get 
expected output - string with size ~ 3.5 MB.

If I run it in CT then I recieve an error:

[...]


I don't understand why...
The only operation in my generator is string concatination: 
_result ~= "some code...".


Are you using the -lowmem flag? This enables the GC during 
compilation i.e. you might be running out of memory (CTFE is not 
efficient with memory during evaluation)


Re: Why are immutable array literals heap allocated?

2019-07-05 Thread Max Haughton via Digitalmars-d-learn

On Friday, 5 July 2019 at 16:25:10 UTC, Nick Treleaven wrote:

On Thursday, 4 July 2019 at 11:06:36 UTC, Eugene Wissner wrote:

static immutable arr = [1, 2];

You have to spell it out that the data is static.


Yes, I was wondering why the compiler doesn't statically 
allocate it automatically as an optimization.


LDC might be able to optimize it away but by default its heap 
allocated, I imagine for thread safety


Re: Transform a function's body into a string for mixing in

2019-06-20 Thread Max Haughton via Digitalmars-d-learn

On Thursday, 20 June 2019 at 19:09:11 UTC, Emmanuelle wrote:

Hello!

Is there any trait or Phobos function for transforming a 
function/delegate/lambda/whatever's body into a string suitable 
for `mixin(...)`? For example:


---
__traits(getBody, (int a, int b) => a + b); // returns "(int a, 
int b) => a + b"
// or maybe just "a 
+ b"

---

If not, is there any way to do this _without_ using strings? 
They are very inconvenient and could hide errors.


Thanks!


We don't have anything AST-macro ish or a trait as described.

The trait isn't impossible to implement but I could imagine it 
being a nightmare for compile times


Re: DIP 1016 and const ref parameters

2019-06-19 Thread Max Haughton via Digitalmars-d-learn
On Wednesday, 19 June 2019 at 19:25:59 UTC, Jonathan M Davis 
wrote:
On Wednesday, June 19, 2019 12:28:12 PM MDT XavierAP via 
Digitalmars-d-learn wrote:

[...]


The DIPs are here: https://github.com/dlang/DIPs

[...]


DIP1014 has not been implemented in DMD or druntime yet, AFAIK


Re: How to compile my DMD fork?

2019-06-14 Thread Max Haughton via Digitalmars-d-learn

On Friday, 14 June 2019 at 18:07:11 UTC, Q. Schroll wrote:
Basically the headline. I want to try to implement my DIP. I've 
already forked DMD from GitHub. Now, what would I have to do in 
order to get a D compiler with my changes?


I have Windows on x86-64 and Visual Studio on my machine.


It might just be a quirk of my install (but It happened recently 
enough ago that it's worth mentioning) but if you get a really 
cryptic error message about some msbuild dll being missing you 
need to find the configuration option and manually set it to the 
dll actually present.


Re: Does slicing have an effect?

2019-05-25 Thread Max Haughton via Digitalmars-d-learn

On Tuesday, 21 May 2019 at 20:31:58 UTC, Dennis wrote:
I was replacing a memcpy with a slice assignment and 
accidentally used == instead of =.

Usually the compiler protects me from mistakes like that:

```
int[4] a;
a == a;
```
Error: a == a has no effect

However, because I was using slices it didn't:
```
int[4] a;
a[] == a[];
```
No errors

Does slicing have an effect I'm not aware of, or is this a bug?


It calls druntime( https://d.godbolt.org/z/3gS3-E ), so 
technically it does have an effect, even if that effect is 
completely unused and therefore optimized away. Whether this 
should be considered an effect or not is up to you


Re: dmd -nodefaultlibs?

2019-05-21 Thread Max Haughton via Digitalmars-d-learn

On Tuesday, 21 May 2019 at 21:48:22 UTC, Jonathan Marler wrote:
Is there a way to prevent dmd from adding any default libraries 
to its linker command?


Something equivalent to "-nodefaultlibs" from gcc? 
https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html


I'd still like to use the dmd.conf file, so I don't want to use 
"-conf="


-betterC ?


Re: DIP1000: Should this compile

2019-05-16 Thread Max Haughton via Digitalmars-d-learn
On Thursday, 16 May 2019 at 21:56:52 UTC, Steven Schveighoffer 
wrote:

On 5/16/19 10:21 PM, Max Haughton wrote:

https://run.dlang.io/is/cKFsXh

Should this compile, or is return scope T* down to the user to 
not escape (Returning  directly does not compile)




Answer to subject: no. This is a bug. Please file.

Not sure what the solution is, because dip1000 makes scope a 
storage class. So there's no way to tag what the input 
parameter points at.


-Steve


The parameter pointer outlives the the , i.e. cannot be 
guaranteed that it doesn't escape, which is sufficient grounds to 
not allow the assignment. Hopefully, this is a implementation 
rather specification error (If my understanding of the DIP is 
correct)




DIP1000: Should this compile

2019-05-16 Thread Max Haughton via Digitalmars-d-learn

https://run.dlang.io/is/cKFsXh

Should this compile, or is return scope T* down to the user to 
not escape (Returning  directly does not compile)