Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterable arg is not inlineable #11602

Open
RblSb opened this issue Mar 8, 2024 · 0 comments
Open

Iterable arg is not inlineable #11602

RblSb opened this issue Mar 8, 2024 · 0 comments

Comments

@RblSb
Copy link
Member

RblSb commented Mar 8, 2024

Maybe it can be optimized to work like Array<T> without allocating iterator objects here?
Blocks optimization point of #11148

using Main;

class Main {
	static function main() new Main();

	final nums = [1, 2, 3];
	public function new() {
		final foo = nums.inlineExists(item -> item == 1);
		trace(foo);
		final foo2 = nums.inlineArrExists(item -> item == 1);
		trace(foo2);
	}

	public static inline function inlineExists<T>(it:Iterable<T>, f:(item:T) -> Bool):Bool {
		var result = false;
		for (v in it) {
			if (f(v)) {
				result = true;
				break;
			}
		}
		return result;
	}

	public static inline function inlineArrExists<T>(it:Array<T>, f:(item:T) -> Bool):Bool {
		var result = false;
		for (v in it) {
			if (f(v)) {
				result = true;
				break;
			}
		}
		return result;
	}
}

Js output:

	this.nums = [1,2,3];
	var result = false;
	var v = $getIterator(this.nums);
	while(v.hasNext()) if(v.next() == 1) {
		result = true;
		break;
	}
	console.log("src/Main.hx:9:",result);
	var it = this.nums;
	var result = false;
	var _g = 0;
	while(_g < it.length) if(it[_g++] == 1) {
		result = true;
		break;
	}
	console.log("src/Main.hx:11:",result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant