@@ -170,7 +170,7 @@ use crate::*;
170170pub struct EditorBuffer {
171171 editor_content : EditorContent ,
172172 pub ( crate ) history : EditorBufferHistory ,
173- pub render_cache : RenderCache ,
173+ pub render_cache : HashMap < String , RenderOps > ,
174174}
175175
176176#[ derive( Clone , PartialEq , Serialize , Deserialize , GetSize , Default ) ]
@@ -188,12 +188,6 @@ pub struct EditorBufferHistory {
188188 current_index : isize ,
189189}
190190
191- #[ derive( Clone , PartialEq , Serialize , Deserialize , Default , GetSize ) ]
192- pub struct RenderCache {
193- cache_map : HashMap < String , RenderOps > ,
194- editor_content : EditorContent ,
195- }
196-
197191impl Default for EditorBufferHistory {
198192 fn default ( ) -> Self {
199193 Self {
@@ -211,6 +205,9 @@ pub mod history {
211205 }
212206
213207 pub fn push ( editor_buffer : & mut EditorBuffer ) {
208+ // Invalidate the content cache, since the content just changed.
209+ cache:: clear ( editor_buffer) ;
210+
214211 let content_copy = editor_buffer. editor_content . clone ( ) ;
215212
216213 // Delete the history from the current version index to the end.
@@ -233,6 +230,9 @@ pub mod history {
233230 }
234231
235232 pub fn undo ( editor_buffer : & mut EditorBuffer ) {
233+ // Invalidate the content cache, since the content just changed.
234+ cache:: clear ( editor_buffer) ;
235+
236236 let retain_caret_position = editor_buffer. editor_content . caret_display_position ;
237237 if let Some ( content) = editor_buffer. history . previous_content ( ) {
238238 editor_buffer. editor_content = content;
@@ -245,6 +245,9 @@ pub mod history {
245245 }
246246
247247 pub fn redo ( editor_buffer : & mut EditorBuffer ) {
248+ // Invalidate the content cache, since the content just changed.
249+ cache:: clear ( editor_buffer) ;
250+
248251 if let Some ( content) = editor_buffer. history . next_content ( ) {
249252 editor_buffer. editor_content = content;
250253 }
@@ -546,47 +549,49 @@ mod constructor {
546549pub mod cache {
547550 use super :: * ;
548551
549- impl EditorBuffer {
550- /// Render the content of the editor buffer to the screen from the cache if the content
551- /// has not been modified.
552- ///
553- /// The cache miss occurs if
554- /// - Scroll Offset changes
555- /// - Window size changes
556- /// - Content of the editor changes
557- pub fn render_content_from_cache (
558- & mut self ,
559- key : String ,
560- render_args : & RenderArgs < ' _ > ,
561- render_ops : & mut RenderOps ,
562- ) {
563- if let Some ( cached_output) = self . render_cache . cache_map . get ( & key) {
564- if & self . render_cache . editor_content . lines == & self . editor_content . lines {
565- // Cache hit
566- * render_ops = cached_output. clone ( ) ;
567- } else {
568- // Content has been modified.
569- self . handle_cache_miss ( key, render_args, render_ops) ;
570- }
571- } else {
572- // Scroll Offset or Window size has been modified.
573- self . handle_cache_miss ( key, render_args, render_ops) ;
574- }
575- }
552+ pub fn clear ( editor_buffer : & mut EditorBuffer ) { editor_buffer. render_cache . clear ( ) ; }
576553
577- /// When the cache miss occurs, the entire buffer needs to be re-rendered.
578- /// Cache Map is cleared and the new render ops are stored in the cache.
579- fn handle_cache_miss (
580- & mut self ,
581- key : String ,
582- render_args : & RenderArgs < ' _ > ,
583- render_ops : & mut RenderOps ,
584- ) {
585- self . render_cache . cache_map . clear ( ) ;
586- EditorEngineApi :: render_content ( render_args, render_ops) ;
587- self . render_cache . editor_content = self . editor_content . clone ( ) ;
588- self . render_cache . cache_map . insert ( key, render_ops. clone ( ) ) ;
589- }
554+ /// Cache key is combination of scroll_offset and window_size.
555+ fn generate_key ( editor_buffer : & EditorBuffer , window_size : Size ) -> String {
556+ format ! ( "{}{}" , editor_buffer. get_scroll_offset( ) , window_size, )
557+ }
558+
559+ /// Render the content of the editor buffer to the screen from the cache if the content
560+ /// has not been modified.
561+ ///
562+ /// The cache miss occurs if
563+ /// - Scroll Offset changes
564+ /// - Window size changes
565+ /// - Content of the editor changes
566+ pub fn render_content (
567+ editor_buffer : & mut EditorBuffer ,
568+ editor_engine : & mut EditorEngine ,
569+ window_size : Size ,
570+ has_focus : & mut HasFocus ,
571+ render_ops : & mut RenderOps ,
572+ ) {
573+ let key = generate_key ( editor_buffer, window_size) ;
574+ if let Some ( cached_output) = editor_buffer. render_cache . get ( & key) {
575+ // Cache hit
576+ * render_ops = cached_output. clone ( ) ;
577+ return ;
578+ }
579+
580+ // Cache miss, due to either:
581+ // - Content has been modified.
582+ // - Scroll Offset or Window size has been modified.
583+ editor_buffer. render_cache . clear ( ) ;
584+ let render_args = RenderArgs {
585+ editor_engine,
586+ editor_buffer,
587+ has_focus,
588+ } ;
589+
590+ // Re-render content, generate & write to render_ops.
591+ EditorEngineApi :: render_content ( & render_args, render_ops) ;
592+
593+ // Snapshot the render_ops in the cache.
594+ editor_buffer. render_cache . insert ( key, render_ops. clone ( ) ) ;
590595 }
591596}
592597pub enum CaretKind {
0 commit comments