Re: D outperformed by C++, what am I doing wrong?

2017-08-12 Thread Roman Hargrave via Digitalmars-d-learn

On Sunday, 13 August 2017 at 06:09:39 UTC, amfvcg wrote:

Hi all,
I'm solving below task:

given container T and value R return sum of R-ranges over T. An 
example:

input : T=[1,1,1] R=2
output : [2, 1]

input : T=[1,2,3] R=1
output : [1,2,3]
(see dlang unittests for more examples)


Below c++ code compiled with g++-5.4.0 -O2 -std=c++14 runs on 
my machine in 656 836 us.
Below D code compiled with dmd v2.067.1 -O runs on my machine 
in ~ 14.5 sec.


If I had to guess, this could be due to backend and optimizer.
I don't want to go in to detail on my thoughts because I am not 
an expert
on codegen optimization, but I might suggest running your test 
compiled with
GDC (using identical optimization settings as G++) and ldc2 with 
similar settings.




Re: D outperformed by C++, what am I doing wrong?

2017-08-12 Thread rikki cattermole via Digitalmars-d-learn

On 13/08/2017 7:09 AM, amfvcg wrote:

Hi all,
I'm solving below task:

given container T and value R return sum of R-ranges over T. An example:
input : T=[1,1,1] R=2
output : [2, 1]

input : T=[1,2,3] R=1
output : [1,2,3]
(see dlang unittests for more examples)


Below c++ code compiled with g++-5.4.0 -O2 -std=c++14 runs on my machine 
in 656 836 us.
Below D code compiled with dmd v2.067.1 -O runs on my machine in ~ 14.5 
sec.


Each language has it's own "way of programming", and as I'm a beginner 
in D - probably I'm running through bushes instead of highway. Therefore 
I'd like to ask you, experienced dlang devs, to shed some light on "how 
to do it dlang-way".



C++ code:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


template
std::vector sum_elements(const T& beg, const T& end, std::size_t k, K 
def)

{
  if (k == 0) {
  return std::vector{};
  }
  return sum_elements(beg, end, k, def, [](auto &l, auto &r){ return 
r+l;});

}

template
std::vector
 sum_elements(
 const T& beg,
 const T& end,
 std::size_t k,
 K def,
 BinaryOp op)
{
 std::vector out;
 out.reserve((std::distance(beg, end) - 1)/k + 1);
 for (auto it = beg; it!=end; std::advance(it, 
std::min(static_cast(std::distance(it, end)), k)))

 {
 out.push_back(std::accumulate(it, std::next(it, 
std::min(static_cast(std::distance(it, end)), k)), def, op));

 }
 return out;
}

int main()
{
 std::vector vec;
 auto size = 100;
 vec.reserve(size);
 for (int i=0; i < size; ++i)
 vec.push_back(i);
 auto beg = std::chrono::system_clock::now();
 auto sum = 0;
 for (int i=0; i < 100; i++)
 sum += sum_elements(vec.begin(), vec.end(), 2, 0).size();
 auto end = std::chrono::system_clock::now();
 std::cout << 
std::chrono::duration_cast(end-beg).count() 
<< std::endl;

 std::cout << sum << std::endl;

 return sum;
}


D code:

import std.stdio : writeln;
import std.algorithm.comparison: min;
import std.algorithm.iteration: sum;
import core.time: MonoTime, Duration;


T[] sum_subranges(T)(T[] input, uint range)
{
 T[] result;
 if (range == 0)
 {
 return result;
 }
 for (uint i; i < input.length; i=min(i+range, input.length))
 {
 result ~= sum(input[i..min(i+range, input.length)]);
 }
 return result;
}

unittest
{
 assert(sum_subranges([1,1,1], 2) == [2, 1]);
 assert(sum_subranges([1,1,1,2,3,3], 2) == [2, 3, 6]);
 assert(sum_subranges([], 2) == []);
 assert(sum_subranges([1], 2) == [1]);
 assert(sum_subranges([1], 0) == []);
}


int main()
{
 int[100] v;
 for (int i=0; i < 100; ++i)
 v[i] = i;
 int sum;
 MonoTime beg = MonoTime.currTime;
 for (int i=0; i < 100; i++)
 sum += cast(int)sum_subranges(v,2).length;
 MonoTime end = MonoTime.currTime;
 writeln(end-beg);
 writeln(sum);
 return sum;
}



Dmd compiles quickly, but doesn't create all that optimized code.
Try ldc or gdc and get back to us about it ;)



