Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

speeding up iOS plugin initialization #552

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ezamagni
Copy link

@ezamagni ezamagni commented Dec 8, 2024

Importing flutter_tts can have a small penalty in the performance of the application startup in iOS:
during initialisation this plugin is performing some unnecessary operations that could be deferred later in the application lifecycle, when the plugin is actually needed.

The following screenshot is showing the Launches audits for an application I currently have in production. This tool shows that flutter_tts is responsible for ~200 ms spent during the app startup, when all the imported plugins are registered via the generated registerWithRegistry(:) method. This procedure is executed on the main thread within the AppDelegate.application(:didFinishLaunchingWithOptions:) method.
Screenshot

flutter_tts is spending some time in this phase eagerly initialising the language and languages fields:

var language: String = AVSpeechSynthesisVoice.currentLanguageCode()
var languages = Set<String>()
// ...
init(channel: FlutterMethodChannel) {
  super.init()
  self.channel = channel
  synthesizer.delegate = self
  setLanguages() 
}

Both these fields could be initialised lazily (just as the audioSession shared instance is already being initialised lazily), since they are not needed until later, when the plugin is eventually used by the application.

This PR aims at solving this small but noticeable performance impact of flutter_tts in iOS by simply initialising the language and languages fields lazily:

lazy var language: String = {
  AVSpeechSynthesisVoice.currentLanguageCode()
}()
lazy var languages: Set<String> = {
  Set(AVSpeechSynthesisVoice.speechVoices().map(\.language))
}()    

var channel = FlutterMethodChannel()
init(channel: FlutterMethodChannel) {
  super.init()
  self.channel = channel
  synthesizer.delegate = self
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants