Skip to content

Commit b2d285b

Browse files
authored
Dead code removal 3 (#79)
* fix: remove dead code and fix double client.close() bug Dead code removal: - Remove DateTimeX.timestamp getter (unused) - Remove GhIdX.markdownLink getter (unused) - Remove Pfs.separator and Pfs.homeDir (unused) - Remove SettingsService.applySpeedSettingsToBuiltin() (unused) - Remove 28 unused individual Settings.set*() methods (setRpcListenPort, setRpcSecret, setMaxConcurrentDownloads, setSplit, etc.) - only setBtTracker and setLastSyncTrackerTime are used by TrackerSyncService Bug fix: - Remove redundant client.close() in try block of file selection save handler; the finally block already handles cleanup, causing double-close on success * perf: optimize hot-path allocations and exception-based control flow Performance: - Cache RegExp as static finals in TaskDetailsBtHelpers (azureus pattern, digit/letter patterns) instead of recompiling per-peer per-second - Cache UnmodifiableListView in DownloadDataService.tasks getter (was creating a new wrapper on every access, dozens of times per second) - Precompute lowercase names map before sort in _compareTasks (eliminates O(n log n) toLowerCase() allocations per refresh cycle) - Precompute lowercase instance names map before search filtering (saves 1x toLowerCase() per task per keystroke) - Replace firstWhere+catch with simple for-loop in getInstanceById and getBuiltinInstance (avoids expensive stack trace capture for control flow) - Use getBuiltinInstance() in initialize() instead of bare firstWhere
1 parent d5cf274 commit b2d285b

10 files changed

Lines changed: 37 additions & 261 deletions

File tree

lib/constants/github_id.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,4 @@ typedef GhId = String;
1010

1111
extension GhIdX on GhId {
1212
String get url => 'https://github.com/$this';
13-
14-
String get markdownLink => '[$this]($url)';
1513
}

lib/kit/core/ext/datetime.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@ extension DateTimeX on DateTime {
22
String get hourMinute {
33
return '${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}';
44
}
5-
6-
static int get timestamp => DateTime.now().millisecondsSinceEpoch;
75
}

lib/kit/core/platform.dart

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,6 @@ enum Pfs {
2424
_ => unknown,
2525
};
2626
}();
27-
28-
static final String separator = isWindows ? '\\' : '/';
29-
30-
static final String? homeDir = () {
31-
final envVars = Platform.environment;
32-
if (isMacOS || isLinux) {
33-
return envVars['HOME'];
34-
} else if (isWindows) {
35-
return envVars['UserProfile'];
36-
}
37-
return null;
38-
}();
3927
}
4028

4129
final isLinux = Pfs.type == Pfs.linux;

lib/models/settings.dart

Lines changed: 0 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -578,203 +578,18 @@ class Settings extends ChangeNotifier with Loggable {
578578
}
579579

580580
// Built-in Aria2 instance setters
581-
// Connection settings
582-
Future<void> setRpcListenPort(int port) async {
583-
_rpcListenPort = port;
584-
notifyListeners();
585-
await _saveAllSettings();
586-
}
587-
588-
Future<void> setRpcSecret(String secret) async {
589-
_rpcSecret = secret;
590-
notifyListeners();
591-
await _saveAllSettings();
592-
}
593-
594-
// Transfer settings
595-
Future<void> setMaxConcurrentDownloads(int value) async {
596-
_maxConcurrentDownloads = value;
597-
notifyListeners();
598-
await _saveAllSettings();
599-
}
600-
601-
Future<void> setMaxConnectionPerServer(int value) async {
602-
_maxConnectionPerServer = value;
603-
notifyListeners();
604-
await _saveAllSettings();
605-
}
606-
607-
Future<void> setSplit(int value) async {
608-
_split = value;
609-
notifyListeners();
610-
await _saveAllSettings();
611-
}
612-
613-
Future<void> setContinueDownloads(bool value) async {
614-
_continueDownloads = value;
615-
notifyListeners();
616-
await _saveAllSettings();
617-
}
618-
619-
Future<void> setDownloadDir(String value) async {
620-
_downloadDir = value;
621-
notifyListeners();
622-
await _saveAllSettings();
623-
}
624-
625-
// Speed settings
626-
Future<void> setMaxOverallDownloadLimit(int limit) async {
627-
_maxOverallDownloadLimit = limit;
628-
notifyListeners();
629-
await _saveAllSettings();
630-
}
631-
632-
Future<void> setMaxOverallUploadLimit(int limit) async {
633-
_maxOverallUploadLimit = limit;
634-
notifyListeners();
635-
await _saveAllSettings();
636-
}
637-
638-
// BT settings
639-
Future<void> setBtSaveMetadata(bool value) async {
640-
_btSaveMetadata = value;
641-
notifyListeners();
642-
await _saveAllSettings();
643-
}
644-
645-
Future<void> setBtForceEncryption(bool value) async {
646-
_btForceEncryption = value;
647-
notifyListeners();
648-
await _saveAllSettings();
649-
}
650-
651-
Future<void> setBtLoadSavedMetadata(bool value) async {
652-
_btLoadSavedMetadata = value;
653-
notifyListeners();
654-
await _saveAllSettings();
655-
}
656-
657-
Future<void> setKeepSeeding(bool value) async {
658-
_keepSeeding = value;
659-
notifyListeners();
660-
await _saveAllSettings();
661-
}
662-
663-
Future<void> setSeedRatio(double ratio) async {
664-
_seedRatio = ratio;
665-
notifyListeners();
666-
await _saveAllSettings();
667-
}
668-
669-
Future<void> setSeedTime(int minutes) async {
670-
_seedTime = minutes;
671-
notifyListeners();
672-
await _saveAllSettings();
673-
}
674-
675-
Future<void> setBtListenPort(String port) async {
676-
_btListenPort = port;
677-
notifyListeners();
678-
await _saveAllSettings();
679-
}
680-
681581
Future<void> setBtTracker(String trackers) async {
682582
_btTracker = _normalizeBtTracker(trackers);
683583
notifyListeners();
684584
await _saveAllSettings();
685585
}
686586

