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

fix: Optimize use of hash tables in platform adapters #485

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion platforms/macos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ default-target = "x86_64-apple-darwin"
[dependencies]
accesskit = { version = "0.17.0", path = "../../common" }
accesskit_consumer = { version = "0.25.0", path = "../../consumer" }
once_cell = "1.13.0"
hashbrown = { version = "0.15", default-features = false, features = ["default-hasher"] }
objc2 = "0.5.1"
objc2-foundation = { version = "0.2.0", features = [
"NSArray",
Expand Down
3 changes: 2 additions & 1 deletion platforms/macos/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

use accesskit::{ActionHandler, ActionRequest, NodeId};
use accesskit_consumer::Tree;
use hashbrown::HashMap;
use objc2::rc::{Id, WeakId};
use objc2_app_kit::*;
use objc2_foundation::MainThreadMarker;
use std::{cell::RefCell, collections::HashMap, rc::Rc};
use std::{cell::RefCell, rc::Rc};

use crate::node::PlatformNode;

Expand Down
3 changes: 2 additions & 1 deletion platforms/macos/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

use accesskit::{Live, NodeId, Role};
use accesskit_consumer::{FilterResult, Node, TreeChangeHandler};
use hashbrown::HashSet;
use objc2::runtime::{AnyObject, ProtocolObject};
use objc2_app_kit::*;
use objc2_foundation::{NSMutableDictionary, NSNumber, NSString};
use std::{collections::HashSet, rc::Rc};
use std::rc::Rc;

use crate::{context::Context, filters::filter, node::NodeWrapper};

Expand Down
58 changes: 30 additions & 28 deletions platforms/macos/src/subclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ use objc2::{
};
use objc2_app_kit::{NSView, NSWindow};
use objc2_foundation::{NSArray, NSObject, NSPoint};
use once_cell::sync::Lazy;
use std::{cell::RefCell, collections::HashMap, ffi::c_void, sync::Mutex};
use std::{cell::RefCell, ffi::c_void, sync::Mutex};

use crate::{event::QueuedEvents, Adapter};

static SUBCLASSES: Lazy<Mutex<HashMap<&'static AnyClass, &'static AnyClass>>> =
Lazy::new(|| Mutex::new(HashMap::new()));
static SUBCLASSES: Mutex<Vec<(&'static AnyClass, &'static AnyClass)>> = Mutex::new(Vec::new());

static ASSOCIATED_OBJECT_KEY: u8 = 0;

Expand Down Expand Up @@ -163,34 +161,38 @@ impl SubclassingAdapter {
)
};
let mut subclasses = SUBCLASSES.lock().unwrap();
let entry = subclasses.entry(prev_class);
let subclass = entry.or_insert_with(|| {
let name = format!("AccessKitSubclassOf{}", prev_class.name());
let mut builder = ClassBuilder::new(&name, prev_class).unwrap();
unsafe {
builder.add_method(
sel!(superclass),
superclass as unsafe extern "C" fn(_, _) -> _,
);
builder.add_method(
sel!(accessibilityChildren),
children as unsafe extern "C" fn(_, _) -> _,
);
builder.add_method(
sel!(accessibilityFocusedUIElement),
focus as unsafe extern "C" fn(_, _) -> _,
);
builder.add_method(
sel!(accessibilityHitTest:),
hit_test as unsafe extern "C" fn(_, _, _) -> _,
);
let subclass = match subclasses.iter().find(|entry| entry.0 == prev_class) {
Some(entry) => entry.1,
None => {
let name = format!("AccessKitSubclassOf{}", prev_class.name());
let mut builder = ClassBuilder::new(&name, prev_class).unwrap();
unsafe {
builder.add_method(
sel!(superclass),
superclass as unsafe extern "C" fn(_, _) -> _,
);
builder.add_method(
sel!(accessibilityChildren),
children as unsafe extern "C" fn(_, _) -> _,
);
builder.add_method(
sel!(accessibilityFocusedUIElement),
focus as unsafe extern "C" fn(_, _) -> _,
);
builder.add_method(
sel!(accessibilityHitTest:),
hit_test as unsafe extern "C" fn(_, _, _) -> _,
);
}
let class = builder.register();
subclasses.push((prev_class, class));
class
}
builder.register()
});
};
// SAFETY: Changing the view's class is only safe because
// the subclass doesn't add any instance variables;
// it uses an associated object instead.
unsafe { object_setClass(view as *mut _, (*subclass as *const AnyClass).cast()) };
unsafe { object_setClass(view as *mut _, (subclass as *const AnyClass).cast()) };
Self {
view: retained_view,
associated,
Expand Down
1 change: 1 addition & 0 deletions platforms/windows/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ targets = []
[dependencies]
accesskit = { version = "0.17.0", path = "../../common" }
accesskit_consumer = { version = "0.25.0", path = "../../consumer" }
hashbrown = { version = "0.15", default-features = false, features = ["default-hasher"] }
paste = "1.0"
static_assertions = "1.1.0"
windows-core = "0.58.0"
Expand Down
6 changes: 2 additions & 4 deletions platforms/windows/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ use accesskit::{
TreeUpdate,
};
use accesskit_consumer::{FilterResult, Node, Tree, TreeChangeHandler};
use std::{
collections::HashSet,
sync::{atomic::Ordering, Arc},
};
use hashbrown::HashSet;
use std::sync::{atomic::Ordering, Arc};
use windows::Win32::{
Foundation::*,
UI::{Accessibility::*, WindowsAndMessaging::*},
Expand Down