I have been translating a solution from C# into VB.Net, because I know more VB and wanted to understand it better. I have a version up and running that replicates the C# performance with small input files; however with an input file over ~8MB the memory usage skyrockets and it will keep chugging until it consumes all memory. This is a problem, because the expected use case includes input files over 10MB, up to maybe 20MB.
I've run the code through the Visual Studio profiler on smaller files, and the below function is doing almost all the gruntwork (80%ish). The rest of the class is on paste-bin here if it helps.
The key question, am I doing anything obviously idiotic, that scales really badly, or is substantially different between VB.Net and C#? (C# code, for comparison, is here)
Public Function BuildSchedule() As Schedule
Dim cifSchedule = New Schedule()
Try
Dim tiploc_1 As Tiploc = Tiploc.GetInstance()
For i As Integer = 0 To _filelines.Count - 1
Dim stopLocation As String
Select Case _filelines(i).Substring(0, 2)
Case "BS"
If True Then
Dim passStops As String() = {"1", "2"}
If passStops.Contains(_filelines(i).Substring(32, 1)) = False OrElse _filelines(i).Substring(2, 1) = "D" Then
Dim [stop] As Integer = _filelines.Count()
Dim newi As Integer = 0
For j As Integer = i + 1 To [stop] - 1
If _filelines(j).Substring(0, 2) = "BS" Then
[stop] = j
newi = j - 1
End If
Next
i = newi
Exit Select
End If
BuildTrain(cifSchedule, i)
Exit Select
End If
Case "LO"
If True Then
Const stopType As String = "Origin"
stopLocation = _filelines(i).Substring(2, 7).Trim()
stopLocation = tiploc_1.getFullName(stopLocation)
Dim publicDeparture As DateTime = DateTime.ParseExact(_filelines(i).Substring(15, 4), "HHmm", CultureInfo.InvariantCulture)
Dim workingDeparture As DateTime = workingTimes(i, 10)
Dim platform As String = _filelines(i).Substring(19, 3).Trim()
Dim line As String = _filelines(i).Substring(22, 3).Trim()
Dim allowances As Double = 0.0
'Engineering Allowance
allowances = allowancesAdd(i, 25)
'Pathing Allowance
allowances = allowancesAdd(i, 27)
'Performance Allowance
allowances = allowancesAdd(i, 41)
Dim last As Integer = cifSchedule.TrainArray.Count() - 1
cifSchedule.TrainArray(last).AddLocation(New Location(stopType, stopLocation,
publicDeparture, workingDeparture,
platform, line, allowances))
Exit Select
End If
Case "LI"
If True Then
Dim validStops As String() = {"T", "U", "D"}
If validStops.Contains(_filelines(i).Substring(42, 1)) = False Then
Else
Const stopType As String = "Intermediate"
stopLocation = _filelines(i).Substring(2, 7).Trim
stopLocation = tiploc_1.getFullName(stopLocation)
Dim publicArrival As DateTime = DateTime.ParseExact(_filelines(i).Substring(25, 4), "HHmm", CultureInfo.InvariantCulture)
Dim publicDeparture As DateTime = DateTime.ParseExact(_filelines(i).Substring(29, 4), "HHmm", CultureInfo.InvariantCulture)
'Flag for inputting Half-minutes to working times, if possible.
Dim workingArrival As DateTime = workingTimes(i, 10)
Dim workingDeparture As DateTime = workingTimes(i, 15)
Dim platform As String = _filelines(i).Substring(33, 3).Trim
Dim line As String = _filelines(i).Substring(36, 3).Trim
Dim allowances As Double = 0.0
'Engineering Allowance
allowances = allowancesAdd(i, 54)
'Pathing Allowance
allowances = allowancesAdd(i, 56)
'Performance Allowance
allowances = allowancesAdd(i, 58)
Dim last As Integer = cifSchedule.TrainArray.Count() - 1
cifSchedule.TrainArray(last).AddLocation(New Location(stopType, stopLocation, publicArrival,
publicDeparture, workingArrival,
workingDeparture, platform, line,
allowances))
End If
Exit Select
End If
Case "LT"
If True Then
Const stopType As String = "Terminus"
stopLocation = _filelines(i).Substring(2, 7).Trim
stopLocation = tiploc_1.getFullName(stopLocation)
Dim publicArrival As DateTime = DateTime.ParseExact(_filelines(i).Substring(15, 4), "HHmm", CultureInfo.InvariantCulture)
Dim workingArrival As DateTime = workingTimes(i, 10)
Dim platform As String = _filelines(i).Substring(19, 3)
Dim last As Integer = cifSchedule.TrainArray.Count() - 1
cifSchedule.TrainArray(last).AddLocation(New Location(stopType, stopLocation, publicArrival,
workingArrival, platform))
cifSchedule.TrainArray(last).locationList.Sort()
Exit Select
End If
End Select
Next
Catch kex As KeyNotFoundException
Throw New LoadException(kex.Message)
Catch ex As Exception
Throw New LoadException("Error: " & ex.Message & Environment.NewLine & "Please ensure CIF is not corrupt")
End Try
If cifSchedule.TrainArray.Count = 0 Then
Throw New LoadException("Error: Invalid CIF" & Environment.NewLine & "No trains found")
End If
cifSchedule.TrainArray.Sort()
Return cifSchedule
End Function