core icon indicating copy to clipboard operation
core copied to clipboard

Improve List.length performance

Open dullbananas opened this issue 5 years ago • 3 comments

dullbananas avatar Jul 26 '20 07:07 dullbananas

Out of interest: do you have a benchmark?

harrysarson avatar Jul 27 '20 10:07 harrysarson

Out of interest: do you have a benchmark?

I made a benchmark here. It tests a list with 50,000 items.

dullbananas avatar Jul 27 '20 16:07 dullbananas

Wow, I see a huge improvement. Looks like by removing the F2 wrappers we get a massive performance gain.

image

var $author$project$Main$currentlength = function (list) {
	return A3(
		$elm$core$List$foldl,
		F2(
			function (_v0, i) {
				return i + 1;
			}),
		0,
		list);
};
var $author$project$Main$lengthHelp0 = F2(
	function (list, acc) {
		lengthHelp0:
		while (true) {
			if (!list.b) {
				return acc;
			} else {
				var xs = list.b;
				var $temp$list = xs,
					$temp$acc = acc + 1;
				list = $temp$list;
				acc = $temp$acc;
				continue lengthHelp0;
			}
		}
	});
var $author$project$Main$newlength = function (list) {
	return A2($author$project$Main$lengthHelp0, list, 0);
};

harrysarson avatar Jul 27 '20 20:07 harrysarson