1
Vote

Parsing error for different version of dotnet

description

I'm running Parser.Parse to try to parse a line of code, but getting different results on different machines. On one machine it fails all the time but another one succeeds. I traced the code on both machines to try to figure out why, the difference happens at line 81 of ScannerDataBuilder:
      if (_grammarData.NoPrefixTerminals.Count > 0) {
        //copy them to Scanner data
        _data.NoPrefixTerminals.AddRange(_grammarData.NoPrefixTerminals);
        // Sort in reverse priority order
-->    _data.NoPrefixTerminals.Sort(Terminal.ByPriorityReverse);
        //Now add Fallback terminals to every list, then sort lists by reverse priority
        // so that terminal with higher priority comes first in the list
        foreach (TerminalList list in _data.TerminalsLookup.Values) 
          foreach (var ft in _data.NoPrefixTerminals)
            if (!list.Contains(ft))
              list.Add(ft);
      }//if count > 0
For some reason on the failed machine the DepthLimitedQuickSort was used in Array.Sort internally, but on the succeeded machine IntrospectiveSort was used.

So for the all terminals that have Priority 0, it's getting different orders for different sort methods.

I peeked into ArraySortHelper:
        if (BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)
          ArraySortHelper<TKey, TValue>.IntrospectiveSort(keys, values, index, length, comparer);
        else
          ArraySortHelper<TKey, TValue>.DepthLimitedQuickSort(keys, values, index, length + index - 1, comparer, 32);
It seems having something to do with the dotnet versions. On both failed and succeeded machines I have 4.5.1 installed and set the projects all to be built against 4.5 though.

Maybe it's better to have a different solution for _data.NoPrefixTerminals.Sort? so the sorting doesn't rely on the version of dotnet.

Or I don't know if there is a secret flag somewhere that I can set to make sure the sort always sort it predictably?

comments

rivantsov wrote Nov 5, 2014 at 4:38 AM

yeah, looks like one version uses 'stable' sort, and another one not so stable (stable is when original order of 'same-key' elements is preserved). But in any way, relying on this 'stability' of sort order is a wrong thing to do, that's why you have Priority, to explicitly set order. Although I do admit that have different behavior on different versions of .NET is a wrong thing, somehow things should be 'stabilized' and be the same. Will look into this for the new version