On Saturday, 24 July 2021 at 09:17:47 UTC, Stefan Koch wrote:
On Wednesday, 21 July 2021 at 22:51:38 UTC, hanabi1224 wrote:
Hi, I'm new to D lang and encounter some performance issues with fiber, not sure if there's something obviously wrong with my code.


There is your problem.
    auto scheduler = new FiberScheduler;

The Fiber scheduler will spawn a new fiber for every job.
It will not use a fiber pool. Spawning a new fiber is expensive because of the stack allocation for it. Also if I recall correctly it will run single-threaded but I am not 100% sure on that. Just have a look at the running processes ... if you just see one than you are single threaded.

I get 8->3 seconds using vibe's fiber scheduler, which still isn't competitive with Elixir.

```d
--- primes.d    2021-07-24 21:37:46.633053839 -0500
+++ primesv1.d  2021-07-24 21:35:50.843053425 -0500
@@ -1,16 +1,19 @@
 /++ dub.sdl:
+    dependency "vibe-core" version="~>1.16.0"
  +/
-import std;
-import core.stdc.stdlib : exit;
+import std.stdio, std.conv;
+import vibe.core.concurrency;

 __gshared Tid mainTid;
 __gshared bool terminated = false;

 const int mailBoxSize = 1;

+extern(C) void _exit(int status);
+
 void main(string[] args) {
     auto n = args.length > 1 ? args[1].to!int() : 10;
-    auto scheduler = new FiberScheduler;
+    setConcurrencyPrimitive(ConcurrencyPrimitive.workerTask);
     scheduler.start({
         mainTid = thisTid();
         setMaxMailboxSize(mainTid, n, OnCrowding.throwException);
@@ -22,7 +25,7 @@
             writeln(prime);
         }
         terminated = true;
-        exit(0);
+        _exit(0);
     });
 }
```

build:

```
dub build --compiler=ldc -brelease --single primesv1.d
```

I think this is just a very goroutine-friendly test that relies on constantly spawning fibers and abusing message-passing rather than architecting out the concurrent parts of your program and how they should communicate. std.parallel's more appropriate in D.

Reply via email to