687-
Future<void> setBtExcludeTracker(String trackers) async {
688-
_btExcludeTracker = trackers;
689-
notifyListeners();
690-
await _saveAllSettings();
691-
}
692-
693-
// Advanced settings
694-
Future<void> setProxyEnabled(bool value) async {
695-
_proxyEnabled = value;
696-
notifyListeners();
697-
await _saveAllSettings();
698-
}
699-
700-
Future<void> setAllProxy(String proxy) async {
701-
_allProxy = proxy;
702-
notifyListeners();
703-
await _saveAllSettings();
704-
}
705-
706-
Future<void> setNoProxy(String noProxy) async {
707-
_noProxy = noProxy;
708-
notifyListeners();
709-
await _saveAllSettings();
710-
}
711-
712-
Future<void> setDhtListenPort(int port) async {
713-
_dhtListenPort = port;
714-
notifyListeners();
715-
await _saveAllSettings();
716-
}
717-
718-
Future<void> setEnableDht6(bool value) async {
719-
_enableDht6 = value;
720-
notifyListeners();
721-
await _saveAllSettings();
722-
}
723-
724-
Future<void> setEnableUpnp(bool value) async {
725-
_enableUpnp = value;
726-
notifyListeners();
727-
await _saveAllSettings();
728-
}
729-
730-
Future<void> setSessionPath(String path) async {
731-
_sessionPath = path;
732-
notifyListeners();
733-
await _saveAllSettings();
734-
}
735-
736-
Future<void> setLogPath(String path) async {
737-
_logPath = path;
738-
notifyListeners();
739-
await _saveAllSettings();
740-
}
741-
742-
Future<void> setAutoSyncTracker(bool value) async {
743-
_autoSyncTracker = value;
744-
notifyListeners();
745-
await _saveAllSettings();
746-
}
747-
748587
Future<void> setLastSyncTrackerTime(int value) async {
749588
_lastSyncTrackerTime = value;
750589
notifyListeners();
751590
await _saveAllSettings();
752591
}
753592

