HaxeFoundation / HaxeFoundation/hashlink

Threads vs. GC

Open
#866 3 comments 1 reaction 2 assignees Claimed by @Simn View on GitHub
Dominant language
C
Stars
901
Forks
201
Avg merge
18h 9m
Merged PRs (30d)
8

Description

I've been working in threaded environments recently and the one target that I just can't get to perform well is HL. Here's an example of busy iterations that allocate strings:

```haxe
import sys.thread.Semaphore;
import sys.thread.Thread;
import haxe.Timer;

final iterationsPerThread = 1000;

var racyString:String;
var racyInt = 0;

function beBusyAndAllocate() {
var busy = 1000;
while (busy --> 0) {
racyInt++;
racyString = new String(racyInt + " " + busy);
}
}

function reset() {
racyString = null;
racyInt = 0;
}

function runSingleThreaded(numThreads:Int) {
final startTime = Timer.milliseconds();
reset();
final numIterations = numThreads * iterationsPerThread;
for (_ in 0...numIterations) {
beBusyAndAllocate();
}
trace('single-threaded ($numThreads)', Timer.milliseconds() - startTime);
}

function runMultiThreaded(numThreads:Int) {
final startTime = Timer.milliseconds();
reset();
final lock = new Semaphore(0);
for (_ in 0...numThreads) {
Thread.create(() -> {
for (_ in 0...iterationsPerThread) {
beBusyAndAllocate();
}
lock.release();
});
}
for (_ in 0...numThreads) {
lock.acquire();
}
trace('multi-threaded ($numThreads)', Timer.milliseconds() - startTime);
}

function main() {
for (numThreads in [2, 5, 10]) {
runSingleThreaded(numThreads);
runMultiThreaded(numThreads);
}
}
```

And some numbers (lower is better):

what | single-threaded | multi-threaded
--- | --- | ---
2 threads
jvm | 203 | 95
c++ | 676 | 356
hl | 1431 | 1959
5 threads
jvm | 391 | 228
c++ | 1701 | 450
hl | 3445 | 14156
10 threads
jvm | 593 | 362
c++ | 3414 | 569
hl | 7012 | 49752

This isn't great.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.