digitalmars.D - A Tango Fibers question and a functional programming anecdote.
- downs <default_357-line yahoo.de> Oct 27 2007
- "Jarrett Billingsley" <kb3ctd2 yahoo.com> Oct 27 2007
- downs <default_357-line yahoo.de> Oct 28 2007
- Daniel Keep <daniel.keep.lists gmail.com> Oct 28 2007
- "Jarrett Billingsley" <kb3ctd2 yahoo.com> Oct 28 2007
- Don Clugston <dac nospam.com.au> Oct 28 2007
- Robert Fraser <fraserofthenight gmail.com> Oct 28 2007
- Bill Baxter <dnewsgroup billbaxter.com> Oct 28 2007
- BCS <ao pathlink.com> Oct 28 2007
- BCS <ao pathlink.com> Oct 28 2007
- Daniel Keep <daniel.keep.lists gmail.com> Oct 28 2007
- downs <default_357-line yahoo.de> Oct 28 2007
- Bill Baxter <dnewsgroup billbaxter.com> Oct 29 2007
- downs <default_357-line yahoo.de> Oct 29 2007
- bearophile <bearophile lycos.com> Oct 29 2007
Here's a question to the tango devs. I've been developing my own stackthreads (Fibers basically) library for a while now, and finally got it working a few days ago. Then we did some speed tests and discovered that it was about twice as slow as Tango's Fiber implementation. This prompted me to dig into the Fibers sourcecode, where I discovered that Fibers doesn't backup ECX/EDX on context switch, and the FP registers only on windows. Now I am confused. Could a knowledgeable tango dev kindly shed light on this issue? Why isn't it necessary to save all registers on a context switch? ---- Now for the anecdote. Today, at five in the morning, I wrote what I believe to be the first purely functional D code with as little redundancy as possible in the language.["Load ", "Prefetch ", "Prefetch "] /zip/ ([0, 1, 2] /map/ (&info
&concat!(string) /zip/ [&update, &prefetch /fix/ (currentStrip-1), &prefetch /fix/ (currentStrip-2)] /map/ &Pool.addTask;
So I looked at it, and for some _strange_ reason my eyes suddenly started bleeding heavily. That's when I knew I had overdone it on the templates again. :) PS: Here's the same code with some line breaks. But where's the fun in that?["Load ", "Prefetch ", "Prefetch "] /zip/ ( [0, 1, 2] /map/ ( &info /rfix/ currentComic /fix/ ( &subtract!(int) /fix/ currentStrip ) ) ) /map/ &concat!(string) /zip/ [ &update, &prefetch /fix/ (currentStrip-1), &prefetch /fix/ (currentStrip-2) ] /map/ &Pool.addTask;
Oct 27 2007
"downs" <default_357-line yahoo.de> wrote in message news:fg15ev$12uh$1 digitalmars.com...Here's a question to the tango devs. I've been developing my own stackthreads (Fibers basically) library for a while now, and finally got it working a few days ago. Then we did some speed tests and discovered that it was about twice as slow as Tango's Fiber implementation. This prompted me to dig into the Fibers sourcecode, where I discovered that Fibers doesn't backup ECX/EDX on context switch, and the FP registers only on windows. Now I am confused. Could a knowledgeable tango dev kindly shed light on this issue? Why isn't it necessary to save all registers on a context switch?
I'm not knowledgeable, but you said ECX/EDX and it triggered something in my brain. Sure enough, according to the D 1.0 ABI, "EAX, ECX, EDX are scratch registers and can be destroyed by a function." So they don't have to be preserved because you're allowed to do anything with them. As for nothing but ST(0-7) being preserved on Windows.. no clue.["Load ", "Prefetch ", "Prefetch "] /zip/ ([0, 1, 2] /map/ (&info
&concat!(string) /zip/ [&update, &prefetch /fix/ (currentStrip-1), &prefetch /fix/ (currentStrip-2)] /map/ &Pool.addTask;
["Load ", "Prefetch ", "Prefetch "] /zip/ ( [0, 1, 2] /map/ ( &info /rfix/ currentComic /fix/ ( &subtract!(int) /fix/ currentStrip ) ) ) /map/ &concat!(string) /zip/ [ &update, &prefetch /fix/ (currentStrip-1), &prefetch /fix/ (currentStrip-2) ] /map/ &Pool.addTask;
Why/how do you write code like this? Is it some kind of thing in your brain that makes you think about everything inside out? Stop it. Please. For all our brains' sakes.
Oct 27 2007
Jarrett Billingsley wrote:I'm not knowledgeable, but you said ECX/EDX and it triggered something in my brain. Sure enough, according to the D 1.0 ABI, "EAX, ECX, EDX are scratch registers and can be destroyed by a function." So they don't have to be preserved because you're allowed to do anything with them.
Removed the push for ECX and EDX ( the others are used internally ). It seems to work. Many thanks! :) Still runs somewhat slower than Fibers, though. I can't imagine why. Also, oddly, GDC doesn't allow me to push EBP in -O mode and D inline assembly. Works in native GCC assembly. *Very* odd.As for nothing but ST(0-7) being preserved on Windows.. no clue.
Aww. Still thanks for answering. I'm sure somebody will know it.["Load ", "Prefetch ", "Prefetch "] /zip/ ([0, 1, 2] /map/ (&info
&concat!(string) /zip/ [&update, &prefetch /fix/ (currentStrip-1), &prefetch /fix/ (currentStrip-2)] /map/ &Pool.addTask;
["Load ", "Prefetch ", "Prefetch "] /zip/ ( [0, 1, 2] /map/ ( &info /rfix/ currentComic /fix/ ( &subtract!(int) /fix/ currentStrip ) ) ) /map/ &concat!(string) /zip/ [ &update, &prefetch /fix/ (currentStrip-1), &prefetch /fix/ (currentStrip-2) ] /map/ &Pool.addTask;
Why/how do you write code like this? Is it some kind of thing in your brain that makes you think about everything inside out? Stop it. Please. For all our brains' sakes.
:evil cackle: Braaaaaaaaaaaaaaains! Actually, it started out with a piece of code that looked like thisPool.addTask("Load "~info(), &update); Pool.addTask("Prefetch "~info(), &update); Pool.addTask("Prefetch "~info(), &update);
Clearly, the addTask is redundant here. I replaced it with[stuple("Load "~info(), &update), stuple...] /map/ &Pool.addTask;
Somebody in IRC pointed out that the stuple was redundant. He must feel horrible for indirectly causing this.["Load "~info, ...] /zip/ [&update, ...] /map/ &Pool.addTask;
Then I noticed the info calls were also redundant. It went kinda out of control after that. You know the result. :) --downs
Oct 28 2007
downs wrote:Jarrett Billingsley wrote:... As for nothing but ST(0-7) being preserved on Windows.. no clue.
Aww. Still thanks for answering. I'm sure somebody will know it.
I think this might be it (from the ABI): "The FPU stack must be empty when calling a function." So when it calls the function, the caller will have emptied the FP stack, so there's nothing *in* ST(0-7) to save....["Load "~info, ...] /zip/ [&update, ...] /map/ &Pool.addTask;
Then I noticed the info calls were also redundant. It went kinda out of control after that. You know the result. :) --downs
You're my hero, downs. -- Daniel
Oct 28 2007
"downs" <default_357-line yahoo.de> wrote in message news:fg1qhp$22m6$1 digitalmars.com...Actually, it started out with a piece of code that looked like thisPool.addTask("Load "~info(), &update); Pool.addTask("Prefetch "~info(), &update); Pool.addTask("Prefetch "~info(), &update);
There's an idea for a language feature (off-the-wall and will never get in, but regardless!) class Pool { void addTask(char[][] names..., void delegate() dgs...) { // names and dgs should be the same length // pairs are in names[i] and dgs[i] } } Pool.addTask("Load " ~ info(), &update, "Prefetch " ~ info(), &update); Heee. I don't see, though, why you couldn't do void addTask(void delegate()[char[]] tasks) { ... } Pool.addTask(["Load " ~ info() : &update, "Prefetch " ~ info() : &update]);Clearly, the addTask is redundant here. I replaced it with[stuple("Load "~info(), &update), stuple...] /map/ &Pool.addTask;
Somebody in IRC pointed out that the stuple was redundant. He must feel horrible for indirectly causing this.["Load "~info, ...] /zip/ [&update, ...] /map/ &Pool.addTask;
Then I noticed the info calls were also redundant. It went kinda out of control after that. You know the result. :)
Redundancy is not much to worry about, especially if the result of removing it is entirely unreadable.
Oct 28 2007
downs wrote:Here's a question to the tango devs. I've been developing my own stackthreads (Fibers basically) library for a while now, and finally got it working a few days ago. Then we did some speed tests and discovered that it was about twice as slow as Tango's Fiber implementation. This prompted me to dig into the Fibers sourcecode, where I discovered that Fibers doesn't backup ECX/EDX on context switch, and the FP registers only on windows. Now I am confused. Could a knowledgeable tango dev kindly shed light on this issue? Why isn't it necessary to save all registers on a context switch? ---- Now for the anecdote. Today, at five in the morning, I wrote what I believe to be the first purely functional D code with as little redundancy as possible in the language.["Load ", "Prefetch ", "Prefetch "] /zip/ ([0, 1, 2] /map/ (&info
&concat!(string) /zip/ [&update, &prefetch /fix/ (currentStrip-1), &prefetch /fix/ (currentStrip-2)] /map/ &Pool.addTask; So I looked at it, and for some _strange_ reason my eyes suddenly started bleeding heavily. That's when I knew I had overdone it on the templates again. :)
In my best Hercules Grytpype-Thynne voice: "You silly, twisted boy." http://www.thegoonshow.net/quotes.asp
Oct 28 2007
downs Wrote:["Load ", "Prefetch ", "Prefetch "] /zip/ ([0, 1, 2] /map/ (&info
&concat!(string) /zip/ [&update, &prefetch /fix/ (currentStrip-1), &prefetch /fix/ (currentStrip-2)] /map/ &Pool.addTask;
Ewwww! That whole create-your-own infix operator thing *is* cool, no doubt, but IMO, it reduces readability. To me: map(array, &func); ... is more readable than... array /map/ &func; ... but maybe that's because I've never actually done any functional programming.
Oct 28 2007
Robert Fraser wrote:downs Wrote:["Load ", "Prefetch ", "Prefetch "] /zip/ ([0, 1, 2] /map/ (&info
&concat!(string) /zip/ [&update, &prefetch /fix/ (currentStrip-1), &prefetch /fix/ (currentStrip-2)] /map/ &Pool.addTask;
Ewwww! That whole create-your-own infix operator thing *is* cool, no doubt, but IMO, it reduces readability. To me: map(array, &func); ... is more readable than... array /map/ &func; ... but maybe that's because I've never actually done any functional programming.
The use of infix operators is unrelated to functional programming. Lisp doesn't have infix operators, for instance. map(array, &func) is every bit as "functional" as array /map/ &func. Or array.map(&func) for that matter. The functional style part comes from hiding the explicit procedural steps taken by the computer in functions like 'map', and by treating functions like 'func' more as data than code. --bb
Oct 28 2007
Reply to Downs,["Load ", "Prefetch ", "Prefetch "] /zip/ ( [0, 1, 2] /map/ ( &info /rfix/ currentComic /fix/ ( &subtract!(int) /fix/ currentStrip ) ) ) /map/ &concat!(string) /zip/ [ &update, &prefetch /fix/ (currentStrip-1), &prefetch /fix/ (currentStrip-2) ] /map/ &Pool.addTask;
how the heck is that parsing?? Is the /map/ actualy this: (A / B) / C; ??
Oct 28 2007
Reply to Benjamin,Reply to Downs,["Load ", "Prefetch ", "Prefetch "] /zip/ ( [0, 1, 2] /map/ ( &info /rfix/ currentComic /fix/ ( &subtract!(int) /fix/ currentStrip ) ) ) /map/ &concat!(string) /zip/ [ &update, &prefetch /fix/ (currentStrip-1), &prefetch /fix/ (currentStrip-2) ] /map/ &Pool.addTask;
(A / B) / C; ??
BTW if that is what you are doing, I may be force to do something totally EVIL to reclaim some ground in the "does nasty things with D" ranking. I have a few ideas though.... }:-) (BBBBWWWWAAAA HAA Haa haaa.....)
Oct 28 2007
BCS wrote:Reply to Benjamin, ...how the heck is that parsing?? Is the /map/ actualy this: (A / B) / C; ??
BTW if that is what you are doing, I may be force to do something totally EVIL to reclaim some ground in the "does nasty things with D" ranking. I have a few ideas though.... }:-) (BBBBWWWWAAAA HAA Haa haaa.....)
IIRC, map is basically defined like this: struct map { static map_ opDiv_r(T)(some_array_type!(T)); } struct map_ { some_array_type!(T) opDiv(T)(some_delegate_type!(T)); } Or something similar. -- Daniel
Oct 28 2007
Daniel Keep wrote:BCS wrote:Reply to Benjamin, ...how the heck is that parsing?? Is the /map/ actualy this: (A / B) / C; ??
totally EVIL to reclaim some ground in the "does nasty things with D" ranking. I have a few ideas though.... }:-) (BBBBWWWWAAAA HAA Haa haaa.....)
Looking forward to it! :) The world needs more weird and disturbing things. Shake it up.IIRC, map is basically defined like this: struct map { static map_ opDiv_r(T)(some_array_type!(T)); } struct map_ { some_array_type!(T) opDiv(T)(some_delegate_type!(T)); } Or something similar. -- Daniel
Yes exactly; though I have all the ugliness packed around in a template. Quote from the respective file:mixin(Operator!("map", " static if (is(ReturnType!(RHS)==void)) { foreach (ref entry; lhs) rhs(mixin(Value!(ElemType!(LHS), RHS,
} else { auto res=new ReturnType!(RHS)[lhs.length]; foreach (id, ref entry; res)
return res; } "));
Oct 28 2007
downs wrote:Daniel Keep wrote:BCS wrote:Reply to Benjamin, ...how the heck is that parsing?? Is the /map/ actualy this: (A / B) / C; ??
totally EVIL to reclaim some ground in the "does nasty things with D" ranking. I have a few ideas though.... }:-) (BBBBWWWWAAAA HAA Haa haaa.....)
Looking forward to it! :) The world needs more weird and disturbing things. Shake it up.IIRC, map is basically defined like this: struct map { static map_ opDiv_r(T)(some_array_type!(T)); } struct map_ { some_array_type!(T) opDiv(T)(some_delegate_type!(T)); } Or something similar. -- Daniel
Yes exactly; though I have all the ugliness packed around in a template.
Does it compile down to the same code as just a plain call to a map() function? (Or is there some other benefit that justifies the extra runtime cost and loss of readability?) --bb
Oct 29 2007
Bill Baxter wrote:Does it compile down to the same code as just a plain call to a map() function? (Or is there some other benefit that justifies the extra runtime cost and loss of readability?) --bb
Loss of readability is debatable. Personally I like it better, or else I wouldn't use it. :) The runtime cost might not be as high as you expect. Let us do a test.module test12; import std.stdio; import tools.base, tools.functional, tools.log; int add(int a, int b) { return a+b; } mixin(Operator!("add3", "return lhs+rhs;")); void main() { const long count=1_000_000_000; long test; int delegate(int, int) add1=(int a, int b) { return add(a, b); }; int function(int, int) add2=&add; logln("Function: ", time({ for (int i=0; i<count; ++i) test+=add(0,
logln("Delegate function: ", time({ for (int i=0; i<count; ++i)
logln("Function ... function: ", time({ for (int i=0; i<count; ++i)
logln("Infix function: ", time({ for (int i=0; i<count; ++i) test+=0
}
The results:gentoo-pc ~/d $ gdc test12.d -o test12 -O3 -frelease -Itools
/--[ Main Thread ]------ | Function: 0 -test 1000000000 | Delegate function: 7234 -test 2000000000 | Function ... function: 6415 -test -1294967296 | Infix function: 1501 -test -294967296 \-----------------------------
Hm ... interesting. I had expected the infix function to be inlined as well as the straight function. Ah well. Four times the speed of an un-inlined function is good enough for me. :)
Oct 29 2007
downs Wrote:["Load ", "Prefetch ", "Prefetch "] /zip/ ([0, 1, 2] /map/ (&info
&concat!(string) /zip/ [&update, &prefetch /fix/ (currentStrip-1), &prefetch /fix/ (currentStrip-2)] /map/ &Pool.addTask;
I too have developed a large functional-style lib for D, but I think D needs a bit more built-in support for it (the first class functions, allowing a better syntax, etc) and you need to program in a more tidy way :-) bearophile
Oct 29 2007









Daniel Keep <daniel.keep.lists gmail.com> 