D talk at Silicon Valley ACCU
Ali Çehreli via Digitalmars-d-announce
digitalmars-d-announce at puremagic.com
Mon May 18 11:35:04 PDT 2015
On 05/18/2015 04:57 AM, "Per =?UTF-8?B?Tm9yZGzDtnci?=
<per.nordlow at gmail.com>" wrote:
> On Monday, 18 May 2015 at 09:12:50 UTC, Per Nordlöw wrote:
>> On Monday, 18 May 2015 at 08:44:30 UTC, Per Nordlöw wrote:
>>> On Tuesday, 12 May 2015 at 00:42:28 UTC, Ali Çehreli wrote:
>>>> http://www.meetup.com/SFBay-Association-of-C-C-Users/events/218992449/
>>>
>>> When will the video be available online?
Unfortunately, this presentation was not recorded. To be honest, it was
a little too rushed for my liking. However, the discussions by the
audience were high quality and there were good questions.
>> Is there any significant space- and time-overhead in being lazy and
>> choosing a fiber-based solution to convert a function to a range
>> instead of converting it to a standard D range?
A fiber's stack is allocated from the heap. That's one overhead. The
rest of the CPU register manipulations are comparable to a function call.
https://github.com/D-Programming-Language/druntime/blob/master/src/core/thread.d#L3584
I am not aware of any compiler tricks that can see through fibers either.
So, for simple tasks like Fibonacci series, there is no need to use a
fiber and it may be hurtful.
> void fibonacciSeries()
> {
> int current = 0; // <-- Not a parameter anymore
> int next = 1;
> while (true)
> {
> current.yield; // return
Yeah, that yields an int...
> const nextNext = current + next;
> current = next;
> next = nextNext;
> }
> }
>
> unittest
> {
> import std.concurrency: yield, Generator;
> auto series = new Generator!int(&fibonacciSeries);
... which we had to spell out up there.
> Could yet another function qualifier (or extended attribute) do the job
> of checking this at compile time?
Yeah, there is nothing that can be done at language level because yield
is not a keyword. However, as you say, a UDA can make it a little
better. Here is a rough version:
struct Yields
{
string type;
}
@Yields("int") // <-- SPECIFIED HERE
void fibonacciSeries()
{
// ...
}
auto generator(alias func)()
{
import std.format : format;
foreach (attr; __traits(getAttributes, func)) {
import std.traits : isInstanceOf;
static if (is (typeof(attr) == Yields)) {
mixin (format("alias YieldedType = %s;", attr.type));
import std.concurrency: Generator;
return new Generator!YieldedType(&func);
}
}
assert(false, format("%s does not have a Yields attribute",
func.stringof));
}
unittest
{
import std.stdio;
import std.range: take;
auto series = generator!fibonacciSeries; // <-- THIS TIME, NO 'int'
writefln("%(%s, %)", series.take(10));
}
It needs to be cleaned up. At least, the assert should be converted to a
static assert. Still though, there is no guarantee that "int" matches
what is yielded.
Ali
More information about the Digitalmars-d-announce
mailing list