Re: How Nested Functions Work, part 2

2009-09-19 Thread bearophile
language_fan:

>There is no real interest in D outside the community (IMHO).<

There can be various causes:
- At University here they teach mostly Java. So young people know and like 
VM-based languages.
- Such people are "spoiled": they don't even want to touch a language that has 
no very good IDE and no good std lib and no good module ecosystem. D looks 
fitter for older people, grown with milk&C.
- Currently D isn't very useful for the web/mobile, where lot of programmers 
are.
- People today like a handy language, because often CPU efficiency isn't 
strictly critical (and when necessary Java is usually fast enough, and often 
faster than D code compiled with DMD). A language that already has just the one 
lib you need right now, is often what they prefer.
- D allows to program in a low-level style too, but most people don't know how 
to program in that style, don't know asm, and usually don't need it. So by 
design D may be a nice language. I don't know.

Bye,
bearophile


Rich Hickey's slides from jvm lang summit - worth a read?

2009-09-19 Thread language_fan
http://wiki.jvmlangsummit.com/images/a/ab/HickeyJVMSummit2009.pdf


Re: Mixin a constructor ?

2009-09-19 Thread language_fan
>> template C ()
>> {
>> this (int i)
>> {
>> }
>> }
>> class A
>> {
>> mixin C;
>> this ()
>> {
>> }
>> }
>> void main ()
>> {
>> auto a = new A(3);
>> }

Since the constructor has no meaning outside classes, should it be 
interpreted as a free function if mixed in a non-class context? I really 
wonder how this could be valid code. Does the grammar even support the 
3rd line?


Re: How Nested Functions Work, part 2

2009-09-19 Thread language_fan
Sun, 20 Sep 2009 01:09:56 +, language_fan thusly wrote:

> Sat, 19 Sep 2009 11:44:33 -0700, Walter Bright thusly wrote:
> 
>> Lutger wrote:
>>> Cool article, I posted a comment. Reddit seems to be going downhill
>>> fast though, it's even worse than slashdot.
>> 
>> I know, the negative comments don't even make any sense.
>> 
>>> Are locally instantiated templates used in phobos?
>> 
>> Yes.
> 
> I read the comments and I think some of them are justified. You cannot
> really expect the way you built dmd to be the only alternative.
> Efficient closures can be implemented differently if you have a VM that
> supports precise generational gc, region inference, and the language is
> a bit more value oriented (= functional).

Another thing is that often when an article about D is released, the only 
positive comments come from the members of the existing (smallish)
community. There is no real interest in D outside the community (IMHO). 
Reddit's programming section is full of language fanatics, thus it is a 
bit hard to impress folks with tiny tricks as they daily work with 
various programming languages and concepts.


Re: How Nested Functions Work, part 2

2009-09-19 Thread language_fan
Sat, 19 Sep 2009 11:44:33 -0700, Walter Bright thusly wrote:

> Lutger wrote:
>> Cool article, I posted a comment. Reddit seems to be going downhill
>> fast though, it's even worse than slashdot.
> 
> I know, the negative comments don't even make any sense.
> 
>> Are locally instantiated templates used in phobos?
> 
> Yes.

I read the comments and I think some of them are justified. You cannot 
really expect the way you built dmd to be the only alternative. Efficient 
closures can be implemented differently if you have a VM that supports 
precise generational gc, region inference, and the language is a bit more 
value oriented (= functional).


memset and related things

2009-09-19 Thread bearophile
In a program I've seen that in the inner loop an array cleaning was taking too 
much time. To solve the problem I've done many experiments, and I've also 
produced the following testing program.

The short summary is, to set array of 4 byte integers to a certain constant the 
best was are:
- if len <~ 20, then just use an inlined loop.
- if 20 < len < 200_000 it's better to use a loop unrolled 4 times with the 
movaps instruction (8 times unrolled is a little worse).
- if n > 200_000 a loop with the movntps instruction is better.

Generally such solutions are better than the memset() (only when len is about 
150_000 memset is a bit better than four movaps).


See also:
http://www.gamedev.net/community/forums/topic.asp?topic_id=532112

http://www.sesp.cse.clrc.ac.uk/html/SoftwareTools/vtune/users_guide/mergedProjects/analyzer_ec/mergedProjects/reference_olh/mergedProjects/instructions/instruct32_hh/vc197.htm


This is a possible function that can be used if a.length > 20, otherwise an 
inlined loop is faster:


