chore: fix some performance issues #6

Merged
reya merged 3 commits from fix-performance into master 2026-02-14 02:01:50 +00:00
11 changed files with 667 additions and 706 deletions

931
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -331,7 +331,11 @@ impl Sidebar {
// Create a new room and emit it // Create a new room and emit it
async_chat.update_in(cx, |this, _window, cx| { async_chat.update_in(cx, |this, _window, cx| {
let room = cx.new(|_| Room::new(public_key, receivers).kind(RoomKind::Ongoing)); let room = cx.new(|_| {
Room::new(public_key, receivers)
.organize(&public_key)
.kind(RoomKind::Ongoing)
});
this.emit_room(&room, cx); this.emit_room(&room, cx);
})?; })?;

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,14 +112,21 @@ 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(
&mut self,
tx: flume::Sender<Arc<AuthRequest>>,
cx: &mut Context<Self>,
) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let task = cx.background_spawn(async move {
let mut notifications = client.notifications(); let mut notifications = client.notifications();
let mut challenges: HashSet<Cow<'_, str>> = HashSet::default(); let mut challenges: HashSet<Cow<'_, str>> = HashSet::default();
@@ -156,7 +137,7 @@ impl RelayAuth {
RelayMessage::Auth { challenge } => { RelayMessage::Auth { challenge } => {
if challenges.insert(challenge.clone()) { if challenges.insert(challenge.clone()) {
let request = AuthRequest::new(challenge, relay_url); let request = AuthRequest::new(challenge, relay_url);
tx.send_async(request).await.ok(); tx.send_async(Arc::new(request)).await.ok();
} }
} }
RelayMessage::Ok { RelayMessage::Ok {
@@ -181,23 +162,22 @@ impl RelayAuth {
_ => {} _ => {}
} }
} }
});
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

@@ -5,10 +5,11 @@ edition.workspace = true
publish.workspace = true publish.workspace = true
[dependencies] [dependencies]
state = { path = "../state" } common = { path = "../common" }
nostr-sdk.workspace = true nostr-sdk.workspace = true
gpui.workspace = true gpui.workspace = true
smol.workspace = true
anyhow.workspace = true anyhow.workspace = true
log.workspace = true log.workspace = true
smallvec.workspace = true smallvec.workspace = true

View File

@@ -1,13 +1,11 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use anyhow::{anyhow, Context as AnyhowContext, Error}; use anyhow::{anyhow, Error};
use common::config_dir;
use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task}; use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task};
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use state::NostrRegistry;
const SETTINGS_IDENTIFIER: &str = "coop:settings";
pub fn init(cx: &mut App) { pub fn init(cx: &mut App) {
AppSettings::set_global(cx.new(AppSettings::new), cx) AppSettings::set_global(cx.new(AppSettings::new), cx)
@@ -118,10 +116,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 +131,6 @@ 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 mut tasks = smallvec![];
let mut subscriptions = smallvec![]; let mut subscriptions = smallvec![];
subscriptions.push( subscriptions.push(
@@ -148,24 +140,16 @@ impl AppSettings {
}), }),
); );
tasks.push( cx.defer(|cx| {
// Load the initial settings let settings = AppSettings::global(cx);
cx.spawn(async move |this, cx| {
let settings = load_settings.await.unwrap_or(Settings::default());
log::info!("Settings: {settings:?}");
// Update the settings state settings.update(cx, |this, cx| {
this.update(cx, |this, cx| { this.load(cx);
this.set_settings(settings, cx); });
})?; });
Ok(())
}),
);
Self { Self {
values: Settings::default(), values: Settings::default(),
tasks,
_subscriptions: subscriptions, _subscriptions: subscriptions,
} }
} }
@@ -176,74 +160,45 @@ 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 task: Task<Result<Settings, Error>> = cx.background_spawn(async move {
let client = nostr.read(cx).client(); let path = config_dir().join(".settings");
cx.background_spawn(async move { if let Ok(content) = smol::fs::read_to_string(&path).await {
// Construct a filter to get the latest settings Ok(serde_json::from_str(&content)?)
let mut filter = Filter::new()
.kind(Kind::ApplicationSpecificData)
.identifier(SETTINGS_IDENTIFIER)
.limit(1);
// If the signer is available, get settings belonging to the current user
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)?)
} else { } else {
Err(anyhow!("Not found")) Err(anyhow!("Not found"))
} }
}) });
}
/// 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| { cx.spawn(async move |this, cx| {
let settings = task.await?; let settings = task.await.unwrap_or(Settings::default());
// Update settings // Update settings
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.set_settings(settings, cx); this.set_settings(settings, cx);
})?; })
.ok();
Ok(()) })
}), .detach();
);
} }
/// Save settings /// Save settings
pub fn save(&mut self, cx: &mut Context<Self>) { pub fn save(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
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 path = config_dir().join(".settings");
let public_key = signer.get_public_key().await?;
let content = serde_json::to_string(&settings)?; let content = serde_json::to_string(&settings)?;
let event = EventBuilder::new(Kind::ApplicationSpecificData, content) // Write settings to file
.tag(Tag::identifier(SETTINGS_IDENTIFIER)) smol::fs::write(&path, content).await?;
.build(public_key)
.sign(&Keys::generate())
.await?;
// Save event to the local database only
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

View File

@@ -13,7 +13,7 @@ pub const APP_ID: &str = "su.reya.coop";
pub const KEYRING: &str = "Coop Safe Storage"; pub const KEYRING: &str = "Coop Safe Storage";
/// Default timeout for subscription /// Default timeout for subscription
pub const TIMEOUT: u64 = 3; pub const TIMEOUT: u64 = 2;
/// Default delay for searching /// Default delay for searching
pub const FIND_DELAY: u64 = 600; pub const FIND_DELAY: u64 = 600;

View File

@@ -1,107 +0,0 @@
use std::collections::{HashMap, HashSet};
use nostr_sdk::prelude::*;
/// Gossip
#[derive(Debug, Clone, Default)]
pub struct Gossip {
/// Gossip relays for each public key
relays: HashMap<PublicKey, HashSet<(RelayUrl, Option<RelayMetadata>)>>,
/// Messaging relays for each public key
messaging_relays: HashMap<PublicKey, HashSet<RelayUrl>>,
}
impl Gossip {
/// Get read relays for a given public key
pub fn read_relays(&self, public_key: &PublicKey) -> Vec<RelayUrl> {
self.relays
.get(public_key)
.map(|relays| {
relays
.iter()
.filter_map(|(url, metadata)| {
if metadata.is_none() || metadata == &Some(RelayMetadata::Read) {
Some(url.to_owned())
} else {
None
}
})
.collect()
})
.unwrap_or_default()
}
/// Get write relays for a given public key
pub fn write_relays(&self, public_key: &PublicKey) -> Vec<RelayUrl> {
self.relays
.get(public_key)
.map(|relays| {
relays
.iter()
.filter_map(|(url, metadata)| {
if metadata.is_none() || metadata == &Some(RelayMetadata::Write) {
Some(url.to_owned())
} else {
None
}
})
.collect()
})
.unwrap_or_default()
}
/// Insert gossip relays for a public key
pub fn insert_relays(&mut self, event: &Event) {
self.relays.entry(event.pubkey).or_default().extend(
event
.tags
.iter()
.filter_map(|tag| {
if let Some(TagStandard::RelayMetadata {
relay_url,
metadata,
}) = tag.clone().to_standardized()
{
Some((relay_url, metadata))
} else {
None
}
})
.take(3),
);
log::info!("Updating gossip relays for: {}", event.pubkey);
}
/// Get messaging relays for a given public key
pub fn messaging_relays(&self, public_key: &PublicKey) -> Vec<RelayUrl> {
self.messaging_relays
.get(public_key)
.cloned()
.unwrap_or_default()
.into_iter()
.collect()
}
/// Insert messaging relays for a public key
pub fn insert_messaging_relays(&mut self, event: &Event) {
self.messaging_relays
.entry(event.pubkey)
.or_default()
.extend(
event
.tags
.iter()
.filter_map(|tag| {
if let Some(TagStandard::Relay(url)) = tag.as_standardized() {
Some(url.to_owned())
} else {
None
}
})
.take(3),
);
log::info!("Updating messaging relays for: {}", event.pubkey);
}
}

View File

@@ -107,9 +107,16 @@ impl NostrRegistry {
let client = ClientBuilder::default() let client = ClientBuilder::default()
.signer(signer.clone()) .signer(signer.clone())
.gossip(NostrGossipMemory::unbounded()) .gossip(NostrGossipMemory::unbounded())
.gossip_config(
GossipConfig::default()
.fetch_timeout(Duration::from_secs(TIMEOUT))
.sync_initial_timeout(Duration::from_secs(2))
.sync_idle_timeout(Duration::from_secs(TIMEOUT)),
)
.database(lmdb) .database(lmdb)
.automatic_authentication(false) .automatic_authentication(false)
.verify_subscriptions(false) .verify_subscriptions(false)
.connect_timeout(Duration::from_secs(TIMEOUT))
.sleep_when_idle(SleepWhenIdle::Enabled { .sleep_when_idle(SleepWhenIdle::Enabled {
timeout: Duration::from_secs(600), timeout: Duration::from_secs(600),
}) })
@@ -174,6 +181,15 @@ impl NostrRegistry {
// Update the state // Update the state
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.set_connected(cx); this.set_connected(cx);
})?;
// Small delay
cx.background_executor()
.timer(Duration::from_millis(200))
.await;
// Update the state
this.update(cx, |this, cx| {
this.get_signer(cx); this.get_signer(cx);
})?; })?;
@@ -655,8 +671,11 @@ impl NostrRegistry {
// Publish relay list event // Publish relay list event
let event = EventBuilder::relay_list(relay_list).sign(signer).await?; let event = EventBuilder::relay_list(relay_list).sign(signer).await?;
let output = client.send_event(&event).broadcast().await?; client
log::info!("Published relay list event: {:?}", output.id()); .send_event(&event)
.broadcast()
.ok_timeout(Duration::from_secs(TIMEOUT))
.await?;
// Construct the default metadata // Construct the default metadata
let name = petname::petname(2, "-").unwrap_or("Cooper".to_string()); let name = petname::petname(2, "-").unwrap_or("Cooper".to_string());
@@ -665,24 +684,33 @@ impl NostrRegistry {
// Publish metadata event // Publish metadata event
let event = EventBuilder::metadata(&metadata).sign(signer).await?; let event = EventBuilder::metadata(&metadata).sign(signer).await?;
let output = client.send_event(&event).broadcast().await?; client
log::info!("Published metadata event: {:?}", output.id()); .send_event(&event)
.broadcast()
.ok_timeout(Duration::from_secs(TIMEOUT))
.await?;
// Construct the default contact list // Construct the default contact list
let contacts = vec![Contact::new(PublicKey::parse(COOP_PUBKEY).unwrap())]; let contacts = vec![Contact::new(PublicKey::parse(COOP_PUBKEY).unwrap())];
// Publish contact list event // Publish contact list event
let event = EventBuilder::contact_list(contacts).sign(signer).await?; let event = EventBuilder::contact_list(contacts).sign(signer).await?;
let output = client.send_event(&event).broadcast().await?; client
log::info!("Published contact list event: {:?}", output.id()); .send_event(&event)
.broadcast()
.ok_timeout(Duration::from_secs(TIMEOUT))
.await?;
// Construct the default messaging relay list // Construct the default messaging relay list
let relays = default_messaging_relays(); let relays = default_messaging_relays();
// Publish messaging relay list event // Publish messaging relay list event
let event = EventBuilder::nip17_relay_list(relays).sign(signer).await?; let event = EventBuilder::nip17_relay_list(relays).sign(signer).await?;
let output = client.send_event(&event).to_nip65().await?; client
log::info!("Published messaging relay list event: {:?}", output.id()); .send_event(&event)
.to_nip65()
.ok_timeout(Duration::from_secs(TIMEOUT))
.await?;
// Write user's credentials to the system keyring // Write user's credentials to the system keyring
write_credential.await?; write_credential.await?;

View File

@@ -1009,8 +1009,7 @@ impl InputState {
let left_part = self.text.slice(0..offset).to_string(); let left_part = self.text.slice(0..offset).to_string();
UnicodeSegmentation::split_word_bound_indices(left_part.as_str()) UnicodeSegmentation::split_word_bound_indices(left_part.as_str())
.filter(|(_, s)| !s.trim_start().is_empty()) .rfind(|(_, s)| !s.trim_start().is_empty())
.next_back()
.map(|(i, _)| i) .map(|(i, _)| i)
.unwrap_or(0) .unwrap_or(0)
} }

View File

@@ -1094,13 +1094,10 @@ impl Render for PopupMenu {
self.update_submenu_menu_anchor(window); self.update_submenu_menu_anchor(window);
let max_width = self.max_width(); let max_width = self.max_width();
let max_height = self.max_height.map_or_else( let max_height = self.max_height.unwrap_or_else(|| {
|| {
let window_half_height = window.window_bounds().get_bounds().size.height * 0.5; let window_half_height = window.window_bounds().get_bounds().size.height * 0.5;
window_half_height.min(px(450.)) window_half_height.min(px(450.))
}, });
|height| height,
);
let view = cx.entity().clone(); let view = cx.entity().clone();
let items_count = self.menu_items.len(); let items_count = self.menu_items.len();

View File

@@ -1,5 +1,5 @@
[toolchain] [toolchain]
channel = "1.91" channel = "1.92"
profile = "minimal" profile = "minimal"
components = ["rustfmt", "clippy"] components = ["rustfmt", "clippy"]
targets = [ targets = [