benchmark-subset.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. unsigned max_subset_size;
  48. const axis_location_t *instance_opts;
  49. unsigned num_instance_opts;
  50. } default_tests[] =
  51. {
  52. {SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf", 1000, nullptr, 0},
  53. {SUBSET_FONT_BASE_PATH "Amiri-Regular.ttf", 4096, nullptr, 0},
  54. {SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf", 1400, 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 "AdobeVFPrototype.otf", 300, nullptr, 0},
  60. {SUBSET_FONT_BASE_PATH "MPLUS1-Variable.ttf", 6000, _mplus_instance_opts, ARRAY_LEN (_mplus_instance_opts)},
  61. {SUBSET_FONT_BASE_PATH "RobotoFlex-Variable.ttf", 900, _roboto_flex_instance_opts, ARRAY_LEN (_roboto_flex_instance_opts)},
  62. #if 0
  63. {"perf/fonts/NotoSansCJKsc-VF.ttf", 100000},
  64. #endif
  65. };
  66. static test_input_t *tests = default_tests;
  67. static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
  68. void AddCodepoints(const hb_set_t* codepoints_in_font,
  69. unsigned subset_size,
  70. hb_subset_input_t* input)
  71. {
  72. auto *unicodes = hb_subset_input_unicode_set (input);
  73. hb_codepoint_t cp = HB_SET_VALUE_INVALID;
  74. for (unsigned i = 0; i < subset_size; i++) {
  75. // TODO(garretrieger): pick randomly.
  76. if (!hb_set_next (codepoints_in_font, &cp)) return;
  77. hb_set_add (unicodes, cp);
  78. }
  79. }
  80. void AddGlyphs(unsigned num_glyphs_in_font,
  81. unsigned subset_size,
  82. hb_subset_input_t* input)
  83. {
  84. auto *glyphs = hb_subset_input_glyph_set (input);
  85. for (unsigned i = 0; i < subset_size && i < num_glyphs_in_font; i++) {
  86. // TODO(garretrieger): pick randomly.
  87. hb_set_add (glyphs, i);
  88. }
  89. }
  90. // Preprocess face and populate the subset accelerator on it to speed up
  91. // the subsetting operations.
  92. static hb_face_t* preprocess_face(hb_face_t* face)
  93. {
  94. hb_face_t* new_face = hb_subset_preprocess(face);
  95. hb_face_destroy(face);
  96. return new_face;
  97. }
  98. /* benchmark for subsetting a font */
  99. static void BM_subset (benchmark::State &state,
  100. operation_t operation,
  101. const test_input_t &test_input,
  102. bool hinting)
  103. {
  104. unsigned subset_size = state.range(0);
  105. hb_face_t *face = nullptr;
  106. static hb_face_t *cached_face;
  107. static const char *cached_font_path;
  108. if (!cached_font_path || strcmp (cached_font_path, test_input.font_path))
  109. {
  110. hb_blob_t *blob = hb_blob_create_from_file_or_fail (test_input.font_path);
  111. assert (blob);
  112. face = hb_face_create (blob, 0);
  113. hb_blob_destroy (blob);
  114. face = preprocess_face (face);
  115. if (cached_face)
  116. hb_face_destroy (cached_face);
  117. cached_face = hb_face_reference (face);
  118. cached_font_path = test_input.font_path;
  119. }
  120. else
  121. face = hb_face_reference (cached_face);
  122. hb_subset_input_t* input = hb_subset_input_create_or_fail ();
  123. assert (input);
  124. if (!hinting)
  125. hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_NO_HINTING);
  126. switch (operation)
  127. {
  128. case subset_codepoints:
  129. {
  130. hb_set_t* all_codepoints = hb_set_create ();
  131. hb_face_collect_unicodes (face, all_codepoints);
  132. AddCodepoints(all_codepoints, subset_size, input);
  133. hb_set_destroy (all_codepoints);
  134. }
  135. break;
  136. case subset_glyphs:
  137. {
  138. unsigned num_glyphs = hb_face_get_glyph_count (face);
  139. AddGlyphs(num_glyphs, subset_size, input);
  140. }
  141. break;
  142. case instance:
  143. {
  144. hb_set_t* all_codepoints = hb_set_create ();
  145. hb_face_collect_unicodes (face, all_codepoints);
  146. AddCodepoints(all_codepoints, subset_size, input);
  147. hb_set_destroy (all_codepoints);
  148. for (unsigned i = 0; i < test_input.num_instance_opts; i++)
  149. hb_subset_input_pin_axis_location (input, face,
  150. test_input.instance_opts[i].axis_tag,
  151. test_input.instance_opts[i].axis_value);
  152. }
  153. break;
  154. }
  155. for (auto _ : state)
  156. {
  157. hb_face_t* subset = hb_subset_or_fail (face, input);
  158. assert (subset);
  159. hb_face_destroy (subset);
  160. }
  161. hb_subset_input_destroy (input);
  162. hb_face_destroy (face);
  163. }
  164. static void test_subset (operation_t op,
  165. const char *op_name,
  166. bool hinting,
  167. benchmark::TimeUnit time_unit,
  168. const test_input_t &test_input)
  169. {
  170. if (op == instance && test_input.instance_opts == nullptr)
  171. return;
  172. char name[1024] = "BM_subset/";
  173. strcat (name, op_name);
  174. strcat (name, "/");
  175. const char *p = strrchr (test_input.font_path, '/');
  176. strcat (name, p ? p + 1 : test_input.font_path);
  177. if (!hinting)
  178. strcat (name, "/nohinting");
  179. benchmark::RegisterBenchmark (name, BM_subset, op, test_input, hinting)
  180. ->Range(10, test_input.max_subset_size)
  181. ->Unit(time_unit);
  182. }
  183. static void test_operation (operation_t op,
  184. const char *op_name,
  185. const test_input_t *tests,
  186. unsigned num_tests,
  187. benchmark::TimeUnit time_unit)
  188. {
  189. for (unsigned i = 0; i < num_tests; i++)
  190. {
  191. auto& test_input = tests[i];
  192. test_subset (op, op_name, true, time_unit, test_input);
  193. test_subset (op, op_name, false, time_unit, test_input);
  194. }
  195. }
  196. int main(int argc, char** argv)
  197. {
  198. benchmark::Initialize(&argc, argv);
  199. if (argc > 1)
  200. {
  201. num_tests = (argc - 1) / 2;
  202. tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
  203. for (unsigned i = 0; i < num_tests; i++)
  204. {
  205. tests[i].font_path = argv[1 + i * 2];
  206. tests[i].max_subset_size = atoi (argv[2 + i * 2]);
  207. }
  208. }
  209. #define TEST_OPERATION(op, time_unit) test_operation (op, #op, tests, num_tests, time_unit)
  210. TEST_OPERATION (subset_glyphs, benchmark::kMillisecond);
  211. TEST_OPERATION (subset_codepoints, benchmark::kMillisecond);
  212. TEST_OPERATION (instance, benchmark::kMillisecond);
  213. #undef TEST_OPERATION
  214. benchmark::RunSpecifiedBenchmarks();
  215. benchmark::Shutdown();
  216. if (tests != default_tests)
  217. free (tests);
  218. }