D outperformed by C++, what am I doing wrong?

2017-08-12 Thread amfvcg via Digitalmars-d-learn

Hi all,
I'm solving below task:

given container T and value R return sum of R-ranges over T. An 
example:

input : T=[1,1,1] R=2
output : [2, 1]

input : T=[1,2,3] R=1
output : [1,2,3]
(see dlang unittests for more examples)


Below c++ code compiled with g++-5.4.0 -O2 -std=c++14 runs on my 
machine in 656 836 us.
Below D code compiled with dmd v2.067.1 -O runs on my machine in 
~ 14.5 sec.


Each language has it's own "way of programming", and as I'm a 
beginner in D - probably I'm running through bushes instead of 
highway. Therefore I'd like to ask you, experienced dlang devs, 
to shed some light on "how to do it dlang-way".



C++ code:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


template
std::vector sum_elements(const T& beg, const T& end, 
std::size_t k, K def)

{
 if (k == 0) {
 return std::vector{};
 }
 return sum_elements(beg, end, k, def, [](auto &l, auto &r){ 
return r+l;});

}

template
std::vector
sum_elements(
const T& beg,
const T& end,
std::size_t k,
K def,
BinaryOp op)
{
std::vector out;
out.reserve((std::distance(beg, end) - 1)/k + 1);
for (auto it = beg; it!=end; std::advance(it, 
std::min(static_cast(std::distance(it, end)), k)))

{
out.push_back(std::accumulate(it, std::next(it, 
std::min(static_cast(std::distance(it, end)), k)), 
def, op));

}
return out;
}

int main()
{
std::vector vec;
auto size = 100;
vec.reserve(size);
for (int i=0; i < size; ++i)
vec.push_back(i);
auto beg = std::chrono::system_clock::now();
auto sum = 0;
for (int i=0; i < 100; i++)
sum += sum_elements(vec.begin(), vec.end(), 2, 0).size();
auto end = std::chrono::system_clock::now();
std::cout << 
std::chrono::duration_cast(end-beg).count() << std::endl;

std::cout << sum << std::endl;

return sum;
}


D code:

import std.stdio : writeln;
import std.algorithm.comparison: min;
import std.algorithm.iteration: sum;
import core.time: MonoTime, Duration;


T[] sum_subranges(T)(T[] input, uint range)
{
T[] result;
if (range == 0)
{
return result;
}
for (uint i; i < input.length; i=min(i+range, input.length))
{
result ~= sum(input[i..min(i+range, input.length)]);
}
return result;
}

unittest
{
assert(sum_subranges([1,1,1], 2) == [2, 1]);
assert(sum_subranges([1,1,1,2,3,3], 2) == [2, 3, 6]);
assert(sum_subranges([], 2) == []);
assert(sum_subranges([1], 2) == [1]);
assert(sum_subranges([1], 0) == []);
}


int main()
{
int[100] v;
for (int i=0; i < 100; ++i)
v[i] = i;
int sum;
MonoTime beg = MonoTime.currTime;
for (int i=0; i < 100; i++)
sum += cast(int)sum_subranges(v,2).length;
MonoTime end = MonoTime.currTime;
writeln(end-beg);
writeln(sum);
return sum;
}



Re: Does anyone understand how to use "shared" types with concurrency send/receive functions?

2017-08-12 Thread crimaniak via Digitalmars-d-learn

On Saturday, 12 August 2017 at 18:57:44 UTC, Arek wrote:

I have the folowing problem:
I like to envelope the class object in struct to control the 
destruction moment and then send this object to another 
thread/fiber (or task, cause I use vibe-d).


I can't find any method to make it working. Any ideas?


I tried it too some time ago. Then I read Alexandrescu book and 
realized that the authors of the language do not want anyone to 
do this. Long story short, just plan your application so that 
each complex object is monitored by only one thread/task, and 
pass not objects, but messages (immutable structs) about what to 
do with them.




Re: html fetcher/parser

2017-08-12 Thread Soulsbane via Digitalmars-d-learn

On Saturday, 12 August 2017 at 19:53:22 UTC, Faux Amis wrote:
I would like to get into D again by making a small program 
which fetches a website every X-time and keeps track of all 
changes within specified dom elements.


fetching: should I go for std curl, vibe.d or something else?
parsing: I could only find these dub packages: htmld & 
libdominator.

And they don't seem overly active, any recommendations?

