From 79519985659e34bd2d67365609a43bb90b28c262 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 20 Aug 2026 03:55:41 -0600 Subject: [PATCH] [set] Compute iterator length lazily Set iteration does not always need the remaining item count. Defer the population scan until len() is first requested, using unsigned accounting to track advances without adding a branch to the iterator hot path. This preserves fast length for iterator consumers that use it while avoiding unnecessary population scans for the others. Noto Nastaliq dependency construction is instruction-neutral because its existing set iterators already encounter cached populations. Tested with: meson test -C build Assisted-by: OpenAI Codex --- src/hb-bit-set-invertible.hh | 71 +++++++++++++++++++++--------------- src/test-set.cc | 7 ++++ 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/hb-bit-set-invertible.hh b/src/hb-bit-set-invertible.hh index 8263cf2e1..bb71105b0 100644 --- a/src/hb-bit-set-invertible.hh +++ b/src/hb-bit-set-invertible.hh @@ -430,20 +430,56 @@ struct hb_bit_set_invertible_t static constexpr bool is_sorted_iterator = true; static constexpr bool has_fast_len = true; iter_t (const hb_bit_set_invertible_t &s_ = Null (hb_bit_set_invertible_t), - bool init = true) : s (&s_), v (INVALID), l (0), - base (INVALID), bits (0) + bool init = true) : s (&s_), v (INVALID), remaining (0), + have_len (!init), base (INVALID), bits (0) { if (init) - { - l = s->get_population () + 1; - __next__ (); - } + advance (); } typedef hb_codepoint_t __item_t__; hb_codepoint_t __item__ () const { return v; } bool __more__ () const { return v != INVALID; } void __next__ () + { + remaining--; + advance (); + } + void __prev__ () + { + if (s->previous (&v)) + { + sync (); + remaining++; + } + else + { + base = INVALID; + bits = 0; + } + } + unsigned __len__ () const + { + if (unlikely (!have_len)) + { + remaining += s->get_population (); + have_len = true; + } + return remaining; + } + iter_t end () const { return iter_t (*s, false); } + bool operator != (const iter_t& o) const + { return v != o.v; } + + protected: + const hb_bit_set_invertible_t *s; + hb_codepoint_t v; + mutable unsigned remaining; + mutable bool have_len; + hb_codepoint_t base; + uint64_t bits; + + void advance () { if (likely (bits)) { @@ -457,30 +493,7 @@ struct hb_bit_set_invertible_t } else v = INVALID; - if (likely (l)) l--; } - void __prev__ () - { - if (s->previous (&v)) - sync (); - else - { - base = INVALID; - bits = 0; - } - l++; - } - unsigned __len__ () const { return l; } - iter_t end () const { return iter_t (*s, false); } - bool operator != (const iter_t& o) const - { return v != o.v; } - - protected: - const hb_bit_set_invertible_t *s; - hb_codepoint_t v; - unsigned l; - hb_codepoint_t base; - uint64_t bits; void sync () { diff --git a/src/test-set.cc b/src/test-set.cc index bd835f867..f47fbaaa5 100644 --- a/src/test-set.cc +++ b/src/test-set.cc @@ -218,8 +218,10 @@ main (int argc, char **argv) auto it = s.iter (); hb_always_assert (*it == 0); + hb_always_assert (it.len () == ARRAY_LENGTH (expected)); ++it; hb_always_assert (*it == 1); + hb_always_assert (it.len () == ARRAY_LENGTH (expected) - 1); --it; hb_always_assert (*it == 0); ++it; @@ -240,6 +242,10 @@ main (int argc, char **argv) hb_always_assert (*it == 1024); ++it; hb_always_assert (*it == HB_SET_VALUE_INVALID - 1); + + it = s.iter (); + it += 2; + hb_always_assert (it.len () == ARRAY_LENGTH (expected) - 2); } /* Test inverted-set iteration. */ @@ -248,6 +254,7 @@ main (int argc, char **argv) s.invert (); auto it = s.iter (); hb_always_assert (*it == 2); + hb_always_assert (it.len () == HB_SET_VALUE_INVALID - 4); ++it; hb_always_assert (*it == 3); --it;