Testing Return Value Optimization (RVO)
Hello, Sorry if this question is a bit naive or shows a misunderstanding of RVO. I was trying to see if my C compiler was doing RVO with struct, but after testing it at is apparently not the case. Since I have heard that D supports RVO I wanted to give it a try in D. But apparently it doesn't do RVO as I expected it would do it. Here is the D code. It is very similar to the C code I tested with gcc and clang : --- #!/usr/bin/rdmd -O import std.stdio; struct S { int a, b; }; S foo(S* p) { S v = {1, 2}; writeln("foo: return value optimization: ", p == ); return v; } void main() { S x; x = foo(); } --- My assumption is the following. x is the target variable where to store the result of foo. I expected that with RVO foo() would receive the address where to store its result as hidden argument. Inside foo(), the optimizer would detect that v, its returned value, will be stored at the location given as hidden argument. It may then optimize out v so that foo() uses x as storage for v. My code is testing if this is the case and it is apparently not. Can someone please explain me why this doesn't work as I would expect RVO to work ?
Re: Purity of std.conv.to!string
On 27/09/2015 3:14 AM, cym13 wrote: On Saturday, 26 September 2015 at 17:08:00 UTC, Nordlöw wrote: Why is the following code not pure: float x = 3.14; import std.conv : to; auto y = x.to!string; ??? Is there a reason for it not being pure? If not, this is a serious problem as this is such a fundamental function. Probably because it uses C's float formatting functions, and they probably set errno and therefore aren't pure. Maybe because of floating point numbers uncertainty that would cause the resulting string to be different for two equivalent inputs? I can't seem to put an example together though. No, it doesn't work like that.
Re: Testing Return Value Optimization (RVO)
On Sunday, 27 September 2015 at 13:55:02 UTC, chmike wrote: Can someone please explain me why this doesn't work as I would expect RVO to work ? I'm not an expert on the subject so this may contain some inaccuracies, but the gist of it is: As the name implies, NRVO is an optimization and therefore generally not guaranteed. The compiler is free to use NRVO or not, as it sees fit. In D there is one case where NRVO is required for correct semantics and therefore guaranteed: returning a struct with disabled postblit. For example: import std.stdio; struct S { int i; @disable this(this); } S foo() { S rv; writeln(); return rv; } void main() { auto s = foo(); writeln(); } This will print the same address twice even without optimizations. If the postblit isn't disabled the compiler will not use NRVO for returning S in this case. Which makes sense, because S is only four bytes, so writing through a pointer will actually be slower. On the other hand if S is large enough, the compiler will switch to using NRVO when possible: struct S { int[16] i; // I just used some large value, didn't test the threshold } S foo() { S rv; writeln(); return rv; } void main() { auto s = foo(); writeln(); }
Re: Client socket sending data only one time.
On Friday, 25 September 2015 at 04:38:06 UTC, tcak wrote: On Thursday, 24 September 2015 at 14:20:39 UTC, holo wrote: Hello I'm trying to connect to server and send data, with such simple client: #!/usr/bin/rdmd import std.stdio; import std.socket; import std.socketstream; import std.process; import std.conv; import core.time; void main() { char[1024] buffer = 0; Socket client = new TcpSocket(); auto addrServer = new InternetAddress("localhost", 8080); client.connect(addrServer); while(1) { client.send(readln()); client.receive(buffer); writeln(buffer); buffer = 0; } } It is working but only one time, when I'm trying to send again (second loop of while) it's stooping on readln() but not sending input data. What am i missing here? Thanks in advance for any help. Where is the other side of this client? There must be a TCP Server to listen connections, and create a second TCPSocket for communication. There is only one TCPSocket in your code. A lot of things are missing there. I would suggest you to check "C TCPSocket example" in your favourite search engine. Sorry i forgot to paste server side. After your post i look on that side of my simple socket program and i find out the problem. This is example from other forum topic: #!/usr/bin/rdmd import std.stdio; import std.socket; import std.algorithm; import std.conv; void main() { Socket server = new TcpSocket(); server.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true); server.bind(new InternetAddress(8080)); server.listen(1); while(true) { Socket client = server.accept(); char[1024] buffer = 0; auto received = client.receive(buffer); writefln("The client said:\n%s", buffer[0.. received]); enum header = "HTTP/1.0 200 OK\nContent-Type: text/html; charset=utf-8\n\n"; string response = header ~ "Hello World!\n"; client.send(response); client.shutdown(SocketShutdown.BOTH); client.close(); } } Lie we see after each data recive server is closing connection. I rewrite it to something like it: #!/usr/bin/rdmd import std.stdio; import std.socket; import std.algorithm; import std.conv; void main() { Socket server = new TcpSocket(); server.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true); server.bind(new InternetAddress(8080)); server.listen(1); Socket client = server.accept(); while(true) { char[1024] buffer = 0; auto received = client.receive(buffer); writefln("The client said:\n%s", buffer[0.. received]); enum header = "HTTP/1.0 200 OK\nContent-Type: text/html; charset=utf-8\n\n"; string response = header ~ "Hello World!\n"; client.send(response); auto command = to!string(buffer[0.. received]); if(command == "exit") { break; } } client.shutdown(SocketShutdown.BOTH); client.close(); } Now everything is working like i needed.
Re: Mac IDE with Intellisense
On Saturday, 26 September 2015 at 18:27:52 UTC, Mike McKee wrote: On Saturday, 26 September 2015 at 10:31:13 UTC, wobbles wrote: Have you installed dkit for sublime? As in? https://github.com/yazd/DKit Looks like it's alpha and doesn't run on Mac? No homebrew install? I'm using this and it works great. It uses dcd, so you need to install that first (via homebrew). As DKit it is a sublime text plugin, you don't install it via homebrew. Usually you'd install sublime packages via Package Control, but sadly DKit is not in the repositories yet. There is an issue about that already (https://github.com/yazd/DKit/issues/18). So instead, you install the package manually (just follow the instrcutions in the readme). I don't know where you got the idea it doesn't work on OS X, it works like a charm for me.
Re: Mac IDE with Intellisense
On Saturday, 26 September 2015 at 09:17:10 UTC, Mike McKee wrote: I've tried Sublime Text 3 editor on the Mac, but even it doesn't seem to have the D2 language in it yet (only D), and doesn't have intellisense for components in the imports that I do, even after saving the file after adding the import statements. What OSX editor do you recommend that would have intellisense? In all reality, I don't like intellisense -- it's annoying. However, I need it because the documentation for me is still a little hard to read and hard for me to search for a class method here or there. For instance, I was doing toHexString(myByteArray) instead of simply doing myByteArray.toHexString(). (That was on an md5 example, by the way.) Intellisense would have helped me realize this. Mono-D works really well. Also it integrates well with dub so you can simply "import" projects by opening dub.json file - pure awesomeness.
Server side command execution.
Hello Im trying to execute commands on server side. Here is my server based on other example from forum: #!/usr/bin/rdmd import std.stdio; import std.socket; import std.algorithm; import std.conv; void main() { Socket server = new TcpSocket(); server.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true); server.bind(new InternetAddress(8080)); server.listen(1); Socket client = server.accept(); while(true) { char[1024] buffer = 0; auto received = client.receive(buffer); writefln("The client said:\n%s", buffer[0.. received]); enum header = "HTTP/1.0 200 OK\nContent-Type: text/html; charset=utf-8\n\n"; string response = header ~ "Hello World!\n"; client.send(response); if(to!string(buffer) == "exit") { break; } } client.shutdown(SocketShutdown.BOTH); client.close(); } When i send "exit" nothing is happening. How it should be done to make it working? //holo
Re: Server side command execution.
On Sunday, 27 September 2015 at 23:56:10 UTC, holo wrote: Hello Im trying to execute commands on server side. Here is my server based on other example from forum: [...] You are comparing whole buffer to "exit"
All these errors running basic Pegged helloworld example.
The example is: import pegged.grammar; mixin(grammar(` Arithmetic: Term < Factor (Add / Sub)* Add < "+" Factor Sub < "-" Factor Factor < Primary (Mul / Div)* Mul < "*" Primary Div < "/" Primary Primary < Parens / Neg / Pos / Number / Variable Parens < "(" Term ")" Neg < "-" Primary Pos < "+" Primary Number < ~([0-9]+) Variable <- identifier `)); I'm using Visual D and have C:\MyProjects\D\Pegged (the git clone of pegged) added to the add'l imports field under project properties > compiler. I'm getting errors like these: Error 1 Error 42: Symbol Undefined _D6pegged7dynamic7grammar7grammarFAyaHAyaDFS6pegged3peg9ParseTreeZS6pegged3peg9ParseTreeZS6pegged7dynamic7grammar14DynamicGrammar (pegged.dynamic.grammar.DynamicGrammar pegged.dynamic.grammar.grammar(immutable(char)[], pegged.peg.ParseTree delegate(pegged.peg.ParseTree)[immutable(char)[]])) C:\MyProjects\D\PeggedPractice\ Error 2 Error 42: Symbol Undefined _D6pegged7dynamic7grammar12__ModuleInfoZ C:\MyProjects\D\PeggedPractice\ The # of errors was greatly reduced when I added the 3 pegged source files to my project. What can be going wrong? Thanks!
Re: All these errors running basic Pegged helloworld example.
On Sunday, 27 September 2015 at 06:30:37 UTC, Enjoys Math wrote: The example is: import pegged.grammar; mixin(grammar(` Arithmetic: Term < Factor (Add / Sub)* Add < "+" Factor Sub < "-" Factor Factor < Primary (Mul / Div)* Mul < "*" Primary Div < "/" Primary Primary < Parens / Neg / Pos / Number / Variable Parens < "(" Term ")" Neg < "-" Primary Pos < "+" Primary Number < ~([0-9]+) Variable <- identifier `)); I'm using Visual D and have C:\MyProjects\D\Pegged (the git clone of pegged) added to the add'l imports field under project properties > compiler. I'm getting errors like these: Error 1 Error 42: Symbol Undefined _D6pegged7dynamic7grammar7grammarFAyaHAyaDFS6pegged3peg9ParseTreeZS6pegged3peg9ParseTreeZS6pegged7dynamic7grammar14DynamicGrammar (pegged.dynamic.grammar.DynamicGrammar pegged.dynamic.grammar.grammar(immutable(char)[], pegged.peg.ParseTree delegate(pegged.peg.ParseTree)[immutable(char)[]])) C:\MyProjects\D\PeggedPractice\ Error 2 Error 42: Symbol Undefined _D6pegged7dynamic7grammar12__ModuleInfoZ C:\MyProjects\D\PeggedPractice\ The # of errors was greatly reduced when I added the 3 pegged source files to my project. What can be going wrong? Thanks! You must also pass the source root with -I: -IC:\MyProjects\D\Pegged (and maybe you miss another source since there are 5: '..\repos\Pegged\pegged\grammar.d' '..\repos\Pegged\pegged\parser.d' '..\repos\Pegged\pegged\peg.d' '..\repos\Pegged\pegged\dynamic\grammar.d' '..\repos\Pegged\pegged\dynamic\peg.d' ) By the way with Coedit you wouldn't have this kind of problems (Pegged is part of metaD). You can even run some test on Pegged without saving the file / without a project (this is called a runnable module). This is just what I've done. At least compile pegged as a static lib, then it's simpler, you just have to pass the -I pegged.lib and your custom sources files.
What am I doing wrong in this Pegged grammar? "Expected ' be ' but got 'groups'"
Here's a minimal example: http://pastebin.com/mJDwGDbb Is this a bug in Pegged? Nothing seems to fix it.