Skip to content

Commit d74bee5

Browse files
committed
Fix first performance performance problem
1 parent f4fca5a commit d74bee5

File tree

3 files changed

+48
-29
lines changed

3 files changed

+48
-29
lines changed

crates/epaint/src/text/fonts.rs

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ impl GalleyCache {
813813
) -> Galley {
814814
profiling::function_scope!();
815815

816+
let mut current_section = 0;
816817
let mut start = 0;
817818
let mut max_rows_remaining = job.wrap.max_rows;
818819
let mut galleys = Vec::new();
@@ -841,36 +842,49 @@ impl GalleyCache {
841842
round_output_to_gui: job.round_output_to_gui,
842843
};
843844

844-
// Keep all (previous) sections (so the correct `section_index` is outputted),
845-
// but shift their byte ranges:
846-
paragraph_job.sections = job
847-
.sections
848-
.iter()
849-
.cloned()
850-
.filter_map(|section| {
851-
let LayoutSection {
852-
leading_space,
853-
byte_range,
854-
format,
855-
} = section;
856-
if end <= byte_range.start {
857-
None
858-
} else {
859-
let byte_range = byte_range.start.saturating_sub(start)
860-
..byte_range.end.saturating_sub(start).min(end - start);
861-
Some(LayoutSection {
862-
leading_space: if byte_range.end == 0 {
863-
0.0 // this section is behind us (unused in this paragraph)
864-
} else {
865-
leading_space
866-
},
867-
byte_range,
868-
format,
869-
})
870-
}
871-
})
872-
.collect();
845+
// Add overlapping sections:
846+
for section in &job.sections[current_section..job.sections.len()] {
847+
let LayoutSection {
848+
leading_space,
849+
byte_range: section_range,
850+
format,
851+
} = section;
852+
853+
// dbg!(section_index, section_range);
854+
855+
// `start` and `end` are the byte range of the current paragraph.
856+
// How does the current section overlap with the paragraph range?
857+
858+
if section_range.end <= start {
859+
// The section is behind us
860+
current_section += 1;
861+
} else if end <= section_range.start {
862+
break; // Haven't reached this one yet.
863+
} else {
864+
// Section range overlaps with paragraph range
865+
debug_assert!(
866+
section_range.start < section_range.end,
867+
"Bad byte_range: {section_range:?}"
868+
);
869+
let new_range = section_range.start.saturating_sub(start)
870+
..(section_range.end.at_most(end)).saturating_sub(start);
871+
debug_assert!(
872+
new_range.start <= new_range.end,
873+
"Bad new section range: {new_range:?}"
874+
);
875+
paragraph_job.sections.push(LayoutSection {
876+
leading_space: if start <= new_range.start {
877+
*leading_space
878+
} else {
879+
0.0
880+
},
881+
byte_range: new_range,
882+
format: format.clone(),
883+
});
884+
}
885+
}
873886

887+
// TODO(emilk): we could lay out each paragraph in parallel to get a nice speedup on multicore machines.
874888
let galley = self.layout(fonts, paragraph_job, false);
875889

876890
// This will prevent us from invalidating cache entries unnecessarily:
@@ -891,6 +905,7 @@ impl GalleyCache {
891905
start = end;
892906
}
893907

908+
// TODO: adjust section_index in the concatted galley
894909
Galley::concat(job, &galleys, fonts.pixels_per_point)
895910
}
896911

crates/epaint/src/text/text_layout.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ impl Paragraph {
6969
/// In most cases you should use [`crate::Fonts::layout_job`] instead
7070
/// since that memoizes the input, making subsequent layouting of the same text much faster.
7171
pub fn layout(fonts: &mut FontsImpl, job: Arc<LayoutJob>) -> Galley {
72+
profiling::function_scope!();
73+
7274
if job.wrap.max_rows == 0 {
7375
// Early-out: no text
7476
return Galley {

crates/epaint/src/text/text_layout_types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,8 @@ impl Galley {
792792

793793
/// Append each galley under the previous one.
794794
pub fn concat(job: Arc<LayoutJob>, galleys: &[Arc<Self>], pixels_per_point: f32) -> Self {
795+
profiling::function_scope!();
796+
795797
let mut merged_galley = Self {
796798
job,
797799
rows: Vec::new(),

0 commit comments

Comments
 (0)