Skip to content

Commit d8505d9

Browse files
authored
DYN-1722 (#9578) (#9597)
* DYN-1722 * Variable change name * Update the callsite.cs * Modifying the equals function. * adding comments * Test comments * Changing variable name * Adding zipAlgorithm check and comments. * More Comments. (cherry picked from commit 62e1b56)
1 parent 58bcb02 commit d8505d9

File tree

5 files changed

+133
-34
lines changed

5 files changed

+133
-34
lines changed

src/Engine/ProtoCore/Lang/CallSite.cs

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -822,22 +822,44 @@ private FunctionEndPoint GetLooseCompliantFEP(
822822
return compliantTarget;
823823
}
824824

825+
826+
/// <summary>
827+
/// This helper function checks if the current replication option is
828+
/// similar to the previous option but of a higher rank.
829+
/// Checks if the first entry is same in both the options and the current options count is more.
830+
/// </summary>
831+
/// <returns>Returns true or false based on the condition described above.
832+
/// </returns>
833+
private Boolean IsSimilarOptionButOfHigherRank(List<ReplicationInstruction> oldOption, List<ReplicationInstruction> newOption)
834+
{
835+
if (oldOption.Count > 0 && newOption.Count > 0 && oldOption.Count < newOption.Count)
836+
{
837+
if (oldOption[0].Equals(newOption[0]))
838+
return true;
839+
}
840+
return false;
841+
}
842+
825843
private void ComputeFeps(
826844
Context context,
827845
List<StackValue> arguments,
828846
FunctionGroup funcGroup,
829847
List<ReplicationInstruction> instructions,
830848
StackFrame stackFrame,
831849
RuntimeCore runtimeCore,
832-
out List<FunctionEndPoint> resolvesFeps,
850+
out List<FunctionEndPoint> resolvedFeps,
833851
out List<ReplicationInstruction> replicationInstructions)
834852
{
853+
replicationInstructions = null;
854+
resolvedFeps = null;
855+
var matchFound = false;
856+
835857
#region Case 1: Replication guide with exact match
836858
{
837859
FunctionEndPoint fep = GetCompleteMatchFunctionEndPoint(context, arguments, funcGroup, instructions, stackFrame, runtimeCore);
838860
if (fep != null)
839861
{
840-
resolvesFeps = new List<FunctionEndPoint>() { fep };
862+
resolvedFeps = new List<FunctionEndPoint>() { fep };
841863
replicationInstructions = instructions;
842864
return;
843865
}
@@ -855,13 +877,17 @@ private void ComputeFeps(
855877
HashSet<FunctionEndPoint> lookups;
856878
if (funcGroup.CanGetExactMatchStatics(context, reducedParams, stackFrame, runtimeCore, out lookups))
857879
{
858-
//Otherwise we have a cluster of FEPs that can be used to dispatch the array
859-
resolvesFeps = new List<FunctionEndPoint>(lookups);
860-
replicationInstructions = replicationOption;
861-
return;
880+
if (replicationInstructions == null || IsSimilarOptionButOfHigherRank(replicationInstructions, replicationOption))
881+
{
882+
//Otherwise we have a cluster of FEPs that can be used to dispatch the array
883+
resolvedFeps = new List<FunctionEndPoint>(lookups);
884+
replicationInstructions = replicationOption;
885+
matchFound = true;
886+
}
862887
}
863888
}
864-
889+
if (matchFound)
890+
return;
865891
}
866892
#endregion
867893

@@ -870,7 +896,7 @@ private void ComputeFeps(
870896
FunctionEndPoint compliantTarget = GetCompliantFEP(context, arguments, funcGroup, instructions, stackFrame, runtimeCore);
871897
if (compliantTarget != null)
872898
{
873-
resolvesFeps = new List<FunctionEndPoint>() { compliantTarget };
899+
resolvedFeps = new List<FunctionEndPoint>() { compliantTarget };
874900
replicationInstructions = instructions;
875901
return;
876902
}
@@ -886,11 +912,13 @@ private void ComputeFeps(
886912
FunctionEndPoint compliantTarget = GetCompliantFEP(context, arguments, funcGroup, replicationOption, stackFrame, runtimeCore);
887913
if (compliantTarget != null)
888914
{
889-
resolvesFeps = new List<FunctionEndPoint>() { compliantTarget };
915+
resolvedFeps = new List<FunctionEndPoint>() { compliantTarget };
890916
replicationInstructions = replicationOption;
891-
return;
917+
matchFound = true;
892918
}
893919
}
920+
if (matchFound)
921+
return;
894922
}
895923
}
896924

@@ -906,7 +934,7 @@ private void ComputeFeps(
906934
FunctionEndPoint compliantTarget = GetCompliantFEP(context, arguments, funcGroup, replicationOption, stackFrame, runtimeCore, true);
907935
if (compliantTarget != null)
908936
{
909-
resolvesFeps = new List<FunctionEndPoint>() { compliantTarget };
937+
resolvedFeps = new List<FunctionEndPoint>() { compliantTarget };
910938
replicationInstructions = replicationOption;
911939
return;
912940
}
@@ -921,15 +949,17 @@ private void ComputeFeps(
921949
FunctionEndPoint compliantTarget = GetLooseCompliantFEP(context, arguments, funcGroup, replicationOption, stackFrame, runtimeCore);
922950
if (compliantTarget != null)
923951
{
924-
resolvesFeps = new List<FunctionEndPoint>() { compliantTarget };
952+
resolvedFeps = new List<FunctionEndPoint>() { compliantTarget };
925953
replicationInstructions = replicationOption;
926-
return;
954+
matchFound = true;
927955
}
928956
}
957+
if (matchFound)
958+
return;
929959
}
930960
#endregion
931961

932-
resolvesFeps = new List<FunctionEndPoint>();
962+
resolvedFeps = new List<FunctionEndPoint>();
933963
replicationInstructions = instructions;
934964
}
935965

src/Engine/ProtoCore/Lang/Replication/ReplicationInstruction.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using System.Collections.Generic;
2-
using System.Text;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
34

45
namespace ProtoCore.Lang.Replication
56
{
@@ -44,5 +45,26 @@ public override string ToString()
4445
}
4546

4647
}
48+
49+
public Boolean Equals(ReplicationInstruction oldOption) {
50+
51+
if (this.Zipped == oldOption.Zipped && this.ZipAlgorithm == oldOption.ZipAlgorithm)
52+
{
53+
if (this.ZipIndecies == null && oldOption.ZipIndecies == null)
54+
return true;
55+
56+
if (this.ZipIndecies != null && oldOption.ZipIndecies != null)
57+
{
58+
// Fastest way to compare all elements of 2 lists.
59+
// Excluding one list from another list and checking if the leftover lists is empty or not.
60+
// https://stackoverflow.com/questions/12795882/quickest-way-to-compare-two-list
61+
var currentExcludesOldList = this.ZipIndecies.Except(oldOption.ZipIndecies).ToList();
62+
var oldExcludescurrentList = oldOption.ZipIndecies.Except(this.ZipIndecies).ToList();
63+
64+
return !currentExcludesOldList.Any() && !oldExcludescurrentList.Any();
65+
}
66+
}
67+
return false;
68+
}
4769
}
4870
}

test/Engine/ProtoTest/Associative/BuiltinMethodsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ public void TestTryGetValuesFromDictionary08()
10691069
thisTest.Verify("r", null);
10701070
}
10711071

1072-
[Test, Category("Failure")]
1072+
[Test]
10731073
public void TestTryGetValuesFromDictionary09()
10741074
{
10751075
string code = @"

test/Engine/ProtoTest/Associative/MethodResolution.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -396,21 +396,5 @@ def qux2()
396396
thisTest.Verify("z1", 22);
397397
thisTest.Verify("z2", 22);
398398
}
399-
400-
[Test]
401-
public void TestEmptyNestedListsForMethodResolution()
402-
{
403-
string code = @"import(""FFITarget.dll"");
404-
pt = DummyPoint2D.ByCoordinates(0,0);
405-
l = [[],[pt]];
406-
px1 = DummyPoint2D.X(l);
407-
pt = DummyPoint2D.ByCoordinates(0,0);
408-
l2 = [[pt],[]];
409-
px2 = DummyPoint2D.X(l2);
410-
";
411-
var mirror = thisTest.RunScriptSource(code);
412-
thisTest.Verify("px1", new object[] { new object[] { }, new object[] { 0 } });
413-
thisTest.Verify("px2", new object[] { new object[] { 0 }, new object[] { } });
414-
}
415399
}
416400
}

