Skip to content

Commit 943533d

Browse files
committed
Stop initializing python in Rust headless if user plugins or repo plugins is disabled
This will speed up InitCorePlugins by 5x for headless rust scripts that do not need python to be initialized. For rust scripts that do use python user plugins or repo plugins this keeps the behavior the same.
1 parent ec89e9c commit 943533d

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

rust/src/headless.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,21 @@ impl Default for InitializationOptions {
172172
}
173173
}
174174

175+
/// Adjust settings temporarily so that we can screw with the core settings and not impact user settings.
176+
fn init_adjusted_settings(options: &InitializationOptions, cb: impl FnOnce(&mut Settings)) {
177+
let mut settings = Settings::new();
178+
let is_python_enabled = settings.get_bool("corePlugins.python");
179+
// Because we don't ship core python plugins, we don't need to load
180+
// python if we are not loading user plugins.
181+
settings.set_bool("corePlugins.python", options.user_plugins);
182+
if options.repo_plugins {
183+
settings.set_bool("corePlugins.python", options.repo_plugins);
184+
}
185+
cb(&mut settings);
186+
// Reset the settings to the previous value.
187+
settings.set_bool("corePlugins.python", is_python_enabled);
188+
}
189+
175190
/// This initializes the core with the given [`InitializationOptions`].
176191
pub fn init_with_opts(options: InitializationOptions) -> Result<(), InitializationError> {
177192
// If we are the main thread that means there is no main thread, we should register a main thread handler.
@@ -207,20 +222,22 @@ pub fn init_with_opts(options: InitializationOptions) -> Result<(), Initializati
207222
_ => {}
208223
}
209224

210-
if let Some(license) = options.license {
225+
if let Some(license) = options.license.clone() {
211226
// We were given a license override, use it!
212227
set_license(Some(license));
213228
}
214229

215-
set_bundled_plugin_directory(options.bundled_plugin_directory);
230+
set_bundled_plugin_directory(&options.bundled_plugin_directory);
216231

217-
unsafe {
218-
BNInitPlugins(options.user_plugins);
219-
if options.repo_plugins {
220-
// We are allowed to initialize repo plugins, so do it!
221-
BNInitRepoPlugins();
232+
init_adjusted_settings(&options, |_| {
233+
unsafe {
234+
BNInitPlugins(options.user_plugins);
235+
if options.repo_plugins {
236+
// We are allowed to initialize repo plugins, so do it!
237+
BNInitRepoPlugins();
238+
}
222239
}
223-
}
240+
});
224241

225242
if !is_license_validated() {
226243
// Unfortunately you must have a valid license to use Binary Ninja.

0 commit comments

Comments
 (0)