shithub: cstory

Download patch

ref: f2c0f94e429f6fae58f3e99b2cff07c6f8688b9a
parent: 729565b5cac80dbfb10ff215c5d724a4556b766c
author: Clownacy <Clownacy@users.noreply.github.com>
date: Thu Sep 17 22:55:45 EDT 2020

Faster binary search

--- a/src/Font.cpp
+++ b/src/Font.cpp
@@ -1073,37 +1073,34 @@
 	size_t left = 0;
 	size_t right = font->total_local_glyphs;
 
-	for (;;)
+	while (right - left >= 2)
 	{
-		size_t index = (left + right) / 2;
+		const size_t index = (left + right) / 2;
 
-		if (font->local_glyphs[index].unicode_value == unicode_value)
-		{
-			const Glyph *local_glyph = &font->local_glyphs[index];
+		if (font->local_glyphs[index].unicode_value > unicode_value)
+			right = index;
+		else
+			left = index;
+	}
 
-			glyph->unicode_value = local_glyph->unicode_value;
-			glyph->width = font->glyph_slot_width;
-			glyph->height = font->glyph_slot_height;
-			glyph->x_offset = 0;
-			glyph->y_offset = 0;
-			glyph->x_advance = local_glyph->x_advance;
+	if (font->local_glyphs[left].unicode_value == unicode_value)
+	{
+		const Glyph *local_glyph = &font->local_glyphs[left];
 
-			RenderBackend_UploadGlyph(font->atlas, glyph->x, glyph->y, &font->image_buffer[local_glyph->y * font->image_buffer_width + local_glyph->x], glyph->width, glyph->height, font->image_buffer_width);
+		glyph->unicode_value = local_glyph->unicode_value;
+		glyph->width = font->glyph_slot_width;
+		glyph->height = font->glyph_slot_height;
+		glyph->x_offset = 0;
+		glyph->y_offset = 0;
+		glyph->x_advance = local_glyph->x_advance;
 
-			*glyph_pointer = glyph->next;
-			glyph->next = font->glyph_list_head;
-			font->glyph_list_head = glyph;
+		RenderBackend_UploadGlyph(font->atlas, glyph->x, glyph->y, &font->image_buffer[local_glyph->y * font->image_buffer_width + local_glyph->x], glyph->width, glyph->height, font->image_buffer_width);
 
-			return glyph;
-		}
+		*glyph_pointer = glyph->next;
+		glyph->next = font->glyph_list_head;
+		font->glyph_list_head = glyph;
 
-		if (index == left)
-			break;
-
-		if (font->local_glyphs[index].unicode_value < unicode_value)
-			left = index;
-		else //if (font->local_glyphs[index].unicode_value > unicode_value)
-			right = index;
+		return glyph;
 	}
 #endif