As I haven't been using D for some time I just don't want to 
get off with a bad start :)

thx


I've the requests module nice to work with: 
http://code.dlang.org/packages/requests


Re: html fetcher/parser

2017-08-12 Thread Michael via Digitalmars-d-learn

On Saturday, 12 August 2017 at 20:22:44 UTC, Adam D. Ruppe wrote:

On Saturday, 12 August 2017 at 19:53:22 UTC, Faux Amis wrote:

[...]


My dom.d and http2.d combine to make this easy:

https://github.com/adamdruppe/arsd/blob/master/dom.d
https://github.com/adamdruppe/arsd/blob/master/http2.d

[...]


Sometimes it feels like there's the standard D library, Phobos, 
and then for everything else you have already developed a 
suitable library to supplement it haha!


Re: html fetcher/parser

2017-08-12 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 12 August 2017 at 19:53:22 UTC, Faux Amis wrote:
I would like to get into D again by making a small program 
which fetches a website every X-time and keeps track of all 
changes within specified dom elements.


My dom.d and http2.d combine to make this easy:

https://github.com/adamdruppe/arsd/blob/master/dom.d
https://github.com/adamdruppe/arsd/blob/master/http2.d

and support file for random encodings:

https://github.com/adamdruppe/arsd/blob/master/characterencodings.d


Or via dub:

http://code.dlang.org/packages/arsd-official

the dom and http subpackages are the ones you want.


Docs: http://dpldocs.info/arsd.dom


Sample program:

---
// compile: $ dmd thisfile.d ~/arsd/{dom,http2,characterencodings}

import std.stdio;
import arsd.dom;

void main() {
auto document = Document.fromUrl("https://dlang.org/";);
writeln(document.optionSelector("p").innerText);
}
---

Output:

D is a general-purpose programming language with
static typing, systems-level access, and C-like syntax.
It combines efficiency, control and modeling power with 
safety

and programmer productivity.




Note that the https support requires OpenSSL available on your 
system. Works best on Linux with it installed as a devel lib (so 
like openssl-devel or whatever, just like you would if using it 
from C).




How it works:


Document.fromUrl uses the http lib to fetch it, then 
automatically parse the contents as a dom document. It will 
correct for common errors in webpage markup, character sets, etc.


Document and Element both have various methods for navigating, 
modifying, and accessing the DOM tree. Here, I used 
`optionSelector`, which works like `querySelector` in Javascript 
(and the same syntax is used for CSS), returning the first 
matching element.


querySelector, however, returns null if there is nothing found. 
optionSelector returns a dummy object instead, so you don't have 
to explicitly test it for null and instead just access its 
methods.


`innerText` returns the text inside, stripped of markup. You 
might also want `innerHTML`, or `toString` to get the whole 
thing, markup and all.




there's a lot more you can do too but just these few functions I 
think will be enough for your task.



Bonus fact: 
http://dpldocs.info/experimental-docs/std.algorithm.comparison.levenshteinDistanceAndPath.1.html that function from the standard library makes doing a diff display of before and after pretty simple


html fetcher/parser

2017-08-12 Thread Faux Amis via Digitalmars-d-learn
I would like to get into D again by making a small program which fetches 
a website every X-time and keeps track of all changes within specified 
dom elements.


fetching: should I go for std curl, vibe.d or something else?
parsing: I could only find these dub packages: htmld & libdominator.
And they don't seem overly active, any recommendations?

As I haven't been using D for some time I just don't want to get off 
with a bad start :)

thx



Does anyone understand how to use "shared" types with concurrency send/receive functions?

2017-08-12 Thread Arek via Digitalmars-d-learn

I have the folowing problem:
I like to envelope the class object in struct to control the 
destruction moment and then send this object to another 
thread/fiber (or task, cause I use vibe-d).


I can't find any method to make it working. Any ideas?