test/Engine/ProtoTest/TD/Associative/Replication.cs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4926,7 +4926,70 @@ public void TestReplicationInInlineConditional()
49264926
thisTest.RunScriptSource(code);
49274927
thisTest.Verify("r", new object[] { 2, 5, 7 });
49284928
}
4929-
}
49304929

4930+
// This tests the case 2 block in the computeFeps method(CallSite.cs line:943)
4931+
[Test]
4932+
public void TestReplicationWithEmptyListInNestedLists()
4933+
{
4934+
string code = @"import(""FFITarget.dll"");
4935+
pt = DummyPoint2D.ByCoordinates(0,0);
4936+
l = [[],[pt]];
4937+
px1 = DummyPoint2D.X(l);
4938+
pt = DummyPoint2D.ByCoordinates(0,0);
4939+
l2 = [[pt],[]];
4940+
px2 = DummyPoint2D.X(l2);
4941+
";
4942+
var mirror = thisTest.RunScriptSource(code);
4943+
thisTest.Verify("px1", new object[] { new object[] { }, new object[] { 0 } });
4944+
thisTest.Verify("px2", new object[] { new object[] { 0 }, new object[] { } });
4945+
}
4946+
4947+
// This tests the case 4 block in the computeFeps method(CallSite.cs line:943)
4948+
[Test]
4949+
public void TestReplicationWithNullElementInNestedLists()
4950+
{
4951+
string code = @"import(""FFITarget.dll"");
4952+
pt = DummyPoint2D.ByCoordinates(0,0);
4953+
l = [null,[pt]];
4954+
px1 = DummyPoint2D.X(l);
4955+
pt = DummyPoint2D.ByCoordinates(0,0);
4956+
l2 = [[pt],null];
4957+
px2 = DummyPoint2D.X(l2);
4958+
";
4959+
4960+
var mirror = thisTest.RunScriptSource(code);
4961+
thisTest.Verify("px1", new object[] { null, new object[] { 0 } });
4962+
thisTest.Verify("px2", new object[] { new object[] { 0 }, null });
4963+
}
4964+
4965+
// This tests the case:6 block in the computeFeps method(CallSite.cs line:943)
4966+
// input example for case 6: l4 and l5 lists.
4967+
[Test]
4968+
public void TestReplicationWithArraysOfDifferentRanks()
4969+
{
4970+
string code = @"import(""FFITarget.dll"");
4971+
t1 = [1, [2]];
4972+
t2 = t1 + 5;
4973+
pt = DummyPoint2D.ByCoordinates(0,0);
4974+
l1 = [[[pt]],[pt]];
4975+
px1 = DummyPoint2D.X(l1);
4976+
l2 = [[pt],[[pt]]];
4977+
px2 = DummyPoint2D.X(l2);
4978+
l3 = [[null],[[pt]]];
4979+
px3 = DummyPoint2D.X(l3);
4980+
l4 = [3.3,[pt],[[pt]]];
4981+
px4 = DummyPoint2D.X(l4);
4982+
l5 = [""test"",[[pt]],[pt]];
4983+
px5 = DummyPoint2D.X(l5);
4984+
";
4985+
var mirror = thisTest.RunScriptSource(code);
4986+
thisTest.Verify("t2", new object[] { 6, new object[] { 7 } });
4987+
thisTest.Verify("px1", new object[] { new object[] { new object[] { 0 } }, new object[] { 0 } });
4988+
thisTest.Verify("px2", new object[] { new object[] { 0 }, new object[] { new object[] { 0 } } });
4989+
thisTest.Verify("px3", new object[] { new object[] { null }, new object[] { new object[] { 0 } } });
4990+
thisTest.Verify("px4", new object[] { null, new object[] { 0 }, new object[] { new object[] { 0 } } });
4991+
thisTest.Verify("px5", new object[] { null, new object[] { new object[] { 0 } }, new object[] { 0 } });
4992+
}
4993+
}
49314994
}
49324995

0 commit comments

Comments
 (0)