From 1d0c99b1367adfdfc16ab8f457c0f388611eb89c Mon Sep 17 00:00:00 2001 From: Jesse Doyle Date: Thu, 1 Sep 2022 22:56:42 -0600 Subject: [PATCH] optimize(fons): Memoize Font Path * Memoize the `Prawn::Icon::FontData#path` method as it performs a filesystem glob and is currently as hot code path. * Update the README to include Material Design Icons. --- CHANGELOG.md | 1 + README.md | 1 + lib/prawn/icon/font_data.rb | 22 ++++++++++++---------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44953bc..5f97ac4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * Update our CI matrix to include recent versions of Prawn and Ruby! Thanks @petergoldstein! (#55) * Resolve a few code smells that were flagged by Rubocop. * [Material Design Icons](https://materialdesignicons.com) are now supported! Currently version `7.0.96` is included. Thanks @maneex! [https://github.com/jessedoyle/prawn-icon/pull/59](Pull Request). +* Memoize calls to `Prawn::Icon::FontData#path` to improve performance. #### Material Design Icons diff --git a/README.md b/README.md index 0dfb38c..5fd8c29 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ Prawn::Icon provides a simple mechanism for rendering icons and icon fonts from The following icon fonts ship with Prawn::Icon: * FontAwesome (http://fontawesome.io/icons/) +* Material Design Icons (https://materialdesignicons.com/) * Foundation Icons (http://zurb.com/playground/foundation-icon-fonts-3) * PaymentFont (https://paymentfont.com) diff --git a/lib/prawn/icon/font_data.rb b/lib/prawn/icon/font_data.rb index 6669b1b..c36e74d 100644 --- a/lib/prawn/icon/font_data.rb +++ b/lib/prawn/icon/font_data.rb @@ -63,17 +63,19 @@ def load_fonts(document) end def path - font = Icon.configuration.font_directory - .join(@set.to_s) - .glob('*.ttf') - .first - - if font.nil? - raise Prawn::Errors::UnknownFont, - "Icon font not found for set: #{@set}" - end + @path = begin + font = Icon.configuration.font_directory + .join(@set.to_s) + .glob('*.ttf') + .first + + if font.nil? + raise Prawn::Errors::UnknownFont, + "Icon font not found for set: #{@set}" + end - @path ||= font.to_s + font.to_s + end end def specifier