I’m developing a Flutter application that uses a custom CodeParser to parse input and update a CoordinateSystemModel. The parser works fine for small inputs, but it hangs and becomes unresponsive when dealing with larger inputs. Here’s a simplified version of my CodeParser:
class CodeParser {
static void parseCode(String code, CoordinateSystemModel model) {
print('Parsing code: $code');
List<String> lines = code.split('n');
List<Place> newPlaces = [];
List<Task> newTasks = [];
for (String line in lines) {
line = line.trim();
parseAndExecuteLine(line, model, newPlaces, newTasks);
}
model.clearAndReplacePlaces(newPlaces);
model.clearAndReplaceTasks(newTasks);
}
static void parseAndExecuteLine(String line, CoordinateSystemModel model,
[List<Place>? newPlaces, List<Task>? newTasks]) {
if (line.isEmpty) return;
if (line.startsWith('project')) {
_parseProject(line, model);
} else if (line.startsWith('type')) {
_parseType(line, model);
} else if (line.startsWith('range')) {
_parseRange(line, model);
} else if (line.startsWith('path')) {
_parsePath(line, model);
} else if (line.startsWith('place')) {
Place? place = _parsePlace(line);
if (place != null) {
if (newPlaces != null) {
newPlaces.add(place);
} else {
model.addPlace(place);
}
}
} else if (line.startsWith('task')) {
Task? task = _parseTask(line);
if (task != null) {
if (newTasks != null) {
newTasks.add(task);
} else {
model.addTask(task);
}
}
}
}
// Other parsing methods...
}
The parser works fine with small inputs, but it hangs when I try to parse larger inputs.
I’ve tried optimizing the parser by implementing a chunked approach and adding batch methods to my CoordinateSystemModel:
void addPlaces(List<Place> newPlaces) {
_places.addAll(newPlaces);
_places.sort((a, b) => a.km.compareTo(b.km));
_updateCodeAndNotify();
}
void addTasks(List<Task> newTasks) {
_tasks.addAll(newTasks);
_tasks.sort((a, b) => a.startDate.compareTo(b.startDate));
_updateCodeAndNotify();
}
However, the app still becomes unresponsive with larger inputs. How can I optimize my CodeParser to handle larger inputs without hanging? Any suggestions for improving performance or alternative approaches would be greatly appreciated.
Environment:
Flutter 3.22.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision b0850beeb2 (5 weeks ago) • 2024-07-16 21:43:41 -0700
Engine • revision 235db911ba
Tools • Dart 3.4.4 • DevTools 2.34.3
Browser: Google Chrome
OS: macOS