void memset4(T)(T[] a, T value) {
// to be used for len(a) >~ 20
static assert(T.sizeof == 4);
if (!a.length)
return;
auto a_ptr = a.ptr;
auto a_end = a_ptr + a.length;

// align pointer
size_t aux = ((cast(size_t)a_ptr + 15) & ~15);
auto n = cast(T*)aux;
while (a_ptr < n)
*a_ptr++ = value;
n = cast(T*)((cast(size_t)a_end) & ~15);

if (a_ptr < n && (a_end - a_ptr) >= 16) {
// Aligned case
if ((a_end - a_ptr) >= 200_000) {
asm {
mov ESI, a_ptr;
mov EDI, n;

movss XMM0, value; // XMM0 = value,value,value,value
shufps XMM0, XMM0, 0;

align 8;
LOOP1:
add ESI, 64;
movntps [ESI+ 0-64], XMM0;
movntps [ESI+16-64], XMM0;
movntps [ESI+32-64], XMM0;
movntps [ESI+48-64], XMM0;
cmp ESI, EDI;
jb LOOP1;

mov a_ptr, ESI;
}
} else {
asm {
mov ESI, a_ptr;
mov EDI, n;

movss XMM0, value; // XMM0 = value,value,value,value
shufps XMM0, XMM0, 0;

align 8;
LOOP2:
add ESI, 64;
movaps [ESI+ 0-64], XMM0;
movaps [ESI+16-64], XMM0;
movaps [ESI+32-64], XMM0;
movaps [ESI+48-64], XMM0;
cmp ESI, EDI;
jb LOOP2;

mov a_ptr, ESI;
}
}
}

// trailing ones
while (a_ptr < a_end)
*a_ptr++ = value;
}



So the D front end can implement:
int[] a = ...;
a[] = k;


as:

int[] a = ...;
if (a.length < 20)
  for (int i; i < a.length; i++)
a[i] = k;
else
  memset4(a, k);

Arrays of other types of values need a little different code. Today most CPUs 
support SSE, but in the uncommon situations where it's not available it can be 
used C memset() instead of memset4 (the inline for loop is useful anyway when 
you use C memset).

I am ignorant of SSE asm still, so the code I have written can contain bugs, 
naive things, etc.

If such code can be debugged, and my timings are meaningful, then the code can 
be put inside the d front-end, so LDC too can use it (another good thing is for 
LLVM to improve its memset intrinsic, so LDC front-end doesn't need to perform 
such thing).



// benchmark code

version (Tango) {
import tango.stdc.stdio: printf;
import tango.stdc.time: clock, CLOCKS_PER_SEC;
import tango.math.Math: sqrt;
import tango.stdc.string: memset;
} else {
import std.c.stdio: printf;
import std.c.time: clock, CLOCKS_PER_SEC;
import std.math: sqrt;
import std.c.string: memset;
}


double myclock() {
return cast(double)clock() / CLOCKS_PER_SEC;
}


