Skip to content

Commit

Permalink
WI #2236 Limit uses of ImmutableList<T> (#2575)
Browse files Browse the repository at this point in the history
  • Loading branch information
fm-117 committed Sep 21, 2023
1 parent be0a114 commit dcac1dd
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 599 deletions.
2 changes: 1 addition & 1 deletion TypeCobol.Test/Parser/Scanner/ScannerUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static string ScanLines(TokensLine[] tokensLines)
tokensLinesList.AddRange(tokensLines);

var initialScanState = new MultilineScanState(TextSourceInfo.EncodingForAlphanumericLiterals);
ScannerStep.ScanDocument(TextSourceInfo, tokensLinesList, CompilerOptions, CopyTextNameVariations, initialScanState);
ScannerStep.ScanDocument(TextSourceInfo, tokensLinesList.ToImmutable(), CompilerOptions, CopyTextNameVariations, initialScanState);

StringBuilder sbResult = new StringBuilder();
for (int i = 0; i < tokensLines.Length; i++)
Expand Down
6 changes: 3 additions & 3 deletions TypeCobol/Compiler/CodeModel/SymbolTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace TypeCobol.Compiler.CodeModel
{
public class SymbolTable
{
private static readonly IList<Section> EmptySectionList = new ImmutableList<Section>();
private static readonly IList<Paragraph> EmptyParagraphList = new ImmutableList<Paragraph>();
private static readonly IList<TypeDefinition> EmptyTypeDefinitionList = new ImmutableList<TypeDefinition>();
private static readonly IList<Section> EmptySectionList = ImmutableList<Section>.Empty;
private static readonly IList<Paragraph> EmptyParagraphList = ImmutableList<Paragraph>.Empty;
private static readonly IList<TypeDefinition> EmptyTypeDefinitionList = ImmutableList<TypeDefinition>.Empty;

public Scope CurrentScope { get; }
public SymbolTable EnclosingScope { get; }
Expand Down
10 changes: 5 additions & 5 deletions TypeCobol/Compiler/CompilationDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ private void ApplyTextChange(TextChange textChange, IList<DocumentChange<ICobolT
private void ShiftLines(int startIndex, int offset)
{
Debug.Assert(offset != 0);
using (var enumerator = compilationDocumentLines.GetEnumerator(startIndex, -1, false))
using (var enumerator = compilationDocumentLines.GetEnumerator(startIndex, false))
{
//Loop on every line that appear after target line and shift them according to given offset
while (enumerator.MoveNext())
Expand Down Expand Up @@ -697,6 +697,7 @@ public void RefreshTokensDocumentSnapshot()
// Create a new snapshot only if things changed since last snapshot
if (TokensDocumentSnapshot == null || TokensDocumentSnapshot.CurrentVersion != currentTokensLinesVersion)
{
// Freeze compilationDocumentLines into an ImmutableList as further steps won't need to modify it
TokensDocumentSnapshot = new TokensDocument(TextSourceInfo, textLinesVersionForCurrentTokensLines, currentTokensLinesVersion, compilationDocumentLines.ToImmutable());
}
}
Expand Down Expand Up @@ -755,7 +756,7 @@ public void RefreshProcessedTokensDocumentSnapshot()
if (tokensDocument != null)
{
// Process all lines of the document for the first time
PreprocessorStep.ProcessDocument(this, ((ImmutableList<CodeElementsLine>)tokensDocument.Lines), _documentImporter, perfStatsForParserInvocation, out missingCopies);
PreprocessorStep.ProcessDocument(this, (ISearchableReadOnlyList<ProcessedTokensLine>)tokensDocument.Lines, _documentImporter, perfStatsForParserInvocation, out missingCopies);

// Create the first processed tokens document snapshot
ProcessedTokensDocumentSnapshot = CreateProcessedTokensDocument(new DocumentVersion<IProcessedTokensLine>(this), (ISearchableReadOnlyList<CodeElementsLine>) tokensDocument.Lines);
Expand All @@ -768,9 +769,8 @@ public void RefreshProcessedTokensDocumentSnapshot()
}
else
{
ImmutableList<CodeElementsLine>.Builder processedTokensDocumentLines = ((ImmutableList<CodeElementsLine>) tokensDocument.Lines).ToBuilder();
IList<DocumentChange<IProcessedTokensLine>> documentChanges = PreprocessorStep.ProcessChanges(this,
processedTokensDocumentLines, tokensLineChanges, PrepareDocumentLineForUpdate,
(ISearchableReadOnlyList<ProcessedTokensLine>)tokensDocument.Lines, tokensLineChanges, PrepareDocumentLineForUpdate,
_documentImporter, perfStatsForParserInvocation, out missingCopies);

// Create a new version of the document to track these changes
Expand All @@ -783,7 +783,7 @@ public void RefreshProcessedTokensDocumentSnapshot()
currentProcessedTokensLineVersion = currentProcessedTokensLineVersion.next;

// Update the processed tokens document snapshot
ProcessedTokensDocumentSnapshot = CreateProcessedTokensDocument(currentProcessedTokensLineVersion, processedTokensDocumentLines.ToImmutable());
ProcessedTokensDocumentSnapshot = CreateProcessedTokensDocument(currentProcessedTokensLineVersion, (ISearchableReadOnlyList<CodeElementsLine>)tokensDocument.Lines);
}

ProcessedTokensDocument CreateProcessedTokensDocument(DocumentVersion<IProcessedTokensLine> version, ISearchableReadOnlyList<CodeElementsLine> lines)
Expand Down
8 changes: 3 additions & 5 deletions TypeCobol/Compiler/CompilationUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void RefreshCodeElementsDocumentSnapshot()
CodeElementsParserStep.ParseDocument(processedTokensDocument, CompilerOptions, perfStatsForParserInvocation);

// Create the first code elements document snapshot
CodeElementsDocumentSnapshot = new CodeElementsDocument(processedTokensDocument, new DocumentVersion<ICodeElementsLine>(this), ((ImmutableList<CodeElementsLine>)processedTokensDocument.Lines));
CodeElementsDocumentSnapshot = new CodeElementsDocument(processedTokensDocument, new DocumentVersion<ICodeElementsLine>(this), (ISearchableReadOnlyList<ICodeElementsLine>)processedTokensDocument.Lines);
}
}
else
Expand All @@ -117,8 +117,7 @@ public void RefreshCodeElementsDocumentSnapshot()
currentCodeElementsLinesVersion = currentCodeElementsLinesVersion.next;

// Update the code elements document snapshot
ImmutableList<CodeElementsLine>.Builder codeElementsDocumentLines = ((ImmutableList<CodeElementsLine>)processedTokensDocument.Lines).ToBuilder();
CodeElementsDocumentSnapshot = new CodeElementsDocument(processedTokensDocument, currentCodeElementsLinesVersion, codeElementsDocumentLines.ToImmutable());
CodeElementsDocumentSnapshot = new CodeElementsDocument(processedTokensDocument, currentCodeElementsLinesVersion, (ISearchableReadOnlyList<ICodeElementsLine>)processedTokensDocument.Lines);
}

// Stop perf measurement
Expand Down Expand Up @@ -258,10 +257,9 @@ public void ProduceTemporarySemanticDocument()
var customAnalyzers = _analyzerProvider?.CreateSyntaxDrivenAnalyzers(CompilerOptions, TextSourceInfo);

// Program and Class parsing is not incremental : the objects are rebuilt each time this method is called
//TODO cast to ImmutableList<CodeElementsLine> sometimes fails here
ProgramClassParserStep.CupParseProgramOrClass(
TextSourceInfo,
(ImmutableList<CodeElementsLine>) codeElementsDocument.Lines,
(ISearchableReadOnlyList<CodeElementsLine>) codeElementsDocument.Lines,
CompilerOptions,
CustomSymbols,
perfStatsForParserInvocation,
Expand Down

0 comments on commit dcac1dd

Please sign in to comment.