754-
Future<void> setTrackerSource(String value) async {
755-
_trackerSource = value;
756-
notifyListeners();
757-
await _saveAllSettings();
758-
}
759-
760-
Future<void> setAutoFileRenaming(bool value) async {
761-
_autoFileRenaming = value;
762-
notifyListeners();
763-
await _saveAllSettings();
764-
}
765-
766-
Future<void> setAllowOverwrite(bool value) async {
767-
_allowOverwrite = value;
768-
notifyListeners();
769-
await _saveAllSettings();
770-
}
771-
772-
Future<void> setUserAgent(String userAgent) async {
773-
_userAgent = userAgent;
774-
notifyListeners();
775-
await _saveAllSettings();
776-
}
777-
778593
Future<void> updateBuiltinInstanceSettings({
779594
required int rpcListenPort,
780595
required String rpcSecret,

lib/pages/download_page/components/task_details_bt_helpers.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import '../../../utils/format_utils.dart';
77
import '../models/download_task.dart';
88

99
class TaskDetailsBtHelpers {
10+
static final _azureusPattern = RegExp(r'^-([A-Za-z~]{2})(.{4})-');
11+
static final _digitPattern = RegExp(r'[0-9]');
12+
static final _letterPattern = RegExp(r'[A-Za-z]');
1013
static Widget buildBitfieldVisualization(
1114
BuildContext context,
1215
DownloadTask task,
@@ -327,7 +330,7 @@ class TaskDetailsBtHelpers {
327330
return 'unknown';
328331
}
329332

330-
final azureusMatch = RegExp(r'^-([A-Za-z~]{2})(.{4})-').firstMatch(decoded);
333+
final azureusMatch = _azureusPattern.firstMatch(decoded);
331334
if (azureusMatch != null) {
332335
final clientCode = azureusMatch.group(1)!;
333336
final versionRaw = azureusMatch.group(2)!;
@@ -370,9 +373,9 @@ class TaskDetailsBtHelpers {
370373
static String _formatPeerVersion(String rawVersion) {
371374
final segments = <String>[];
372375
for (final char in rawVersion.split('')) {
373-
if (RegExp(r'[0-9]').hasMatch(char)) {
376+
if (_digitPattern.hasMatch(char)) {
374377
segments.add(char);
375-
} else if (RegExp(r'[A-Za-z]').hasMatch(char)) {
378+
} else if (_letterPattern.hasMatch(char)) {
376379
segments.add(char.toLowerCase());
377380
}
378381
}

lib/pages/download_page/components/task_details_dialog.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,6 @@ class TaskDetailsDialog {
604604
),
605605
},
606606
);
607-
client.close();
608607

609608
onTaskUpdated?.call();
610609

lib/pages/download_page/download_page.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,12 @@ class DownloadPageState extends State<DownloadPage>
377377

378378
if (_searchQuery.isNotEmpty) {
379379
final query = _searchQuery.toLowerCase();
380+
final lowerInstanceNames = <String, String>{
381+
for (final entry in _instanceNames.entries)
382+
entry.key: entry.value.toLowerCase(),
383+
};
380384
tasks = tasks.where((task) {
381-
final instanceName = (_instanceNames[task.instanceId] ?? '')
382-
.toLowerCase();
385+
final instanceName = lowerInstanceNames[task.instanceId] ?? '';
383386
final taskDir = (task.dir ?? '').toLowerCase();
384387
final taskName = task.name.toLowerCase();
385388
return taskName.contains(query) ||

lib/services/download_data_service.dart

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class DownloadDataService extends ChangeNotifier with Loggable {
3434
Timer? _refreshTimer;
3535

3636
List<DownloadTask> _tasks = [];
37+
List<DownloadTask> _tasksView = const [];
3738
bool _isRefreshing = false;
3839
String? _lastError;
3940
final List<DownloadTaskNotification> _pendingNotifications = [];
@@ -44,7 +45,7 @@ class DownloadDataService extends ChangeNotifier with Loggable {
4445
final Map<String, Aria2RpcClient> _clientCache = {};
4546
List<Aria2Instance> Function()? _connectedInstancesProvider;
4647

47-
List<DownloadTask> get tasks => UnmodifiableListView(_tasks);
48+
List<DownloadTask> get tasks => _tasksView;
4849
int get tasksVersion => _tasksVersion;
4950
bool get isRefreshing => _isRefreshing;
5051
String? get lastError => _lastError;
@@ -86,6 +87,7 @@ class DownloadDataService extends ChangeNotifier with Loggable {
8687
final hadTasks = _tasks.isNotEmpty;
8788
final hadError = _lastError != null;
8889
_tasks = [];
90+
_tasksView = UnmodifiableListView(_tasks);
8991
_tasksVersion++;
9092
_lastError = null;
9193
if (hadTasks || hadError) {
@@ -102,14 +104,18 @@ class DownloadDataService extends ChangeNotifier with Loggable {
102104
final taskGroups = await Future.wait(
103105
connectedInstances.map(_fetchTasksForInstance),
104106
);
105-
final newTasks = taskGroups.expand((tasks) => tasks).toList()
106-
..sort(_compareTasks);
107+
final newTasks = taskGroups.expand((tasks) => tasks).toList();
108+
final lowerCaseNames = <String, String>{
109+
for (final t in newTasks) t.name: t.name.toLowerCase(),
110+
};
111+
newTasks.sort((a, b) => _compareTasks(a, b, lowerCaseNames));
107112

108113
final terminalTransitionInstanceIds = _collectTaskNotifications(
109114
previousTasks,
110115
newTasks,
111116
);
112117
_tasks = newTasks;
118+
_tasksView = UnmodifiableListView(_tasks);
113119
_tasksVersion++;
114120
_saveSessionsForTerminalTransitions(
115121
connectedInstances,
@@ -217,7 +223,11 @@ class DownloadDataService extends ChangeNotifier with Loggable {
217223
DownloadStatus.stopped: 2,
218224
};
219225

220-
int _compareTasks(DownloadTask left, DownloadTask right) {
226+
int _compareTasks(
227+
DownloadTask left,
228+
DownloadTask right,
229+
Map<String, String> lowerCaseNames,
230+
) {
221231
final leftOrder = _statusOrder[left.status] ?? 99;
222232
final rightOrder = _statusOrder[right.status] ?? 99;
223233
if (leftOrder != rightOrder) {
@@ -228,12 +238,8 @@ class DownloadDataService extends ChangeNotifier with Loggable {
228238
return left.instanceId.compareTo(right.instanceId);
229239
}
230240

231-
return _compareIgnoreCase(left.name, right.name);
232-
}
233-
234-
static int _compareIgnoreCase(String a, String b) {
235-
final aLower = a.toLowerCase();
236-
final bLower = b.toLowerCase();
241+
final aLower = lowerCaseNames[left.name] ?? left.name.toLowerCase();
242+
final bLower = lowerCaseNames[right.name] ?? right.name.toLowerCase();
237243
return aLower.compareTo(bLower);
238244
}
239245

0 commit comments

Comments
 (0)