benchmark-subset.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include "benchmark/benchmark.h"
  2. #include <cassert>
  3. #include <cstring>
  4. #ifdef HAVE_CONFIG_H
  5. #include "config.h"
  6. #endif
  7. #include "hb-subset.h"
  8. enum operation_t
  9. {
  10. subset_codepoints,
  11. subset_glyphs,
  12. instance,
  13. };
  14. struct axis_location_t
  15. {
  16. hb_tag_t axis_tag;
  17. float axis_value;
  18. };
  19. static const axis_location_t
  20. _roboto_flex_instance_opts[] =
  21. {
  22. {HB_TAG ('w', 'g', 'h', 't'), 600.f},
  23. {HB_TAG ('w', 'd', 't', 'h'), 75.f},
  24. {HB_TAG ('o', 'p', 's', 'z'), 90.f},
  25. {HB_TAG ('G', 'R', 'A', 'D'), -100.f},
  26. {HB_TAG ('s', 'l', 'n', 't'), -3.f},
  27. {HB_TAG ('X', 'T', 'R', 'A'), 500.f},
  28. {HB_TAG ('X', 'O', 'P', 'Q'), 150.f},
  29. {HB_TAG ('Y', 'O', 'P', 'Q'), 100.f},
  30. {HB_TAG ('Y', 'T', 'L', 'C'), 480.f},
  31. {HB_TAG ('Y', 'T', 'U', 'C'), 600.f},
  32. {HB_TAG ('Y', 'T', 'A', 'S'), 800.f},
  33. {HB_TAG ('Y', 'T', 'D', 'E'), -50.f},
  34. {HB_TAG ('Y', 'T', 'F', 'I'), 600.f},
  35. };
  36. static const axis_location_t
  37. _mplus_instance_opts[] =
  38. {
  39. {HB_TAG ('w', 'g', 'h', 't'), 800.f},
  40. };
  41. template <typename Type, unsigned int n>
  42. static inline unsigned int ARRAY_LEN (const Type (&)[n]) { return n; }
  43. #define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
  44. struct test_input_t
  45. {
  46. const char *font_path;
  47. const unsigned max_subset_size;
  48. const axis_location_t *instance_opts;
  49. const unsigned num_instance_opts;
  50. } tests[] =
  51. {
  52. {SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf", 4000, nullptr, 0},
  53. {SUBSET_FONT_BASE_PATH "Amiri-Regular.ttf", 4000, nullptr, 0},
  54. {SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf", 1000, nullptr, 0},
  55. {SUBSET_FONT_BASE_PATH "NotoSansDevanagari-Regular.ttf", 1000, nullptr, 0},
  56. {SUBSET_FONT_BASE_PATH "Mplus1p-Regular.ttf", 10000, nullptr, 0},
  57. {SUBSET_FONT_BASE_PATH "SourceHanSans-Regular_subset.otf", 10000, nullptr, 0},
  58. {SUBSET_FONT_BASE_PATH "SourceSansPro-Regular.otf", 2000, nullptr, 0},
  59. {SUBSET_FONT_BASE_PATH "MPLUS1-Variable.ttf", 6000, _mplus_instance_opts, ARRAY_LEN (_mplus_instance_opts)},
  60. {SUBSET_FONT_BASE_PATH "RobotoFlex-Variable.ttf", 900, _roboto_flex_instance_opts, ARRAY_LEN (_roboto_flex_instance_opts)},
  61. #if 0
  62. {"perf/fonts/NotoSansCJKsc-VF.ttf", 100000},
  63. #endif
  64. };
  65. void AddCodepoints(const hb_set_t* codepoints_in_font,
  66. unsigned subset_size,
  67. hb_subset_input_t* input)
  68. {
  69. auto *unicodes = hb_subset_input_unicode_set (input);
  70. hb_codepoint_t cp = HB_SET_VALUE_INVALID;
  71. for (unsigned i = 0; i < subset_size; i++) {
  72. // TODO(garretrieger): pick randomly.
  73. if (!hb_set_next (codepoints_in_font, &cp)) return;
  74. hb_set_add (unicodes, cp);
  75. }
  76. }
  77. void AddGlyphs(unsigned num_glyphs_in_font,
  78. unsigned subset_size,
  79. hb_subset_input_t* input)
  80. {
  81. auto *glyphs = hb_subset_input_glyph_set (input);
  82. for (unsigned i = 0; i < subset_size && i < num_glyphs_in_font; i++) {
  83. // TODO(garretrieger): pick randomly.
  84. hb_set_add (glyphs, i);
  85. }
  86. }
  87. // Preprocess face and populate the subset accelerator on it to speed up
  88. // the subsetting operations.
  89. static hb_face_t* preprocess_face(hb_face_t* face)
  90. {
  91. #ifdef HB_EXPERIMENTAL_API
  92. hb_face_t* new_face = hb_subset_preprocess(face);
  93. hb_face_destroy(face);
  94. return new_face;
  95. #else
  96. return face;
  97. #endif
  98. }
  99. /* benchmark for subsetting a font */
  100. static void BM_subset (benchmark::State &state,
  101. operation_t operation,
  102. const test_input_t &test_input)
  103. {
  104. unsigned subset_size = state.range(0);
  105. hb_face_t *face;
  106. {
  107. hb_blob_t *blob = hb_blob_create_from_file_or_fail (test_input.font_path);
  108. assert (blob);
  109. face = hb_face_create (blob, 0);
  110. hb_blob_destroy (blob);
  111. face = preprocess_face (face);
  112. }
  113. hb_subset_input_t* input = hb_subset_input_create_or_fail ();
  114. assert (input);
  115. switch (operation)
  116. {
  117. case subset_codepoints:
  118. {
  119. hb_set_t* all_codepoints = hb_set_create ();
  120. hb_face_collect_unicodes (face, all_codepoints);
  121. AddCodepoints(all_codepoints, subset_size, input);
  122. hb_set_destroy (all_codepoints);
  123. }
  124. break;
  125. case subset_glyphs:
  126. {
  127. unsigned num_glyphs = hb_face_get_glyph_count (face);
  128. AddGlyphs(num_glyphs, subset_size, input);
  129. }
  130. break;
  131. case instance:
  132. #ifdef HB_EXPERIMENTAL_API
  133. {
  134. hb_set_t* all_codepoints = hb_set_create ();
  135. hb_face_collect_unicodes (face, all_codepoints);
  136. AddCodepoints(all_codepoints, subset_size, input);
  137. hb_set_destroy (all_codepoints);
  138. for (unsigned i = 0; i < test_input.num_instance_opts; i++)
  139. hb_subset_input_pin_axis_location (input, face,
  140. test_input.instance_opts[i].axis_tag,
  141. test_input.instance_opts[i].axis_value);
  142. }
  143. #endif
  144. break;
  145. }
  146. for (auto _ : state)
  147. {
  148. hb_face_t* subset = hb_subset_or_fail (face, input);
  149. assert (subset);
  150. hb_face_destroy (subset);
  151. }
  152. hb_subset_input_destroy (input);
  153. hb_face_destroy (face);
  154. }
  155. static void test_subset (operation_t op,
  156. const char *op_name,
  157. benchmark::TimeUnit time_unit,
  158. const test_input_t &test_input)
  159. {
  160. if (op == instance && test_input.instance_opts == nullptr)
  161. return;
  162. char name[1024] = "BM_subset/";
  163. strcat (name, op_name);
  164. strcat (name, strrchr (test_input.font_path, '/'));
  165. benchmark::RegisterBenchmark (name, BM_subset, op, test_input)
  166. ->Range(10, test_input.max_subset_size)
  167. ->Unit(time_unit);
  168. }
  169. static void test_operation (operation_t op,
  170. const char *op_name,
  171. benchmark::TimeUnit time_unit)
  172. {
  173. for (auto& test_input : tests)
  174. {
  175. test_subset (op, op_name, time_unit, test_input);
  176. }
  177. }
  178. int main(int argc, char** argv)
  179. {
  180. #define TEST_OPERATION(op, time_unit) test_operation (op, #op, time_unit)
  181. TEST_OPERATION (subset_glyphs, benchmark::kMillisecond);
  182. TEST_OPERATION (subset_codepoints, benchmark::kMillisecond);
  183. #ifdef HB_EXPERIMENTAL_API
  184. TEST_OPERATION (instance, benchmark::kMillisecond);
  185. #endif
  186. #undef TEST_OPERATION
  187. benchmark::Initialize(&argc, argv);
  188. benchmark::RunSpecifiedBenchmarks();
  189. benchmark::Shutdown();
  190. }