chore: fix some performance issues (#6)
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 1m14s

Reviewed-on: #6
This commit was merged in pull request #6.
This commit is contained in:
2026-02-14 02:01:49 +00:00
parent ecd7f6aa9b
commit e327178161
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
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);
})?;

View File

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

View File

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

View File

@@ -1,13 +1,11 @@
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 nostr_sdk::prelude::*;
use serde::{Deserialize, Serialize};
use smallvec::{smallvec, SmallVec};
use state::NostrRegistry;
const SETTINGS_IDENTIFIER: &str = "coop:settings";
pub fn init(cx: &mut App) {
AppSettings::set_global(cx.new(AppSettings::new), cx)
@@ -118,10 +116,7 @@ pub struct AppSettings {
values: Settings,
/// Event subscriptions
_subscriptions: SmallVec<[Subscription; 1]>,
/// Background tasks
tasks: SmallVec<[Task<Result<(), Error>>; 1]>,
_subscriptions: SmallVec<[Subscription; 2]>,
}
impl AppSettings {
@@ -136,9 +131,6 @@ impl AppSettings {
}
fn new(cx: &mut Context<Self>) -> Self {
let load_settings = Self::get_from_database(cx);
let mut tasks = smallvec![];
let mut subscriptions = smallvec![];
subscriptions.push(
@@ -148,24 +140,16 @@ impl AppSettings {
}),
);
tasks.push(
// Load the initial settings
cx.spawn(async move |this, cx| {
let settings = load_settings.await.unwrap_or(Settings::default());
log::info!("Settings: {settings:?}");
cx.defer(|cx| {
let settings = AppSettings::global(cx);
// Update the settings state
this.update(cx, |this, cx| {
this.set_settings(settings, cx);
})?;
Ok(())
}),
);
settings.update(cx, |this, cx| {
this.load(cx);
});
});
Self {
values: Settings::default(),
tasks,
_subscriptions: subscriptions,
}
}
@@ -176,74 +160,45 @@ impl AppSettings {
cx.notify();
}
/// Get settings from the database
fn get_from_database(cx: &App) -> Task<Result<Settings, Error>> {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
/// Load settings
fn load(&mut self, cx: &mut Context<Self>) {
let task: Task<Result<Settings, Error>> = cx.background_spawn(async move {
let path = config_dir().join(".settings");
cx.background_spawn(async move {
// Construct a filter to get the latest settings
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)?)
if let Ok(content) = smol::fs::read_to_string(&path).await {
Ok(serde_json::from_str(&content)?)
} else {
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();
})
}
/// 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(())
}),
);
.detach();
}
/// Save settings
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();
self.tasks.push(cx.background_spawn(async move {
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let path = config_dir().join(".settings");
let content = serde_json::to_string(&settings)?;
let event = EventBuilder::new(Kind::ApplicationSpecificData, content)
.tag(Tag::identifier(SETTINGS_IDENTIFIER))
.build(public_key)
.sign(&Keys::generate())
.await?;
// Save event to the local database only
client.database().save_event(&event).await?;
// Write settings to file
smol::fs::write(&path, content).await?;
Ok(())
}));
});
task.detach();
}
/// 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";
/// Default timeout for subscription
pub const TIMEOUT: u64 = 3;
pub const TIMEOUT: u64 = 2;
/// Default delay for searching
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()
.signer(signer.clone())
.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)
.automatic_authentication(false)
.verify_subscriptions(false)
.connect_timeout(Duration::from_secs(TIMEOUT))
.sleep_when_idle(SleepWhenIdle::Enabled {
timeout: Duration::from_secs(600),
})
@@ -174,6 +181,15 @@ impl NostrRegistry {
// Update the state
this.update(cx, |this, 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);
})?;
@@ -655,8 +671,11 @@ impl NostrRegistry {
// Publish relay list event
let event = EventBuilder::relay_list(relay_list).sign(signer).await?;
let output = client.send_event(&event).broadcast().await?;
log::info!("Published relay list event: {:?}", output.id());
client
.send_event(&event)
.broadcast()
.ok_timeout(Duration::from_secs(TIMEOUT))
.await?;
// Construct the default metadata
let name = petname::petname(2, "-").unwrap_or("Cooper".to_string());
@@ -665,24 +684,33 @@ impl NostrRegistry {
// Publish metadata event
let event = EventBuilder::metadata(&metadata).sign(signer).await?;
let output = client.send_event(&event).broadcast().await?;
log::info!("Published metadata event: {:?}", output.id());
client
.send_event(&event)
.broadcast()
.ok_timeout(Duration::from_secs(TIMEOUT))
.await?;
// Construct the default contact list
let contacts = vec![Contact::new(PublicKey::parse(COOP_PUBKEY).unwrap())];
// Publish contact list event
let event = EventBuilder::contact_list(contacts).sign(signer).await?;
let output = client.send_event(&event).broadcast().await?;
log::info!("Published contact list event: {:?}", output.id());
client
.send_event(&event)
.broadcast()
.ok_timeout(Duration::from_secs(TIMEOUT))
.await?;
// Construct the default messaging relay list
let relays = default_messaging_relays();
// Publish messaging relay list event
let event = EventBuilder::nip17_relay_list(relays).sign(signer).await?;
let output = client.send_event(&event).to_nip65().await?;
log::info!("Published messaging relay list event: {:?}", output.id());
client
.send_event(&event)
.to_nip65()
.ok_timeout(Duration::from_secs(TIMEOUT))
.await?;
// Write user's credentials to the system keyring
write_credential.await?;

View File

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

View File

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

View File

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