Skip to content

Threads vs. GC #866

@Simn

Description

@Simn

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:

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions