perf: reuse rendered elements across repaints
Rust / build (macos-latest, stable) (push) Canceled after 0s
Rust / build (ubuntu-latest, stable) (push) Canceled after 0s
Rust / build (windows-latest, stable) (push) Canceled after 0s

This commit is contained in:
2026-11-25 05:00:06 +07:00
parent 3f64b36ff4
commit bf06d29c2e
21 changed files with 603 additions and 316 deletions
+35 -22
View File
@@ -4,14 +4,14 @@ use std::sync::Arc;
use gpui::prelude::FluentBuilder as _;
use gpui::{
AnyElement, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement,
ParentElement as _, Render, SharedString, Styled as _, Subscription, UniformListScrollHandle,
WeakEntity, Window, div,
ParentElement as _, Render, SharedString, StyleRefinement, Styled as _, Subscription,
UniformListScrollHandle, WeakEntity, Window, div,
};
use metadata::ProfileDirectory;
use nostr::prelude::{EventId, Kind, Url};
use signed_core::{GitEvent, PullRequest, RepoAddr};
use signed_git::{FileCommit, PatchParser};
use signed_state::{Backend, Mirrors, RepoStore};
use signed_state::{Backend, BackendEvent, Mirrors, RepoStore};
use ui::avatar::Avatar;
use ui::button::Button;
use ui::clipboard::ClipboardButton;
@@ -20,15 +20,16 @@ use ui::empty_state::EmptyState;
use ui::icons::{Icon, IconRole};
use ui::input::InputState;
use ui::layout::panel::Panel;
use ui::layout::root::RootContext as _;
use ui::scroll_area::ScrollArea;
use ui::spinner::Spinner;
use ui::tabs::{Tab, Tabs};
use ui::theme::ActiveTheme as _;
use utils::relative_time;
use super::dialogs;
use crate::app::AppRoot;
use crate::issues::helpers::{comment_form, comments_section, sidebar_section};
use crate::pull_requests::dialogs::update::UpdatePrDialog;
use crate::repo::commit_diff::{CommitDiffView, DiffPane};
use crate::shared::event_list::commit_list;
use crate::shared::log_dropped;
@@ -52,7 +53,6 @@ pub struct PullRequestDetailView {
workspace: WeakEntity<AppRoot>,
store: Entity<RepoStore>,
pr_id: EventId,
update_dialog: Option<Entity<dialogs::update::UpdatePrDialog>>,
comment_input: Entity<InputState>,
worktree: Option<PathBuf>,
description: SharedString,
@@ -67,7 +67,7 @@ pub struct PullRequestDetailView {
commit_scroll_handle: UniformListScrollHandle,
tasks: Vec<gpui::Task<Result<(), anyhow::Error>>>,
directory: Entity<ProfileDirectory>,
_subscription: Subscription,
_subscriptions: Vec<Subscription>,
}
impl PullRequestDetailView {
@@ -78,23 +78,35 @@ impl PullRequestDetailView {
_window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
let pane = cx.new(DiffPane::new);
let comment_input = InputState::builder().multiline().build(cx);
let directory = ProfileDirectory::new(cx);
let pane = cx.new(DiffPane::new);
let weak = cx.entity().downgrade();
let subscription = cx.observe(&store, |this, _store, cx| {
let backend = Backend::global(cx);
let mut subscriptions = vec![];
subscriptions.push(cx.observe(&store, |this, _store, cx| {
this.sync(cx);
});
}));
subscriptions.push(cx.subscribe(&backend, |_, _, event, cx| {
if matches!(event, BackendEvent::SignerChanged) {
cx.notify();
}
}));
cx.defer(move |cx| {
log_dropped(weak.update(cx, |this, cx| this.sync(cx)));
log_dropped(weak.update(cx, |this, cx| {
this.sync(cx);
}));
});
Self {
workspace,
store,
pr_id,
update_dialog: None,
comment_input,
worktree: None,
description: SharedString::default(),
@@ -108,8 +120,8 @@ impl PullRequestDetailView {
pane,
commit_scroll_handle: UniformListScrollHandle::new(),
tasks: Vec::new(),
directory: ProfileDirectory::new(cx),
_subscription: subscription,
directory,
_subscriptions: subscriptions,
}
}
@@ -445,10 +457,15 @@ impl PullRequestDetailView {
if self.loading {
return Spinner::centered("pr-loading");
}
if let Some(error) = self.error.clone() {
return EmptyState::centered(error);
}
self.pane.clone().into_any_element()
self.pane
.clone()
.cached(StyleRefinement::default().size_full().min_h_0())
.into_any_element()
}
fn render_commits(&self, cx: &mut Context<Self>) -> AnyElement {
@@ -545,14 +562,11 @@ impl PullRequestDetailView {
pr.id == this.pr_id && pr.kind == Kind::GitPullRequest
})
.cloned();
if let Some(root) = root {
this.update_dialog =
Some(dialogs::update::UpdatePrDialog::new(
this.store.clone(),
root,
cx,
));
cx.notify();
let dialog =
UpdatePrDialog::new(this.store.clone(), root, cx);
cx.open_dialog(dialog);
}
}));
},
@@ -597,7 +611,6 @@ impl Render for PullRequestDetailView {
.size_full()
.min_h_0()
.child(self.render_header(cx))
.when_some(self.update_dialog.clone(), |el, view| el.child(view))
.child(self.render_tabs(cx))
.map(|this| match self.active_tab {
0 => this.child(self.render_discussion(cx)),
+15 -10
View File
@@ -1,4 +1,5 @@
use std::path::PathBuf;
use std::sync::Arc;
use gpui::{SharedString, WeakEntity};
use nostr::prelude::PublicKey;
@@ -66,27 +67,29 @@ pub fn truncate_label(label: &str) -> SharedString {
pub fn source_menu_items(
view: WeakEntity<NewPullRequestView>,
associated: Vec<PathBuf>,
associated: Arc<Vec<PathBuf>>,
active_path: Option<PathBuf>,
candidates: Vec<Announcement>,
candidates: Arc<Vec<Announcement>>,
user: Option<PublicKey>,
active_fork: Option<RepoAddr>,
) -> Vec<MenuItem> {
let mut items = Vec::new();
for path in associated {
for (index, path) in associated.iter().enumerate() {
let label = path
.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_else(|| path.display().to_string());
let target = view.clone();
let path_for_action = path.clone();
let associated = Arc::clone(&associated);
items.push(
MenuItem::new(truncate_label(&label))
.checked(active_path.as_ref() == Some(&path))
.checked(active_path.as_ref() == Some(path))
.handler(move |window, cx| {
let path = path_for_action.clone();
let Some(path) = associated.get(index).cloned() else {
return;
};
if let Err(error) =
target.update(cx, |this, cx| this.apply_folder_path(path, window, cx))
{
@@ -110,23 +113,25 @@ pub fn source_menu_items(
if candidates.is_empty() {
items.push(MenuItem::new("No announced forks of this repository").disabled(true));
} else {
for candidate in candidates {
for (index, candidate) in candidates.iter().enumerate() {
let subtitle = if Some(candidate.owner) == user {
SharedString::from("Your fork")
} else {
SharedString::from(format!("by {}", shorten_owner(&candidate.owner)))
};
let title = format!("{} — {}", announcement_display_name(&candidate), subtitle);
let title = format!("{} — {}", announcement_display_name(candidate), subtitle);
let target = view.clone();
let candidates = Arc::clone(&candidates);
let checked = active_fork == Some(candidate.addr());
let announcement = candidate;
items.push(
MenuItem::new(truncate_label(&title))
.checked(checked)
.handler(move |window, cx| {
let announcement = announcement.clone();
let Some(announcement) = candidates.get(index).cloned() else {
return;
};
if let Err(error) =
target.update(cx, |this, cx| this.choose_fork(announcement, window, cx))
{
+106 -40
View File
@@ -4,13 +4,13 @@ use std::sync::Arc;
use gpui::prelude::FluentBuilder as _;
use gpui::{
AnyElement, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement,
ParentElement as _, PathPromptOptions, Render, SharedString, Styled as _,
UniformListScrollHandle, WeakEntity, Window, div, px,
ParentElement as _, PathPromptOptions, Render, SharedString, StyleRefinement, Styled as _,
Subscription, UniformListScrollHandle, WeakEntity, Window, div, px,
};
use nostr::prelude::Url;
use nostr::prelude::{PublicKey, Url};
use signed_core::Announcement;
use signed_git::{GitCache, Repo};
use signed_state::{Backend, CheckoutsStore, Mirrors, RepoListStore, RepoStore};
use signed_state::{Backend, BackendEvent, CheckoutsStore, Mirrors, RepoListStore, RepoStore};
use ui::button::{Button, ButtonVariant};
use ui::combobox::{Combobox, ComboboxState};
use ui::common::{StyledExt as _, h_flex, v_flex};
@@ -56,6 +56,11 @@ pub struct NewPullRequestView {
scroll_handle: UniformListScrollHandle,
pub(super) tasks: Vec<gpui::Task<Result<(), anyhow::Error>>>,
source_open: bool,
/// The checkouts, forks, and user the source menu lists.
checkouts: Arc<Vec<PathBuf>>,
forks: Arc<Vec<Announcement>>,
user: Option<PublicKey>,
_subscriptions: Vec<Subscription>,
}
impl NewPullRequestView {
@@ -66,12 +71,16 @@ impl NewPullRequestView {
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
let pane = cx.new(DiffPane::new);
let checkouts = CheckoutsStore::global(cx);
let repositories = RepoListStore::global(cx);
let backend = Backend::global(cx);
let weak = cx.entity().downgrade();
let subject = InputState::new(cx);
let description = InputState::builder().multiline().build(cx);
let pane = cx.new(DiffPane::new);
let weak = cx.entity().downgrade();
let base_select = cx.new(|cx| {
ComboboxState::new(
Combobox::new("pr-base", "Base", Vec::new()).on_change({
@@ -114,23 +123,42 @@ impl NewPullRequestView {
)
});
window.defer(cx, move |window, cx| {
log_dropped(weak.update(cx, |this, cx| {
let Some(addr) = this.store.read(cx).addr().cloned() else {
return;
};
let mut subscriptions = vec![];
let Some(path) = CheckoutsStore::global(cx)
.read(cx)
.associations_of(&addr)
.into_iter()
.next()
else {
return;
};
subscriptions.push(cx.observe(&checkouts, |this, _, cx| {
this.refresh_sources(cx);
}));
this.apply_folder_path(path, window, cx);
}));
subscriptions.push(cx.observe(&repositories, |this, _, cx| {
this.refresh_sources(cx);
}));
subscriptions.push(cx.observe(&store, |this, _, cx| {
this.refresh_sources(cx);
}));
subscriptions.push(cx.subscribe(&backend, |this, _, event, cx| {
if matches!(event, BackendEvent::SignerChanged) {
this.refresh_sources(cx);
}
}));
cx.defer_in(window, move |this, window, cx| {
let Some(addr) = this.store.read(cx).addr().cloned() else {
return;
};
let Some(path) = CheckoutsStore::global(cx)
.read(cx)
.associations_of(&addr)
.into_iter()
.next()
else {
return;
};
this.apply_folder_path(path, window, cx);
this.refresh_sources(cx);
});
Self {
@@ -158,6 +186,10 @@ impl NewPullRequestView {
scroll_handle: UniformListScrollHandle::new(),
tasks: Vec::new(),
source_open: false,
checkouts: Arc::default(),
forks: Arc::default(),
user: None,
_subscriptions: subscriptions,
}
}
@@ -258,19 +290,17 @@ impl NewPullRequestView {
let weak = cx.entity().downgrade();
let weak_menu = cx.entity().downgrade();
let source_view = cx.entity().downgrade();
let associated = self
.store
.read(cx)
.addr()
.map(|addr| CheckoutsStore::global(cx).read(cx).associations_of(addr))
.unwrap_or_default();
let associated = Arc::clone(&self.checkouts);
let active_path = (self.fork.is_none())
.then(|| self.repo_path.clone())
.flatten();
let candidates = self.fork_candidates(cx);
let candidates = Arc::clone(&self.forks);
let user = signed_state::Backend::global(cx).read(cx).current_user();
let active_fork = self.fork.as_ref().map(|fork| fork.announcement.addr());
let can_submit = self.submit_enabled(cx);
let source_label = match &self.fork {
Some(fork) => truncate_label(&announcement_display_name(&fork.announcement)),
None => self.repo_path.as_ref().map_or_else(
@@ -379,14 +409,21 @@ impl NewPullRequestView {
if self.loading {
return Spinner::centered("new-pr-loading");
}
if !self.has_source() {
return EmptyState::centered("Choose a local checkout or an announced fork to compare");
}
if self.commits.is_none() && self.error.is_some() {
return EmptyState::centered("Nothing to compare");
}
match self.active_tab {
0 => self.pane.clone().into_any_element(),
0 => self
.pane
.clone()
.cached(StyleRefinement::default().size_full().min_h_0())
.into_any_element(),
_ => self.render_commits(cx),
}
}
@@ -575,22 +612,51 @@ impl NewPullRequestView {
});
}
pub(super) fn fork_candidates(&self, cx: &App) -> Vec<Announcement> {
let Some(addr) = self.store.read(cx).addr().cloned() else {
return Vec::new();
/// Rebuilds the source menu's checkouts, forks, and user when their inputs changed.
fn refresh_sources(&mut self, cx: &mut Context<Self>) {
let (addr, euc) = {
let store = self.store.read(cx);
(
store.addr().cloned(),
store
.announcement
.as_ref()
.and_then(|announcement| announcement.euc.clone()),
)
};
let euc = self
.store
.read(cx)
.announcement
let checkouts = CheckoutsStore::global(cx);
let repositories = RepoListStore::global(cx);
let backend = Backend::global(cx);
let user = backend.read(cx).current_user();
let checkouts = addr
.as_ref()
.and_then(|a| a.euc.clone());
let user = Backend::global(cx).read(cx).current_user();
let announcements = RepoListStore::global(cx).read(cx).announcements.clone();
Announcement::forks_in(&announcements, &addr, euc.as_deref(), user)
.map_or_else(Vec::new, |addr| checkouts.read(cx).associations_of(addr));
let forks: Vec<Announcement> = addr.as_ref().map_or_else(Vec::new, |addr| {
Announcement::forks_in(
&repositories.read(cx).announcements,
addr,
euc.as_deref(),
user,
)
.into_iter()
.cloned()
.collect()
});
if self.user == user
&& self.checkouts.as_slice() == checkouts.as_slice()
&& self.forks.as_slice() == forks.as_slice()
{
return;
}
self.user = user;
self.checkouts = Arc::new(checkouts);
self.forks = Arc::new(forks);
cx.notify();
}
pub(crate) fn choose_fork(