- SCM mod = scm_c_resolve_module ("scm page");
- SCM page = make_page (page_num, last);
- SCM calc_height = scm_c_module_lookup (mod, "calc-printable-height");
- calc_height = scm_variable_ref (calc_height);
+ // The caches allow us to store the page heights for any
+ // non-negative page numbers. We use a negative value in the
+ // cache to signal that that position has not yet been initialized.
+ // This means that we won't cache properly if page_num is negative or
+ // if calc_height returns a negative number. But that's likely to
+ // be rare, so it shouldn't affect performance.
+ vector<Real>& cache = last ? last_page_height_cache_ : page_height_cache_;
+ if (page_num >= 0 && (int) cache.size () > page_num && cache[page_num] >= 0)
+ return cache[page_num];
+ else
+ {
+ SCM mod = scm_c_resolve_module ("scm page");
+ SCM page = make_page (page_num, last);
+ SCM calc_height = scm_c_module_lookup (mod, "calc-printable-height");
+ calc_height = scm_variable_ref (calc_height);