Skip to content

List.fold IL simplification #3982

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

Merged
merged 1 commit into from
Nov 22, 2017
Merged

List.fold IL simplification #3982

merged 1 commit into from
Nov 22, 2017

Conversation

ncave
Copy link
Contributor

@ncave ncave commented Nov 20, 2017

before:

public static TState Fold<T, TState>(FSharpFunc<TState, FSharpFunc<T, TState>> folder, TState state, FSharpList<T> list)
{
	if (list.tail == null)
	{
		return state;
	}
	OptimizedClosures.FSharpFunc<TState, T, TState> f = OptimizedClosures.FSharpFunc<TState, T, TState>.Adapt(folder);
	return ListModule.loop@219-29<T, TState>(f, state, list);
}

internal static TState loop@219-29<T, TState>(OptimizedClosures.FSharpFunc<TState, T, TState> f, TState s, FSharpList<T> xs)
{
	while (xs.tail != null)
	{
		FSharpList<T> fsharpList = xs;
		FSharpList<T> tail = fsharpList.tail;
		T head = fsharpList.head;
		OptimizedClosures.FSharpFunc<TState, T, TState> fsharpFunc = f;
		TState tstate = f.Invoke(s, head);
		xs = tail;
		s = tstate;
		f = fsharpFunc;
	}
	return s;
}

after:

public static TState Fold<T, TState>(FSharpFunc<TState, FSharpFunc<T, TState>> folder, TState state, FSharpList<T> list)
{
	if (list.tail == null)
	{
		return state;
	}
	OptimizedClosures.FSharpFunc<TState, T, TState> fsharpFunc = OptimizedClosures.FSharpFunc<TState, T, TState>.Adapt(folder);
	TState tstate = state;
	FSharpList<T> fsharpList = list;
	for (FSharpList<T> tail = fsharpList.tail; tail != null; tail = fsharpList.tail)
	{
		T head = fsharpList.head;
		tstate = fsharpFunc.Invoke(tstate, head);
		fsharpList = tail;
	}
	return tstate;
}

Copy link
Contributor

@KevinRansom KevinRansom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this

@dsyme
Copy link
Contributor

dsyme commented Nov 21, 2017

Super, thanks

@KevinRansom KevinRansom merged commit 9ef05a7 into dotnet:master Nov 22, 2017
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

Successfully merging this pull request may close these issues.

3 participants