Skip to content

Commit

Permalink
WI #2619 Optimize the StartPosition property (#2620)
Browse files Browse the repository at this point in the history
  • Loading branch information
fm-117 committed Mar 26, 2024
1 parent bb4a488 commit 39ec0f9
Showing 1 changed file with 36 additions and 40 deletions.
76 changes: 36 additions & 40 deletions TypeCobol/Compiler/Nodes/Data.cs
Expand Up @@ -528,61 +528,57 @@ public virtual long StartPosition
{
get
{
if (_startPosition.HasValue)
if (!_startPosition.HasValue)
{
return _startPosition.Value;
ComputeStartPosition();
}

if (this is DataRedefines node)
System.Diagnostics.Debug.Assert(_startPosition.HasValue);
return _startPosition.Value;

void ComputeStartPosition()
{
//Get the start position from the node it redefines.
var result = node.RedefinedVariable;
if (result != null)
if (this is DataRedefines node)
{
_startPosition = result.StartPosition + SlackBytes;
return _startPosition.Value;
//Get the start position from the node it redefines.
var result = node.RedefinedVariable;
if (result != null)
{
_startPosition = result.StartPosition + SlackBytes;
return;
}

// Redefined variable does not exist -> handle node as a DataDescription
}

// Redefined variable does not exist -> handle node as a DataDescription
}
if (Parent is DataSection)
{
_startPosition = 1;
return;
}

if (Parent is DataSection)
{
_startPosition = 1;
}
else
{
//Searching for the first sibling with specified physical position, preceeding the current node.
for (int i = 0; i < Parent.Children.Count; i++)
//Searching for the first sibling with specified physical position, preceding the current node.
int siblingIndex = Parent.ChildIndex(this) - 1;
if (siblingIndex >= 0)
{
Node sibling = Parent.Children[i];
Node sibling = Parent.Children[siblingIndex];

if (i == Parent.ChildIndex(this) - 1)
//Looks further up if the first position encountered is from a DataRedefines node with an existing redefined variable.
while (sibling is DataRedefines dataRedefines && dataRedefines.RedefinedVariable != null)
{
int siblingIndex = i;
//Looks further up if the first position encountered is from a DataRedefines node with an existing redefined variable.
while (sibling is DataRedefines dataRedefines && dataRedefines.RedefinedVariable != null)
{
sibling = Parent.Children[siblingIndex - 1];

siblingIndex--;

}

DataDefinition siblingDefinition = (DataDefinition)sibling;
//Add 1 for the next free Byte in memory
_startPosition = Math.Max(siblingDefinition.GetBiggestRedefines()?.PhysicalPosition ?? 0, siblingDefinition.PhysicalPosition) + 1 + SlackBytes;

sibling = Parent.Children[--siblingIndex];
}

DataDefinition siblingDefinition = (DataDefinition)sibling;
//Add 1 for the next free Byte in memory
_startPosition = Math.Max(siblingDefinition.GetBiggestRedefines()?.PhysicalPosition ?? 0, siblingDefinition.PhysicalPosition) + 1 + SlackBytes;
return;
}
if (_startPosition == null)
{
_startPosition = (Parent as DataDefinition)?.StartPosition;
}
}

return _startPosition ?? 0;
// No previous data at same level, use position of parent
// Default to 0, but it means we could not compute the actual start position
_startPosition = (Parent as DataDefinition)?.StartPosition ?? 0;
}
}
}

Expand Down

0 comments on commit 39ec0f9

Please sign in to comment.