Skip to content

Commit 529a3ff

Browse files
authored
Moved wave dump file writing to be async for performance (#150)
1 parent 06a6e86 commit 529a3ff

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

lib/src/wave_dumper.dart

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class WaveDumper {
2626
/// The file to write dumped output waveform to.
2727
final File _outputFile;
2828

29+
/// A sink to write contents into [_outputFile].
30+
late final IOSink _outFileSink;
31+
2932
/// A counter for tracking signal names in the VCD file.
3033
int _signalMarkerIdx = 0;
3134

@@ -51,6 +54,8 @@ class WaveDumper {
5154
'Module must be built before passed to dumper. Call build() first.');
5255
}
5356

57+
_outFileSink = _outputFile.openWrite();
58+
5459
_collectAllSignals();
5560

5661
_writeHeader();
@@ -66,11 +71,24 @@ class WaveDumper {
6671
}
6772
});
6873

69-
Simulator.simulationEnded.then((args) {
74+
Simulator.registerEndOfSimulationAction(() async {
7075
_captureTimestamp(Simulator.time);
76+
77+
await _terminate();
7178
});
7279
}
7380

81+
/// Writes [contents] to the output file.
82+
void _writeToFile(String contents) {
83+
_outFileSink.write(contents);
84+
}
85+
86+
/// Terminates the waveform dumping, including closing the file.
87+
Future<void> _terminate() async {
88+
await _outFileSink.flush();
89+
await _outFileSink.close();
90+
}
91+
7492
/// Registers all signal value changes to write updates to the dumped VCD.
7593
void _collectAllSignals() {
7694
var modulesToParse = <Module>[module];
@@ -113,19 +131,19 @@ class WaveDumper {
113131
\$end
114132
\$timescale $timescale \$end
115133
''';
116-
_outputFile.writeAsStringSync(header);
134+
_writeToFile(header);
117135
}
118136

119137
/// Writes the scope of the VCD, including signal and hierarchy declarations, as well as initial values.
120138
void _writeScope() {
121139
var scopeString = _computeScopeString(module);
122140
scopeString += '\$enddefinitions \$end\n';
123141
scopeString += '\$dumpvars\n';
124-
_outputFile.writeAsStringSync(scopeString, mode: FileMode.append);
142+
_writeToFile(scopeString);
125143
for (var element in _signalToMarkerMap.keys) {
126144
_writeSignalValueUpdate(element);
127145
}
128-
_outputFile.writeAsStringSync('\$end\n', mode: FileMode.append);
146+
_writeToFile('\$end\n');
129147
}
130148

131149
/// Generates the top of the scope string (signal and hierarchy definitions).
@@ -160,7 +178,7 @@ class WaveDumper {
160178
/// Writes the current timestamp to the VCD.
161179
void _captureTimestamp(int timestamp) {
162180
var timestampString = '#$timestamp\n';
163-
_outputFile.writeAsStringSync(timestampString, mode: FileMode.append);
181+
_writeToFile(timestampString);
164182

165183
for (var signal in _changedLogicsThisTimestamp) {
166184
_writeSignalValueUpdate(signal);
@@ -180,7 +198,7 @@ class WaveDumper {
180198
: signal.value.toString(includeWidth: false);
181199
var marker = _signalToMarkerMap[signal];
182200
var updateString = '$updateValue$marker\n';
183-
_outputFile.writeAsStringSync(updateString, mode: FileMode.append);
201+
_writeToFile(updateString);
184202
}
185203
}
186204

0 commit comments

Comments
 (0)