void memset4_movaps(T)(T[] a, T value) {
// to be used if a.length >~ 20, otherwise an inlined loop is faster

static assert(T.sizeof == 4);
if (!a.length)
return;
auto a_ptr = a.ptr;
auto a_end = a_ptr + a.length;

// align pointer
size_t aux = ((cast(size_t)a_ptr + 15) & ~15);
auto n = cast(T*)aux;
while (a_ptr < n)
*a_ptr++ = value;
n = cast(T*)((cast(size_t)a_end) & ~15);

if (a_ptr < n && (a_end - a_ptr) >= 16) {
// Aligned case
asm {
mov ESI, a_ptr;
mov EDI, n;

movss XMM0, value; // XMM0 = value,value,value,value
shufps XMM0, XMM0, 0;

align 8;
LOOP2:
add ESI, 64;
movaps [ESI+ 0-64], XMM0;
movaps [ESI+16-64], XMM0;
movaps [ESI+32-64], XMM0;
movaps [ESI+48-64], XMM0;
cmp ESI, EDI;
jb LOOP2;

mov a_ptr, E

Re: Mixin a constructor ?

2009-09-19 Thread Jacob Carlborg

On 9/19/09 20:55, Christopher Wright wrote:

Jacob Carlborg wrote:

Is it supposed to possible to mixin a constructor? The code below
doesn't compile. The error: is "main.d(23): Error: constructor
main.A.this() does not match parameter types (int)
main.d(23): Error: expected 0 arguments, not 1"


A template mixin introduces a new scope. This is pretty annoying in some
cases. More often, it is convenient. Since the constructor is not in the
same scope as the class, it might be causing problems.


The problem only seems to be when I'm having constructors both in the 
template and in the class.



If you didn't introduce a new scope by default, it'd be easy enough to
add it in the template or around the mixin:

template MixMeIn()
{
int imInUrScope;
{
int imOuttaUrScope;
}
}


Additionally, the difference would be easily detectable, because symbol
collision would cause the code to fail to compile. Unless the mixin adds
a function overload and you pass the address of the overload set
somewhere, and now you're passing the wrong overload...which is an old
problem for D, aggravated by D's property syntax, and unlikely to be
fixed soon.




Re: Mixin a constructor ?

2009-09-19 Thread BCS

Hello Jacob,


Is it supposed to possible to mixin a constructor? The code below
doesn't compile. The error: is "main.d(23): Error: constructor
main.A.this() does not match parameter types (int)
main.d(23): Error: expected 0 arguments, not 1"


IIRC mixins can't overload with other mixins or non mixins so if you were 
to drop the this() that should work. OTOH I'd be surprised if you can do 
that without breaking something else in the general case.



template C ()
{
this (int i)
{
}
}
class A
{
mixin C;
this ()
{
}
}
void main ()
{
auto a = new A(3);
}





Re: Descent support dmd2.032£¿

2009-09-19 Thread dolive89
dolive89 дµ½:

> 
> In addition, please provide the availability of language pack£¬Let's translate

ISO-8859-1 encode don't Support Chinese£¬Show is Unrecognizable Code.

help me

dolive89



Re: Mixin a constructor ?

2009-09-19 Thread Christopher Wright

Jacob Carlborg wrote:
Is it supposed to possible to mixin a constructor? The code below 
doesn't compile. The error: is "main.d(23): Error: constructor 
main.A.this() does not match parameter types (int)

main.d(23): Error: expected 0 arguments, not 1"


A template mixin introduces a new scope. This is pretty annoying in some 
cases. More often, it is convenient. Since the constructor is not in the 
same scope as the class, it might be causing problems.


If you didn't introduce a new scope by default, it'd be easy enough to 
add it in the template or around the mixin:


template MixMeIn()
{
   int imInUrScope;
   {
  int imOuttaUrScope;
   }
}


Additionally, the difference would be easily detectable, because symbol 
collision would cause the code to fail to compile. Unless the mixin adds 
a function overload and you pass the address of the overload set 
somewhere, and now you're passing the wrong overload...which is an old 
problem for D, aggravated by D's property syntax, and unlikely to be 
fixed soon.


Re: How Nested Functions Work, part 2

2009-09-19 Thread Walter Bright

Max Samukha wrote:

Walter Bright wrote:

http://www.reddit.com/r/programming/comments/9lxy5/how_nested_functions_clos


There is a typo in the example


Thanks, fixed it.


Re: How Nested Functions Work, part 2

2009-09-19 Thread Walter Bright

Max Samukha wrote:

Walter Bright wrote:

http://www.reddit.com/r/programming/comments/9lxy5/how_nested_functions_clos


There is a typo in the example


Thanks, fixed it.


Re: How Nested Functions Work, part 2

2009-09-19 Thread Walter Bright

Lutger wrote:
Cool article, I posted a comment. Reddit seems to be going downhill fast 
though, it's even worse than slashdot. 


I know, the negative comments don't even make any sense.


Are locally instantiated templates used in phobos?


Yes.


Re: Mixin a constructor ?

2009-09-19 Thread dsimcha
== Quote from Jacob Carlborg (d...@me.com)'s article
> Is it supposed to possible to mixin a constructor? The code below
> doesn't compile. The error: is "main.d(23): Error: constructor
> main.A.this() does not match parameter types (int)
> main.d(23): Error: expected 0 arguments, not 1"
> template C ()
> {
>   this (int i)
>   {
>   }
> }
> class A
> {
>   mixin C;
>   this ()
>   {
>   }
> }
> void main ()
> {
>   auto a = new A(3);
> }

I'm not sure exactly why this example doesn't work, but it looks like a bug.
Please file.  If you just want to insert a bunch of boilerplate without any
parameters, you probably should try a string mixin instead.  The following does 
work:

enum string C = q{
this (int i) {

}
};

class A {
mixin(C);

this () {

}
}

void main () {
auto a = new A(3);
}



Mixin a constructor ?

2009-09-19 Thread Jacob Carlborg
Is it supposed to possible to mixin a constructor? The code below 
doesn't compile. The error: is "main.d(23): Error: constructor 
main.A.this() does not match parameter types (int)

main.d(23): Error: expected 0 arguments, not 1"

template C ()
{
this (int i)
{

}
}

class A
{
mixin C;

this ()
{

}
}

void main ()
{
auto a = new A(3);
}


Re: How Nested Functions Work, part 2

2009-09-19 Thread Lutger
Cool article, I posted a comment. Reddit seems to be going downhill fast 
though, it's even worse than slashdot. 

Are locally instantiated templates used in phobos?


Re: How Nested Functions Work, part 2

2009-09-19 Thread Max Samukha
Walter Bright wrote:

> 
http://www.reddit.com/r/programming/comments/9lxy5/how_nested_functions_clos


There is a typo in the example (if it is supposed to be compiled with 
current dmd):

int bar(int i)
{
int abc(int x) { return i + x; }

return foo(&abc);
}

should be

int bar(int i)
{
int abc(int x) { return i + x; }

return foo!abc;
}