From 60d641c51caa5a53b185d40b6f6825417670887e Mon Sep 17 00:00:00 2001 From: Joe Neeman Date: Fri, 20 Aug 2010 12:43:37 -0700 Subject: [PATCH] Cache page-height during page breaking. --- lily/include/page-breaking.hh | 3 +++ lily/page-breaking.cc | 32 ++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lily/include/page-breaking.hh b/lily/include/page-breaking.hh index 58c8de0951..d34534c1ca 100644 --- a/lily/include/page-breaking.hh +++ b/lily/include/page-breaking.hh @@ -196,6 +196,9 @@ private: vector cached_line_details_; vector uncompressed_line_details_; + mutable vector page_height_cache_; + mutable vector last_page_height_cache_; + vector chunk_list (vsize start, vsize end); Line_division system_count_bounds (vector const &chunks, bool min); void line_breaker_args (vsize i, diff --git a/lily/page-breaking.cc b/lily/page-breaking.cc index d22e999a7f..4ac005461c 100644 --- a/lily/page-breaking.cc +++ b/lily/page-breaking.cc @@ -368,13 +368,33 @@ Page_breaking::make_page (int page_num, bool last) const Real Page_breaking::page_height (int page_num, bool last) const { - 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& 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); - SCM height = scm_apply_1 (calc_height, page, SCM_EOL); - return scm_to_double (height); + SCM height_scm = scm_apply_1 (calc_height, page, SCM_EOL); + Real height = scm_to_double (height_scm); + + if (page_num >= 0) + { + if ((int) cache.size () <= page_num) + cache.resize (page_num + 1, -1); + cache[page_num] = height; + } + return height; + } } SCM -- 2.39.2