perf: a few micro optimizations

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2025-08-03 20:12:31 -07:00
parent 7e73ed2760
commit 0dda26791a
4 changed files with 22 additions and 17 deletions

View File

@@ -56,7 +56,7 @@ pub fn hline_with_width(ui: &egui::Ui, range: egui::Rangef) {
ui.painter().hline(range, resize_y, stroke);
}
pub fn secondary_label(ui: &mut egui::Ui, s: impl Into<String>) {
pub fn secondary_label(ui: &mut egui::Ui, s: impl Into<String>) -> egui::Response {
let color = ui.style().visuals.noninteractive().fg_stroke.color;
ui.add(Label::new(RichText::new(s).size(10.0).color(color)).selectable(false));
ui.add(Label::new(RichText::new(s).size(10.0).color(color)).selectable(false))
}

View File

@@ -206,6 +206,7 @@ fn render_undecorated_note_contents<'a>(
match block.blocktype() {
BlockType::MentionBech32 => match block.as_mention().unwrap() {
Mention::Profile(profile) => {
profiling::scope!("profile-block");
let act = crate::Mention::new(
note_context.ndb,
note_context.img_cache,
@@ -220,6 +221,7 @@ fn render_undecorated_note_contents<'a>(
}
Mention::Pubkey(npub) => {
profiling::scope!("pubkey-block");
let act = crate::Mention::new(
note_context.ndb,
note_context.img_cache,
@@ -251,6 +253,7 @@ fn render_undecorated_note_contents<'a>(
},
BlockType::Hashtag => {
profiling::scope!("hashtag-block");
if block.as_str().trim().is_empty() {
continue;
}
@@ -268,6 +271,7 @@ fn render_undecorated_note_contents<'a>(
}
BlockType::Url => {
profiling::scope!("url-block");
let mut found_supported = || -> bool {
let url = block.as_str();
@@ -297,6 +301,7 @@ fn render_undecorated_note_contents<'a>(
}
BlockType::Text => {
profiling::scope!("text-block");
// truncate logic
let mut truncate = false;
let block_str = if options.contains(NoteOptions::Truncate)

View File

@@ -240,7 +240,7 @@ impl<'a, 'd> NoteView<'a, 'd> {
let (_id, rect) = ui.allocate_space(egui::vec2(50.0, 20.0));
ui.allocate_rect(rect, Sense::hover());
ui.put(rect, |ui: &mut egui::Ui| {
render_notetime(ui, self.note_context.i18n, self.note.created_at(), false).response
render_notetime(ui, self.note_context.i18n, self.note.created_at(), false)
});
let (_id, rect) = ui.allocate_space(egui::vec2(150.0, 20.0));
ui.allocate_rect(rect, Sense::hover());
@@ -398,7 +398,7 @@ impl<'a, 'd> NoteView<'a, 'd> {
let response = ui
.add(Username::new(i18n, profile.as_ref().ok(), note.pubkey()).abbreviated(20));
if !flags.contains(NoteOptions::FullCreatedDate) {
return render_notetime(ui, i18n, note.created_at(), true).response;
return render_notetime(ui, i18n, note.created_at(), true);
}
response
})
@@ -892,18 +892,18 @@ fn render_notetime(
i18n: &mut Localization,
created_at: u64,
before: bool,
) -> egui::InnerResponse<()> {
ui.horizontal(|ui| {
) -> Response {
if before {
secondary_label(ui, "");
secondary_label(
ui,
format!("{}", notedeck::time_ago_since(i18n, created_at)),
)
} else {
secondary_label(
ui,
format!("{}", notedeck::time_ago_since(i18n, created_at)),
)
}
secondary_label(ui, notedeck::time_ago_since(i18n, created_at));
if !before {
secondary_label(ui, "");
}
})
}
fn reply_button(ui: &mut egui::Ui, i18n: &mut Localization, note_key: NoteKey) -> egui::Response {

View File

@@ -116,7 +116,7 @@ fn render_text_segments(
let link_color = visuals.hyperlink_color;
for segment in segments {
match segment {
match &segment {
TextSegment::Plain(text) => {
ui.add(
Label::new(RichText::new(text).size(size).color(color)).selectable(selectable),