Clone in a terminal:
Commit 2844fc4
Johan Mattsson - 2015-05-29T05:18:13+00:00
2844fc4a8685135e641fc6b0af72873a9d2036f4johan.mattsson.m@gmail.com

Use less memory for fallback font

Changed files (1)

Diff

diff --git a/libbirdfont/Renderer/FallbackFont.vala b/libbirdfont/Renderer/FallbackFont.valaindex 758abca..aa9c5f8 100644--- a/libbirdfont/Renderer/FallbackFont.vala+++ b/libbirdfont/Renderer/FallbackFont.vala@@ -40,8 +40,10 @@ public class FallbackFont : GLib.Object { 	string default_font_file_name = "Roboto-Regular.ttf"; 	string default_font_family_name = "Roboto"; -	Gee.HashMap<unichar, Font> glyphs;-	Gee.ArrayList<unichar> cached;+	Gee.HashMap<unichar, CachePair> glyphs;+	Gee.ArrayList<CachePair> cached;++	public int max_cached_fonts = 300;  	public FallbackFont () { 		string home = Environment.get_home_dir ();@@ -60,14 +62,13 @@ public class FallbackFont : GLib.Object { 		add_font_folder ("/System/Library/Fonts"); 		add_font_folder ("/System Folder/Fonts"); 		-		glyphs = new Gee.HashMap<unichar, Font> ();-		cached = new Gee.ArrayList<unichar> ();+		glyphs = new Gee.HashMap<unichar, CachePair> ();+		cached = new Gee.ArrayList<CachePair> (); 		 		open_default_font (); 	} 	 	~FallbackFont () {-		print("Delete fallback font."); 		if (default_font != null) { 			close_font (default_font); 		}@@ -75,25 +76,52 @@ public class FallbackFont : GLib.Object {  	public Font get_single_glyph_font (unichar c) { 		Font f;+		unichar last; +		int last_index;+		CachePair p; 		-		// remove glyphs from cache if it is full-		if (cached.size > 300) {-			for (int i = 0; i < 100 && cached.size > 0; i++) {-				glyphs.unset (cached.get (cached.size - 1));-				cached.remove_at (cached.size - 1);+		if (likely (glyphs.has_key (c))) {+			p = glyphs.get (c);+			+			if (p.referenced < int.MAX) {+				p.referenced++; 			}+			+			return p.font; 		}-		-		if (glyphs.has_key (c)) {-			f = glyphs.get (c);-			return f;++		// remove glyphs from cache if it is full+		if (cached.size > max_cached_fonts - 100) {+			+			cached.sort ((a, b) => {+				CachePair pa = (CachePair) a;+				CachePair pb = (CachePair) b;+				return pb.referenced - pa.referenced;+			});+			+			int j = 0;+			for (int i = cached.size - 1; i > 0; i--) {+				CachePair ca = cached.get (i);+				+				if (j > 100) {+					break;+				}+				+				j++;+				+				last = cached.get (i).character;+				glyphs.unset (last);+				cached.remove_at (i);+			} 		}-		+				 		f = get_single_fallback_glyph_font (c);-		glyphs.set (c, f);-		cached.add (c);+		p = new CachePair (f, c); 		-		return f;+		glyphs.set (c, p);+		cached.add (p);+		+		return (Font) f; 	} 	 	Font get_single_fallback_glyph_font (unichar c) {@@ -215,6 +243,17 @@ public class FallbackFont : GLib.Object { 			default_font = open_font ((!) fn); 		} 	}+	+	class CachePair : GLib.Object {+		public Font font;+		public unichar character;+		public int referenced = 1;+		+		public CachePair (Font f, unichar c) {+			font = f;+			character = c;+		}+	} }  }

Contact

Email: johan.mattsson.m@gmail.com
Mastodon: typo.social/@birdfont
GitHub: johanmattssonm
LinkedIn: johan-mattsson-2ab324112