dmd (version 075) gives so stupid results, I belive it's broken 
(I've created the issue 
https://issues.dlang.org/show_bug.cgi?id=17749 )


ldc2 returns some errors:

.../import/std/variant.d(610,27): Error: function 
core.stdc.string.memcpy (void* s1, const(void*) s2, ulong n) is 
not callable using argument types (ubyte[32]*, 
shared(Env!(shared(A)))*, ulong)
.../import/std/conv.d(4186,9): Error: static assert  "Cannot 
emplace a Env!(shared(A)) because Env!(shared(A)).this(this) is 
annotated with @disable."
.../import/std/conv.d(4198,24):instantiated from here: 
emplaceRef!(Env!(shared(A)), Env!(shared(A)), Env!(shared(A)))
.../import/std/variant.d(301,35):instantiated from here: 
emplaceRef!(Env!(shared(A)), Env!(shared(A)))
.../import/std/variant.d(630,21):instantiated from here: 
handler!(shared(Env!(shared(A
.../import/std/variant.d(544,17):... (5 instantiations, 
-v to show) ...

../../../.dub/packages/vibe-core-1.1.1/vibe-core/source/vibe/core/concurrency.d(1223,64):
instantiated from here: send!(shared(Env!(shared(A
app.d(74,7):instantiated from here: 
send!(shared(Env!(shared(A




The code may be complied like this: dub --single --compiler=ldc2 
./app.d   (assuming it's saved in app.d file).



/+
dub.sdl:
name "simple"
dependency "vibe-core" version="~>1.1.0"
+/
import std.stdio;
import std.format;

import vibe.core.core;
import vibe.core.task;
import vibe.core.concurrency;

// simple class with destructor
shared class A
{
int i;
this(int i)
{
this.i = i;
}

~this()
{
writeln("destruct ", i);
}

}

shared struct Env(T) if (is(T == class) && is(T == shared))
{
T obj;

this(T o)
{
obj = obj;
}

this(this)
{
// some magic here
}

auto opAssign(shared(Env!T) other)
{
obj = other.obj;
return this;
}

auto opAssign(ref shared(Env!T) other)
{
obj = other.obj;
return this;
}

~this()
{
if (obj !is null)
destroy(obj);
obj = null;
}
}

void main()
{
auto consumer = runTask({
auto got = receiveOnly!(shared Env!(shared A))();
writeln(typeof(got).stringof);
});

auto producer = runTask({
auto a = new shared A(1);
shared b = shared Env!(shared A)(a);
writeln(typeof(b).stringof);
send(consumer, b);
});
runApplication();
}



Re: How to make http requests to unix socket?

2017-08-12 Thread Sebastiaan Koppe via Digitalmars-d-learn

On Friday, 11 August 2017 at 18:39:54 UTC, dvnguyen wrote:
How to make http requests to unix socket? For example, in 
Docker engine api,


curl --unix-socket /var/run/docker.sock 
http:/v1.24/containers/json


I made a pull request a year ago or so to have vibe-d's 
requestHttp to support unix sockets. Call it like this 
`requestHTTP("https+unix://%2Fvar%2Frun%2Fdocker.sock/containers/create`.
You will need to set some vibe-d tls things to accept the 
connection.


Sönke just upgraded vibe-core/event-core to support it as well if 
I remember it correctly (so you can also use vibe 8.x with 
vibe-core).


As for endpoints which return streams (like 
containers/"~id~"/logs?stdout=1&stderr=1&follow=1), Docker 
multiplexes stdout/stderr over http with chunked 
transfer-encoding.
The protocol is a 8 byte Header + Frame. The last four bytes in 
the Header is an uint32 representing the frame size. The first 
byte is either 1 -> stdout or 2 -> stderr, which designates the 
type of the stream in the following frame.


Re: __dtor vs __xdtor

2017-08-12 Thread Marco Leise via Digitalmars-d-learn
Am Fri, 11 Aug 2017 17:10:14 +
schrieb bitwise :

> Ok thanks.
> 
> I don't understand why you would ever want to call __dtor 
> then...is it possible to have only __dtor without also having 
> __xdtor? Like, if I want to call a struct's destructor, do I have 
> to check for both, or can I just always check for, and call 
> __xdtor?

I think it was simply that all the special methods needed a
symbol name, so this() was called __ctor and ~this() was
called __dtor. It was never supposed to cover field
destruction, mixed in destructors or inheritance in classes.
User code was not expected to call these directly anyways.
Not very long ago __xdtor and __xpostblit were introduced
that wrap up the entire finalization and copy operation.
__dtor will remain as the 1:1 representation of the ~this()
method.

-- 
Marco



ddoc and method overriding.

2017-08-12 Thread Alexandru Ermicioi via Digitalmars-d-learn

Hi all,

Having a trivial example such as:

```D
class Foo {
/**
 * A documentation info
**/
void fancy();
}

class Moo {
override void fancy();
}
```

Is there a way to tell ddoc to use Foo.fancy documentation block 
for overriding Moo.fancy method?


Thx.