benchmark-subset.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "hb-benchmark.hh"
  2. enum operation_t
  3. {
  4. subset_glyphs,
  5. subset_unicodes,
  6. instance,
  7. };
  8. struct axis_location_t
  9. {
  10. hb_tag_t axis_tag;
  11. float axis_value;
  12. };
  13. static const axis_location_t
  14. _roboto_flex_instance_opts[] =
  15. {
  16. {HB_TAG ('w', 'g', 'h', 't'), 600.f},
  17. {HB_TAG ('w', 'd', 't', 'h'), 75.f},
  18. {HB_TAG ('o', 'p', 's', 'z'), 90.f},
  19. {HB_TAG ('G', 'R', 'A', 'D'), -100.f},
  20. {HB_TAG ('s', 'l', 'n', 't'), -3.f},
  21. {HB_TAG ('X', 'T', 'R', 'A'), 500.f},
  22. {HB_TAG ('X', 'O', 'P', 'Q'), 150.f},
  23. {HB_TAG ('Y', 'O', 'P', 'Q'), 100.f},
  24. {HB_TAG ('Y', 'T', 'L', 'C'), 480.f},
  25. {HB_TAG ('Y', 'T', 'U', 'C'), 600.f},
  26. {HB_TAG ('Y', 'T', 'A', 'S'), 800.f},
  27. {HB_TAG ('Y', 'T', 'D', 'E'), -50.f},
  28. {HB_TAG ('Y', 'T', 'F', 'I'), 600.f},
  29. };
  30. static const axis_location_t
  31. _mplus_instance_opts[] =
  32. {
  33. {HB_TAG ('w', 'g', 'h', 't'), 800.f},
  34. };
  35. static const axis_location_t
  36. _fraunces_partial_instance_opts[] =
  37. {
  38. {HB_TAG ('S', 'O', 'F', 'T'), 75.0f},
  39. {HB_TAG ('W', 'O', 'N', 'K'), 0.75f},
  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. {SUBSET_FONT_BASE_PATH "Fraunces.ttf", 900, _fraunces_partial_instance_opts, ARRAY_LEN (_fraunces_partial_instance_opts)},
  63. #if 0
  64. {"perf/fonts/NotoSansCJKsc-VF.ttf", 100000},
  65. #endif
  66. };
  67. static test_input_t *tests = default_tests;
  68. static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
  69. void AddCodepoints(const hb_set_t* codepoints_in_font,
  70. unsigned subset_size,
  71. hb_subset_input_t* input)
  72. {
  73. auto *unicodes = hb_subset_input_unicode_set (input);
  74. hb_codepoint_t cp = HB_SET_VALUE_INVALID;
  75. for (unsigned i = 0; i < subset_size; i++) {
  76. // TODO(garretrieger): pick randomly.
  77. if (!hb_set_next (codepoints_in_font, &cp)) return;
  78. hb_set_add (unicodes, cp);
  79. }
  80. }
  81. void AddGlyphs(unsigned num_glyphs_in_font,
  82. unsigned subset_size,
  83. hb_subset_input_t* input)
  84. {
  85. auto *glyphs = hb_subset_input_glyph_set (input);
  86. for (unsigned i = 0; i < subset_size && i < num_glyphs_in_font; i++) {
  87. if (i + 1 == subset_size &&
  88. hb_subset_input_get_flags (input) & HB_SUBSET_FLAGS_RETAIN_GIDS)
  89. {
  90. hb_set_add (glyphs, num_glyphs_in_font - 1);
  91. continue;
  92. }
  93. hb_set_add (glyphs, i);
  94. }
  95. }
  96. // Preprocess face and populate the subset accelerator on it to speed up
  97. // the subsetting operations.
  98. static hb_face_t* preprocess_face(hb_face_t* face)
  99. {
  100. hb_face_t* new_face = hb_subset_preprocess(face);
  101. hb_face_destroy(face);
  102. return new_face;
  103. }
  104. static hb_face_t *cached_face;
  105. static void
  106. free_cached_face (void)
  107. {
  108. hb_face_destroy (cached_face);
  109. cached_face = nullptr;
  110. }
  111. /* benchmark for subsetting a font */
  112. static void BM_subset (benchmark::State &state,
  113. operation_t operation,
  114. const test_input_t &test_input,
  115. bool retain_gids)
  116. {
  117. unsigned subset_size = state.range(0);
  118. hb_face_t *face = nullptr;
  119. static const char *cached_font_path;
  120. if (!cached_font_path || strcmp (cached_font_path, test_input.font_path))
  121. {
  122. face = hb_benchmark_face_create_from_file_or_fail (test_input.font_path, 0);
  123. assert (face);
  124. face = preprocess_face (face);
  125. if (cached_face)
  126. hb_face_destroy (cached_face);
  127. cached_face = hb_face_reference (face);
  128. cached_font_path = test_input.font_path;
  129. }
  130. else
  131. face = hb_face_reference (cached_face);
  132. hb_subset_input_t* input = hb_subset_input_create_or_fail ();
  133. assert (input);
  134. if (retain_gids)
  135. hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_RETAIN_GIDS);
  136. switch (operation)
  137. {
  138. case subset_unicodes:
  139. {
  140. hb_set_t* all_codepoints = hb_set_create ();
  141. hb_face_collect_unicodes (face, all_codepoints);
  142. AddCodepoints(all_codepoints, subset_size, input);
  143. hb_set_destroy (all_codepoints);
  144. }
  145. break;
  146. case subset_glyphs:
  147. {
  148. unsigned num_glyphs = hb_face_get_glyph_count (face);
  149. AddGlyphs(num_glyphs, subset_size, input);
  150. }
  151. break;
  152. case instance:
  153. {
  154. hb_set_t* all_codepoints = hb_set_create ();
  155. hb_face_collect_unicodes (face, all_codepoints);
  156. AddCodepoints(all_codepoints, subset_size, input);
  157. hb_set_destroy (all_codepoints);
  158. hb_subset_input_set_flags(input, hb_subset_input_get_flags(input) | HB_SUBSET_FLAGS_OPTIMIZE_IUP_DELTAS);
  159. for (unsigned i = 0; i < test_input.num_instance_opts; i++)
  160. hb_subset_input_pin_axis_location (input, face,
  161. test_input.instance_opts[i].axis_tag,
  162. test_input.instance_opts[i].axis_value);
  163. }
  164. break;
  165. }
  166. for (auto _ : state)
  167. {
  168. hb_face_t* subset = hb_subset_or_fail (face, input);
  169. assert (subset);
  170. hb_face_destroy (subset);
  171. }
  172. hb_subset_input_destroy (input);
  173. hb_face_destroy (face);
  174. }
  175. static void test_subset (operation_t op,
  176. const char *op_name,
  177. bool retain_gids,
  178. benchmark::TimeUnit time_unit,
  179. const test_input_t &test_input)
  180. {
  181. if (op == instance && test_input.instance_opts == nullptr)
  182. return;
  183. char name[1024] = "BM_subset/";
  184. strcat (name, op_name);
  185. strcat (name, "/");
  186. const char *p = strrchr (test_input.font_path, '/');
  187. strcat (name, p ? p + 1 : test_input.font_path);
  188. if (retain_gids)
  189. strcat (name, "/retaingids");
  190. benchmark::RegisterBenchmark (name, BM_subset, op, test_input, retain_gids)
  191. ->Range(10, test_input.max_subset_size)
  192. ->Unit(time_unit);
  193. }
  194. static void test_operation (operation_t op,
  195. const char *op_name,
  196. const test_input_t *tests,
  197. unsigned num_tests,
  198. benchmark::TimeUnit time_unit)
  199. {
  200. for (unsigned i = 0; i < num_tests; i++)
  201. {
  202. auto& test_input = tests[i];
  203. test_subset (op, op_name, true, time_unit, test_input);
  204. test_subset (op, op_name, false, time_unit, test_input);
  205. }
  206. }
  207. int main(int argc, char** argv)
  208. {
  209. benchmark::Initialize(&argc, argv);
  210. #ifndef HB_NO_ATEXIT
  211. atexit (free_cached_face);
  212. #endif
  213. if (argc > 1)
  214. {
  215. num_tests = (argc - 1) / 2;
  216. tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
  217. for (unsigned i = 0; i < num_tests; i++)
  218. {
  219. tests[i].font_path = argv[1 + i * 2];
  220. tests[i].max_subset_size = atoi (argv[2 + i * 2]);
  221. }
  222. }
  223. #define TEST_OPERATION(op, time_unit) test_operation (op, #op, tests, num_tests, time_unit)
  224. TEST_OPERATION (subset_glyphs, benchmark::kMicrosecond);
  225. TEST_OPERATION (subset_unicodes, benchmark::kMicrosecond);
  226. TEST_OPERATION (instance, benchmark::kMicrosecond);
  227. #undef TEST_OPERATION
  228. benchmark::RunSpecifiedBenchmarks();
  229. benchmark::Shutdown();
  230. if (tests != default_tests)
  231. free (tests);
  232. }