chore: fix some performance issues #6

Merged
reya merged 3 commits from fix-performance into master 2026-02-14 02:01:50 +00:00
2 changed files with 95 additions and 142 deletions
Showing only changes of commit 911b841918 - Show all commits

View File

@@ -63,14 +63,11 @@ impl Global for GlobalRelayAuth {}
// Relay authentication // Relay authentication
#[derive(Debug)] #[derive(Debug)]
pub struct RelayAuth { pub struct RelayAuth {
/// Entity for managing auth requests /// Tasks for asynchronous operations
requests: HashSet<Arc<AuthRequest>>, tasks: SmallVec<[Task<()>; 2]>,
/// Event subscriptions /// Event subscriptions
_subscriptions: SmallVec<[Subscription; 1]>, _subscriptions: SmallVec<[Subscription; 1]>,
/// Tasks for asynchronous operations
_tasks: SmallVec<[Task<()>; 1]>,
} }
impl RelayAuth { impl RelayAuth {
@@ -87,50 +84,27 @@ impl RelayAuth {
/// Create a new relay auth instance /// Create a new relay auth instance
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self { fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
// Get the current entity
let entity = cx.entity();
// Channel for communication between nostr and gpui // Channel for communication between nostr and gpui
let (tx, rx) = flume::bounded::<AuthRequest>(100); let (tx, rx) = flume::bounded::<Arc<AuthRequest>>(100);
let mut subscriptions = smallvec![]; let mut subscriptions = smallvec![];
let mut tasks = smallvec![]; let mut tasks = smallvec![];
subscriptions.push( subscriptions.push(
// Observe the current state // Observe the current state
cx.observe_in(&entity, window, |this, _state, window, cx| { cx.observe(&nostr, move |this, state, cx| {
let settings = AppSettings::global(cx); if state.read(cx).connected() {
let mode = AppSettings::get_auth_mode(cx); this.handle_notifications(tx.clone(), cx)
for req in this.requests.iter() {
let trusted_relay = settings.read(cx).trusted_relay(req.url(), cx);
if trusted_relay && mode == AuthMode::Auto {
// Automatically authenticate if the relay is authenticated before
this.response(req, window, cx);
} else {
// Otherwise open the auth request popup
this.ask_for_approval(req, window, cx);
}
} }
}), }),
); );
tasks.push(
// Handle nostr notifications
cx.background_spawn(async move {
Self::handle_notifications(&client, &tx).await;
}),
);
tasks.push( tasks.push(
// Update GPUI states // Update GPUI states
cx.spawn(async move |this, cx| { cx.spawn_in(window, async move |this, cx| {
while let Ok(request) = rx.recv_async().await { while let Ok(req) = rx.recv_async().await {
this.update(cx, |this, cx| { this.update_in(cx, |this, window, cx| {
this.add_request(request, cx); this.handle_auth(&req, window, cx);
}) })
.ok(); .ok();
} }
@@ -138,66 +112,72 @@ impl RelayAuth {
); );
Self { Self {
requests: HashSet::new(), tasks,
_subscriptions: subscriptions, _subscriptions: subscriptions,
_tasks: tasks,
} }
} }
// Handle nostr notifications // Handle nostr notifications
async fn handle_notifications(client: &Client, tx: &flume::Sender<AuthRequest>) { fn handle_notifications(
let mut notifications = client.notifications(); &mut self,
let mut challenges: HashSet<Cow<'_, str>> = HashSet::default(); tx: flume::Sender<Arc<AuthRequest>>,
cx: &mut Context<Self>,
) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
while let Some(notification) = notifications.next().await { let task = cx.background_spawn(async move {
match notification { let mut notifications = client.notifications();
ClientNotification::Message { relay_url, message } => { let mut challenges: HashSet<Cow<'_, str>> = HashSet::default();
match message {
RelayMessage::Auth { challenge } => {
if challenges.insert(challenge.clone()) {
let request = AuthRequest::new(challenge, relay_url);
tx.send_async(request).await.ok();
}
}
RelayMessage::Ok {
event_id, message, ..
} => {
let msg = MachineReadablePrefix::parse(&message);
let mut tracker = tracker().write().await;
// Handle authentication messages while let Some(notification) = notifications.next().await {
if let Some(MachineReadablePrefix::AuthRequired) = msg { match notification {
// Keep track of events that need to be resent after authentication ClientNotification::Message { relay_url, message } => {
tracker.add_to_pending(event_id, relay_url); match message {
} else { RelayMessage::Auth { challenge } => {
// Keep track of events sent by Coop if challenges.insert(challenge.clone()) {
tracker.sent(event_id) let request = AuthRequest::new(challenge, relay_url);
tx.send_async(Arc::new(request)).await.ok();
}
} }
RelayMessage::Ok {
event_id, message, ..
} => {
let msg = MachineReadablePrefix::parse(&message);
let mut tracker = tracker().write().await;
// Handle authentication messages
if let Some(MachineReadablePrefix::AuthRequired) = msg {
// Keep track of events that need to be resent after authentication
tracker.add_to_pending(event_id, relay_url);
} else {
// Keep track of events sent by Coop
tracker.sent(event_id)
}
}
_ => {}
} }
_ => {}
} }
ClientNotification::Shutdown => break,
_ => {}
} }
ClientNotification::Shutdown => break,
_ => {}
} }
} });
self.tasks.push(task);
} }
/// Add a new authentication request. fn handle_auth(&mut self, req: &Arc<AuthRequest>, window: &mut Window, cx: &mut Context<Self>) {
fn add_request(&mut self, request: AuthRequest, cx: &mut Context<Self>) { let settings = AppSettings::global(cx);
self.requests.insert(Arc::new(request)); let trusted_relay = settings.read(cx).trusted_relay(req.url(), cx);
cx.notify(); let mode = AppSettings::get_auth_mode(cx);
}
/// Get the number of pending requests. if trusted_relay && mode == AuthMode::Auto {
pub fn pending_requests(&self, _cx: &App) -> usize { // Automatically authenticate if the relay is authenticated before
self.requests.len() self.response(req, window, cx);
} } else {
// Otherwise open the auth request popup
/// Reask for approval for all pending requests. self.ask_for_approval(req, window, cx);
pub fn re_ask(&mut self, window: &mut Window, cx: &mut Context<Self>) {
for request in self.requests.iter() {
self.ask_for_approval(request, window, cx);
} }
} }
@@ -268,20 +248,17 @@ impl RelayAuth {
let result = task.await; let result = task.await;
let url = req.url(); let url = req.url();
this.update_in(cx, |this, window, cx| { this.update_in(cx, |_this, window, cx| {
window.clear_notification(challenge, cx);
match result { match result {
Ok(_) => { Ok(_) => {
window.clear_notification(challenge, cx);
window.push_notification(format!("{} has been authenticated", url), cx);
// Save the authenticated relay to automatically authenticate future requests // Save the authenticated relay to automatically authenticate future requests
settings.update(cx, |this, cx| { settings.update(cx, |this, cx| {
this.add_trusted_relay(url, cx); this.add_trusted_relay(url, cx);
}); });
// Remove the challenge from the list of pending authentications window.push_notification(format!("{} has been authenticated", url), cx);
this.requests.remove(&req);
cx.notify();
} }
Err(e) => { Err(e) => {
window.push_notification(Notification::error(e.to_string()), cx); window.push_notification(Notification::error(e.to_string()), cx);

View File

@@ -118,10 +118,7 @@ pub struct AppSettings {
values: Settings, values: Settings,
/// Event subscriptions /// Event subscriptions
_subscriptions: SmallVec<[Subscription; 1]>, _subscriptions: SmallVec<[Subscription; 2]>,
/// Background tasks
tasks: SmallVec<[Task<Result<(), Error>>; 1]>,
} }
impl AppSettings { impl AppSettings {
@@ -136,9 +133,7 @@ impl AppSettings {
} }
fn new(cx: &mut Context<Self>) -> Self { fn new(cx: &mut Context<Self>) -> Self {
let load_settings = Self::get_from_database(cx); let nostr = NostrRegistry::global(cx);
let mut tasks = smallvec![];
let mut subscriptions = smallvec![]; let mut subscriptions = smallvec![];
subscriptions.push( subscriptions.push(
@@ -148,24 +143,15 @@ impl AppSettings {
}), }),
); );
tasks.push( subscriptions.push(
// Load the initial settings // Observe and automatically save settings on changes
cx.spawn(async move |this, cx| { cx.observe(&nostr, |this, _state, cx| {
let settings = load_settings.await.unwrap_or(Settings::default()); this.load(cx);
log::info!("Settings: {settings:?}");
// Update the settings state
this.update(cx, |this, cx| {
this.set_settings(settings, cx);
})?;
Ok(())
}), }),
); );
Self { Self {
values: Settings::default(), values: Settings::default(),
tasks,
_subscriptions: subscriptions, _subscriptions: subscriptions,
} }
} }
@@ -176,50 +162,38 @@ impl AppSettings {
cx.notify(); cx.notify();
} }
/// Get settings from the database /// Load settings
fn get_from_database(cx: &App) -> Task<Result<Settings, Error>> { fn load(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
cx.background_spawn(async move { let task: Task<Result<Settings, Error>> = cx.background_spawn(async move {
// Construct a filter to get the latest settings let signer = client.signer().context("Signer not found")?;
let mut filter = Filter::new() let public_key = signer.get_public_key().await?;
let filter = Filter::new()
.kind(Kind::ApplicationSpecificData) .kind(Kind::ApplicationSpecificData)
.identifier(SETTINGS_IDENTIFIER) .identifier(SETTINGS_IDENTIFIER)
.author(public_key)
.limit(1); .limit(1);
// If the signer is available, get settings belonging to the current user if let Some(event) = client.database().query(filter).await?.last_owned() {
if let Some(signer) = client.signer() {
if let Ok(public_key) = signer.get_public_key().await {
// Push author to the filter
filter = filter.author(public_key);
}
}
if let Some(event) = client.database().query(filter).await?.first_owned() {
Ok(serde_json::from_str(&event.content)?) Ok(serde_json::from_str(&event.content)?)
} else { } else {
Err(anyhow!("Not found")) Err(anyhow!("Not found"))
} }
});
cx.spawn(async move |this, cx| {
let settings = task.await.unwrap_or(Settings::default());
// Update settings
this.update(cx, |this, cx| {
this.set_settings(settings, cx);
})
.ok();
}) })
} .detach();
/// Load settings
pub fn load(&mut self, cx: &mut Context<Self>) {
let task = Self::get_from_database(cx);
self.tasks.push(
// Run task in the background
cx.spawn(async move |this, cx| {
let settings = task.await?;
// Update settings
this.update(cx, |this, cx| {
this.set_settings(settings, cx);
})?;
Ok(())
}),
);
} }
/// Save settings /// Save settings
@@ -228,7 +202,7 @@ impl AppSettings {
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let settings = self.values.clone(); let settings = self.values.clone();
self.tasks.push(cx.background_spawn(async move { let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let signer = client.signer().context("Signer not found")?; let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?; let public_key = signer.get_public_key().await?;
let content = serde_json::to_string(&settings)?; let content = serde_json::to_string(&settings)?;
@@ -243,7 +217,9 @@ impl AppSettings {
client.database().save_event(&event).await?; client.database().save_event(&event).await?;
Ok(()) Ok(())
})); });
task.detach();
} }
/// Check if the given relay is already authenticated /// Check if the given relay is already authenticated