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

optimized some counting so that we don't enumerate multiple times #2237

Draft
wants to merge 4 commits into
base: release-7.x
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -456,19 +456,18 @@ private IEdmVocabularyAnnotatable ComputeTarget()
private static IEdmOperationImport FindParameterizedOperationImport(string parameterizedName, Func<string, IEnumerable<IEdmOperationImport>> findFunctions, Func<IEnumerable<IEdmOperationImport>, IEdmOperationImport> ambiguityCreator)
{
IEnumerable<IEdmOperationImport> matchingFunctions = findFunctions(parameterizedName);
if (!matchingFunctions.Any())
var length = TryCount(matchingFunctions, out var value);
if (length == 0)
{
return null;
}

if (matchingFunctions.Count() == 1)
else if (length == 1)
{
return matchingFunctions.First();
return value;
}
else
{
IEdmOperationImport ambiguous = ambiguityCreator(matchingFunctions);
return ambiguous;
return ambiguityCreator(matchingFunctions);
}
}

Expand All @@ -484,19 +483,18 @@ private IEdmOperation FindParameterizedOperation(string parameterizedName, Func<
string name = parameterizedName.Substring(0, openParen);
string[] parameters = parameterizedName.Substring(openParen + 1, closeParen - (openParen + 1)).Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
IEnumerable<IEdmOperation> matchingFunctions = this.FindParameterizedOperationFromList(findFunctions(name).Cast<IEdmOperation>(), parameters);
if (!matchingFunctions.Any())
var length = TryCount(matchingFunctions, out var value);
if (length == 0)
{
return null;
}

if (matchingFunctions.Count() == 1)
else if (length == 1)
{
return matchingFunctions.First();
return value;
}
else
{
IEdmOperation ambiguous = ambiguityCreator(matchingFunctions);
return ambiguous;
return ambiguityCreator(matchingFunctions);
}
}

Expand Down Expand Up @@ -577,5 +575,31 @@ private IEnumerable<IEdmOperation> FindParameterizedOperationFromList(IEnumerabl

return matchingOperations;
}

private static int TryCount<T>(IEnumerable<T> source, out T value)
{
//// TODO check build from https://github.com/OData/odata.net/pull/2237
//// TODO benchmark this
//// TODO ensure that there are existing tests covering this
//// TODO add prepend stuff so that you are sure to enumerate only once?
using (var enumerator = source.GetEnumerator())
{
int length;
for (length = 0; length < 2 && enumerator.MoveNext(); ++length)
{
}

if (length == 1)
{
value = enumerator.Current;
Copy link
Contributor

Choose a reason for hiding this comment

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

If length ==1, enumerator.Current will be default/null, right? If length is 1 , we are trying to return the first funtion, right.

}
else
{
value = default(T);
}

return length;
}
}
}
}