Re: Copy a struct and its context
On Sunday, 11 September 2016 at 05:44:13 UTC, Yuxuan Shui wrote: I recently noticed nested struct capture its context by reference (which, BTW, is not mentioned at all here: https://dlang.org/spec/struct.html#nested). And bliting a struct obviously doesn't do a deep copy of its context. So my question is, is there a way to deep copy the context of a struct? I've tried a few things, but I don't think you can. The root issue is that the context pointer is void*, so you can't do meaningful reflection on it.
Confusion over what types have value vs reference semantics
Hi, Are there universal rules that I can apply to determine what types have reference or value semantics? I know that for the basic primitive C types (int, bool, etc) has value semantics. In particular, I'm still trying to understand stack vs GC-managed arrays, and slices. Finally, I have an associative array, and I want to pass it into a function. The function is only reading data. Would putting ref on in function parameter pass it by reference?
Re: Confusion over what types have value vs reference semantics
On 12/09/2016 3:15 AM, Neurone wrote: Hi, Are there universal rules that I can apply to determine what types have reference or value semantics? I know that for the basic primitive C types (int, bool, etc) has value semantics. In particular, I'm still trying to understand stack vs GC-managed arrays, and slices. Finally, I have an associative array, and I want to pass it into a function. The function is only reading data. Would putting ref on in function parameter pass it by reference? Ok two questions here: 1) What constitutes value vs reference passing 2) Allocation location So basically you have two "locations" where something can be stored, on the stack or the heap. Now I put it in quotes because they are both in RAM somewhere so it doesn't matter too much. The only difference is only a subset of the stack is readily allocatable at any single function call. So in regarding to what gets passed by value, well that is simple. If it is a class, you're passing a few pointers as your reference. All primal types including pointers are passed as values. If you use ref, you turn a type into a pointer auto magically (a location in the stack most likely). Just so you're aware, arrays are slices in D (excluding static arrays). They are simply a pointer + length. So I suppose you can think of references and slices as a container of sorts for other values which get passed in.
Re: Confusion over what types have value vs reference semantics
On Sunday, 11 September 2016 at 15:15:09 UTC, Neurone wrote: Hi, Are there universal rules that I can apply to determine what types have reference or value semantics? I know that for the basic primitive C types (int, bool, etc) has value semantics. Primitive types (int, bool, etc) and structs are passed by value unless a function parameter is annotated with 'ref'. Classes are reference types, so given instance foo of class Foo, foo itself is a reference. For arrays, it's easiest to think of one as a struct with two fields: length and ptr. The ptr field points to the memory where the array data is stored. When you pass an array to a function, the length & ptr are passed by value (it's a "slice"). That means that modifying the length of the array by adding or removing elements or attempting to change where ptr is pointing will only modify the local copy. You can modify the array elements in the original array (manipulate their fields, overwrite them, and such), but you can't modify the structure (length or pointer) of the original array unless the parameter is annotated with ref. Although AAs don't have a length or ptr, they work similarly: you can modify the contents without ref, but can only modify the structure with. In particular, I'm still trying to understand stack vs GC-managed arrays, and slices. Steven's article on slices should help [1]. It also helps if you just think of all dynamic arrays as slices. int[] foo; // slice with length & ptr, no memory allocated for elements int[3] bar; // static array with length & ptr, three ints allocated on the star The memory for foo needs to be allocated somewhere. It might be the GC-managed heap, it might be malloc, it might even be stack memory. Doesn't matter. You cannot modify the length of bar, but you can slice it: auto barSlice = bar[]; And here, no memory is allocated. barSlice.ptr is the same as bar.ptr and barSlice.length is the same as bar.length. However, if you append a new element: barSlice ~= 10; The GC will allocate memory for a new array and barSlice will no longer point to bar. It will now have four elements. Finally, I have an associative array, and I want to pass it into a function. The function is only reading data. Would putting ref on in function parameter pass it by reference? You can easily test this: ``` void addElem(int[string] aa, string key, int val) { aa[key] = val; } void main() { int[string] map; map.addElem("key", 10); import std.stdio : writeln; writeln("The aa was modified: ", ("key" in map) != null); } ``` AAs behave like arrays. The meta data (like the array length and ptr) is passed by value, the data by reference. The above should print false. Add ref to the function parameter, it will print true. However, if a key already exists in the aa, then you can modify it without ref as it isn't changing the structure of the aa. The following will print 10 whether the aa parameter is ref or not. ``` void addElem(int[string] aa, string key, int val) { aa[key] = val; } void main() { int[string] map; map["key"] = 5; map.addElem("key", 10); import std.stdio : writeln; writeln("The value of key is ", map["key"]); } ``` [1] https://dlang.org/d-array-article.html
Re: Confusion over what types have value vs reference semantics
On Sunday, 11 September 2016 at 16:10:04 UTC, Mike Parker wrote: And here, no memory is allocated. barSlice.ptr is the same as bar.ptr and barSlice.length is the same as bar.length. However, if you append a new element: barSlice ~= 10; The GC will allocate memory for a new array and barSlice will no longer point to bar. It will now have four elements. I should clarify that this holds true for all slices, not just slices of static arrays. The key point is that appending to a slice will only allocate if the the .capacity property of the slice is 0. Slices of static arrays will always have a capacity of 0. Slices of slices might not, i.e. there may be room in the memory block for more elements.
dub - load shared library (so) in example
I make a packet for dub in Linux. In a packet there is libQt.so which is loaded by an example of D. Usually I execute LD_LIBRARY_PATH='pwd'; export LD_LIBRARY_PATH, but now it is necessary to make it in dub. How to make it?
Error while compiling and linking modules (mysql)
Hi! I'm using a mysql wrapper (i don't even know how to use it yet) that i got from github: https://github.com/adamdruppe/arsd When i try to compile the code i get an error message: Error: module mysql is in file 'mysql.d' which cannot be read This is the folder structure i have: /home/test/main.d /home/test/arsd/mysql.d /home/test/arsd/database.d And the compile command: ldc main.d mysql.d database.d -I/home/test/arsd/ The main file contains a few lines just to test: module compiling_test; import arsd.mysql; import arsd.database; int main(string[] args) { return 0; }
Re: Error while compiling and linking modules (mysql)
On Sunday, 11 September 2016 at 21:43:12 UTC, Geert wrote: Hi! I'm using a mysql wrapper (i don't even know how to use it yet) that i got from github: https://github.com/adamdruppe/arsd When i try to compile the code i get an error message: Error: module mysql is in file 'mysql.d' which cannot be read This is the folder structure i have: /home/test/main.d /home/test/arsd/mysql.d /home/test/arsd/database.d And the compile command: ldc main.d mysql.d database.d -I/home/test/arsd/ The main file contains a few lines just to test: module compiling_test; import arsd.mysql; import arsd.database; int main(string[] args) { return 0; } I was passing the wrong paths. Te correct compile command is: ldc main.d arsd/mysql.d arsd/database.d -I/home/marduk/Proyectos/gtkd/sql/arsd
Compiling vibe.d application for Amazon ec2 instance
Hi all! I made vibe-d application, and client give me already taken hosting for it on Amazon aws ec2, uname -a: Linux ip-xxx-xx-xx-xx 4.4.11-23.53.amzn1.x86_64 #1 SMP Wed Jun 1 22:22:50 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux Compiled on my Ubuntu binary don't run because of different versions of libraries (on Amazon they older). I can try to install dmd to ec2 instance but don't think this is good idea. I want to compile binary on my developing machine (may be in specific OS in VirtualBox). How to resolve this problem by right way? Can I get VirtualBox image of ec2 version of Linux and use it on my machine? Can I compile all libraries used as static and make more independent binary? Something else?
iasm, unexpectedly slower than DMD production
I have this function, written in iasm: ° T foo(T)(T x, T c) if (is(T == float) || is(T == double)) { version(none) { return x*x*x - x*x*c + x*c; } else asm { naked; movsd XMM3, XMM1; mulsd XMM0, XMM1; mulsd XMM1, XMM1; movsd XMM2, XMM1; mulsd XMM1, XMM3; addsd XMM1, XMM0; mulsd XMM0, XMM3; subsd XMM1, XMM0; movsd XMM0, XMM1; ret; } } void main() { // compile with -O -release import std.datetime, std.stdio; benchmark!({auto a = foo(0.2,0.2);})(1_000)[0].writeln; } ° The DMD production for the non-iasm version is ;--- SUB 004A7BD8h --- 004A7BD8h sub rsp, 18h 004A7BDCh movsd xmm5, xmm0 004A7BE0h movsd xmm4, xmm1 004A7BE4h movsd qword ptr [rsp], xmm1 004A7BE9h movsd xmm0, qword ptr [rsp] 004A7BEEh mulsd xmm0, xmm5 004A7BF2h mulsd xmm1, xmm4 004A7BF6h mulsd xmm1, xmm4 004A7BFAh movsd xmm2, xmm4 004A7BFFh mulsd xmm2, xmm4 004A7C03h mulsd xmm2, xmm5 004A7C07h subsd xmm1, xmm2 004A7C0Bh addsd xmm0, xmm1 004A7C0Fh add rsp, 18h 004A7C13h ret ;- When I change the version(none) to version(all), the benchmark is **7X** faster (e.g 410 against 3000 for the iasm version). This difference doesn't look normal at all. Does anyone know why ? The usage of the stack to move xmm1 in xmm0 is particularly strange...
Re: Compiling vibe.d application for Amazon ec2 instance
On Sunday, September 11, 2016 23:12:15 crimaniak via Digitalmars-d-learn wrote: > Hi all! > > I made vibe-d application, and client give me already taken > hosting for it on Amazon aws ec2, uname -a: > Linux ip-xxx-xx-xx-xx 4.4.11-23.53.amzn1.x86_64 #1 SMP Wed Jun 1 > 22:22:50 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux > > Compiled on my Ubuntu binary don't run because of different > versions of libraries (on Amazon they older). > > I can try to install dmd to ec2 instance but don't think this is > good idea. I want to compile binary on my developing machine (may > be in specific OS in VirtualBox). > How to resolve this problem by right way? > > Can I get VirtualBox image of ec2 version of Linux and use it on > my machine? > Can I compile all libraries used as static and make more > independent binary? > Something else? I've never used EC2, so I don't know what it would take to be able to build in the same environment locally (though I would certainly think that there would be a way do to so, and I would assume that Amazon's documentation talks about it somewhere). However, you can try just grabbing the .zip file for dmd and putting that on your EC2 instance and using that to build your program there - especially if you're in a hurry. You can also try fully statically linking, but it's a bit of a pain, since last time I checked it doesn't work right if you let dmd do the linking command, which means that you have to use it to generate the object files and then manually link them using gcc. https://issues.dlang.org/show_bug.cgi?id=6952 - Jonathan M Davis
Mysql-native with LAMPP
Hi all! I tried the client driver for MySQL/MariaDB "mysql-native". https://github.com/mysql-d/mysql-native Everything works well with an individually installed version of MySql. But I would like to know if there is a way to make D programms work with LAMPP/XAMPP. I'm getting this error message while executing the program: host=localhost;port=3306;user=root;pwd=MY_testPa550;db=testdb Failed: std.socket.SocketOSException@/build/ldc/src/ldc/runtime/phobos/std/socket.d(2822): Unable to connect socket: Connection refused
Re: cloning arrays
On Sat, 2016-09-10 at 15:54 -0700, Jonathan M Davis via Digitalmars-d- learn wrote: > […] > > dup creates an array with mutable elements that are copies of what > was in > the original array. How deep a copy that is depends on the type. It > does > exactly as deep a copy as simply copying the element would do. e.g. > > […] Which raises the question of whether: x[] and x.dup have the same result. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part