From f005e660eecbf5ad42b082060566da8b2caab3a6 Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Sun, 29 Nov 2026 00:12:33 +0100 Subject: [PATCH 1/2] refactor: move the list layout model into the shared core --- cpp/adapter/ScrollTargets.h | 2 +- cpp/{adapter => core}/ListLayout.cpp | 2 +- cpp/{adapter => core}/ListLayout.h | 2 +- cpp/{adapter => core}/ListRows.cpp | 0 cpp/{adapter => core}/ListRows.h | 0 cpp/tests/list_layout_test.cpp | 2 +- cpp/tests/list_rows_test.cpp | 2 +- ios/FLMLayout.h | 2 +- ios/FLMView.mm | 2 +- 9 files changed, 7 insertions(+), 7 deletions(-) rename cpp/{adapter => core}/ListLayout.cpp (99%) rename cpp/{adapter => core}/ListLayout.h (98%) rename cpp/{adapter => core}/ListRows.cpp (100%) rename cpp/{adapter => core}/ListRows.h (100%) diff --git a/cpp/adapter/ScrollTargets.h b/cpp/adapter/ScrollTargets.h index 0628ef4..51562d5 100644 --- a/cpp/adapter/ScrollTargets.h +++ b/cpp/adapter/ScrollTargets.h @@ -4,7 +4,7 @@ #include #include -#include "adapter/ListLayout.h" +#include "core/ListLayout.h" namespace flume { diff --git a/cpp/adapter/ListLayout.cpp b/cpp/core/ListLayout.cpp similarity index 99% rename from cpp/adapter/ListLayout.cpp rename to cpp/core/ListLayout.cpp index dd753ee..e0ed73c 100644 --- a/cpp/adapter/ListLayout.cpp +++ b/cpp/core/ListLayout.cpp @@ -1,4 +1,4 @@ -#include "adapter/ListLayout.h" +#include "core/ListLayout.h" #include diff --git a/cpp/adapter/ListLayout.h b/cpp/core/ListLayout.h similarity index 98% rename from cpp/adapter/ListLayout.h rename to cpp/core/ListLayout.h index eaf6979..e90f7f1 100644 --- a/cpp/adapter/ListLayout.h +++ b/cpp/core/ListLayout.h @@ -62,7 +62,7 @@ struct LengthChange { float contentMoved; // how far the content length moved }; -// Item positions along the scroll axis from measured lengths and per-type estimates; units are points. +// Item positions along the scroll axis from measured lengths and per-type estimates, in the platform units: points on iOS, px on Android. class ListLayout { public: using TypeOf = std::function; diff --git a/cpp/adapter/ListRows.cpp b/cpp/core/ListRows.cpp similarity index 100% rename from cpp/adapter/ListRows.cpp rename to cpp/core/ListRows.cpp diff --git a/cpp/adapter/ListRows.h b/cpp/core/ListRows.h similarity index 100% rename from cpp/adapter/ListRows.h rename to cpp/core/ListRows.h diff --git a/cpp/tests/list_layout_test.cpp b/cpp/tests/list_layout_test.cpp index be3b62e..6faf705 100644 --- a/cpp/tests/list_layout_test.cpp +++ b/cpp/tests/list_layout_test.cpp @@ -1,6 +1,6 @@ #include "doctest.h" -#include "adapter/ListLayout.h" +#include "core/ListLayout.h" #include "core/KeyDiff.h" using namespace flume; diff --git a/cpp/tests/list_rows_test.cpp b/cpp/tests/list_rows_test.cpp index 0f83bd8..ee28575 100644 --- a/cpp/tests/list_rows_test.cpp +++ b/cpp/tests/list_rows_test.cpp @@ -1,6 +1,6 @@ #include "doctest.h" -#include "adapter/ListRows.h" +#include "core/ListRows.h" using namespace flume; diff --git a/ios/FLMLayout.h b/ios/FLMLayout.h index f8237a5..9324f11 100644 --- a/ios/FLMLayout.h +++ b/ios/FLMLayout.h @@ -3,7 +3,7 @@ #include #include "adapter/ContentAnchor.h" -#include "adapter/ListLayout.h" +#include "core/ListLayout.h" #include "adapter/ScrollTargets.h" #include "adapter/Viewability.h" diff --git a/ios/FLMView.mm b/ios/FLMView.mm index 1eda97c..ffb2180 100644 --- a/ios/FLMView.mm +++ b/ios/FLMView.mm @@ -30,7 +30,7 @@ #include "adapter/BatchUpdates.h" #include "adapter/EdgeTracker.h" #include "adapter/JumpGate.h" -#include "adapter/ListRows.h" +#include "core/ListRows.h" #include "adapter/ScrollCommands.h" #include "adapter/ScrollTargets.h" #include "adapter/Viewability.h" From 65f8f622fd9d66acd1e0ca16cf7dd49028290fd1 Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Sun, 29 Nov 2026 00:15:32 +0100 Subject: [PATCH 2/2] fix: keep list positions exact past 16 million Float prefix sums round past 2^24, so in a list of 200,000 rows of 101 the content length came out 33,900 short and row 166,113 started at 16,777,412 instead of 16,777,413. Positions along the scroll axis are now doubles, and item lengths stay floats. --- cpp/core/ListLayout.cpp | 66 +++++++++++++++++----------------- cpp/core/ListLayout.h | 40 ++++++++++----------- cpp/tests/list_layout_test.cpp | 11 ++++++ 3 files changed, 64 insertions(+), 53 deletions(-) diff --git a/cpp/core/ListLayout.cpp b/cpp/core/ListLayout.cpp index e0ed73c..e2145de 100644 --- a/cpp/core/ListLayout.cpp +++ b/cpp/core/ListLayout.cpp @@ -82,8 +82,8 @@ LengthChange ListLayout::setLength(int index, float length, int anchor, float vi // Justified short content moves with its slack, which the offset must not chase. Stacking from the end wins. const bool recenters = justified() && !config_.stackFromEnd; const auto edge = [&] { return anchorEdge(rowOf_[anchor], viewportLength) - (recenters ? slack(viewportLength) : 0); }; - const float before = hasAnchor ? edge() : 0; - const float contentBefore = contentLength(viewportLength); + const double before = hasAnchor ? edge() : 0; + const double contentBefore = contentLength(viewportLength); Item& item = items_[index]; Mean& mean = means_[item.type]; if (item.measured) { @@ -95,7 +95,7 @@ LengthChange ListLayout::setLength(int index, float length, int anchor, float vi item.length = length; item.measured = true; updateRow(rowOf_[index]); - const float moved = hasAnchor ? edge() - before : 0; + const double moved = hasAnchor ? edge() - before : 0; return LengthChange{moved, contentLength(viewportLength) - contentBefore}; } @@ -118,15 +118,15 @@ float ListLayout::estimate(int type) const { return std::max(config_.estimateFloor, static_cast(it->second.sum / it->second.count)); } -float ListLayout::contentLength(float viewportLength) const { - const float length = config_.paddingStart + rowStart(static_cast(rows_.size())) + config_.paddingEnd; - return bottomAnchored() || justified() ? std::max(viewportLength, length) : length; +double ListLayout::contentLength(float viewportLength) const { + const double length = config_.paddingStart + rowStart(static_cast(rows_.size())) + config_.paddingEnd; + return bottomAnchored() || justified() ? std::max(static_cast(viewportLength), length) : length; } -std::pair ListLayout::contentSpan(float viewportLength) const { - const float length = config_.paddingStart + rowStart(static_cast(rows_.size())) + config_.paddingEnd; +std::pair ListLayout::contentSpan(float viewportLength) const { + const double length = config_.paddingStart + rowStart(static_cast(rows_.size())) + config_.paddingEnd; if (!config_.inverted) return {slack(viewportLength), slack(viewportLength) + length}; - const float end = contentLength(viewportLength); + const double end = contentLength(viewportLength); return {end - length, end}; } @@ -145,7 +145,7 @@ float ListLayout::laneLength(float listCrossLength) const { ItemRect ListLayout::rectOf(int index, float listCrossLength, float viewportLength) const { if (index < 0 || index >= count()) return ItemRect{0, 0, 0, 0}; const int row = rowOf_[index]; - const float main = startOf(row, viewportLength); + const double main = startOf(row, viewportLength); const float cross = crossLength(index, listCrossLength); const float lane = static_cast(index - rows_[row].first); const float crossAt = @@ -155,17 +155,17 @@ ItemRect ListLayout::rectOf(int index, float listCrossLength, float viewportLeng return ItemRect{crossAt, main, cross, length}; } -int ListLayout::indexAt(float offset, float viewportLength) const { +int ListLayout::indexAt(double offset, float viewportLength) const { if (rows_.empty()) return -1; return rows_[rowAt(offset, viewportLength)].first; } // The first row that starts in the viewport, or the last that ends in it when bottom-anchored; a cut row grows unseen. -int ListLayout::anchorAt(float viewportStart, float viewportLength) const { +int ListLayout::anchorAt(double viewportStart, float viewportLength) const { if (rows_.empty()) return -1; const int last = static_cast(rows_.size()) - 1; if (bottomAnchored()) { - const float end = viewportStart + viewportLength; + const double end = viewportStart + viewportLength; const int row = rowAt(end - kEdgeSlop, viewportLength); if (anchorEdge(row, viewportLength) <= end + kEdgeSlop) return rows_[row].first; // The row above it: the next one inverted, the previous one upright. @@ -176,26 +176,26 @@ int ListLayout::anchorAt(float viewportStart, float viewportLength) const { return rows_[cut && row < last ? row + 1 : row].first; } -float ListLayout::anchorEdgeOf(int index, float viewportLength) const { +double ListLayout::anchorEdgeOf(int index, float viewportLength) const { if (index < 0 || index >= count()) return 0; return anchorEdge(rowOf_[index], viewportLength); } -std::vector ListLayout::itemsIn(float start, float end, float viewportLength) const { +std::vector ListLayout::itemsIn(double start, double end, float viewportLength) const { std::vector out; if (rows_.empty()) return out; const int a = rowAt(start, viewportLength); const int b = rowAt(end, viewportLength); for (int row = std::min(a, b); row <= std::max(a, b); row++) { // An inverted row's separator sits above it; either way a row whose separator shows is listed. - const float rowFrom = startOf(row, viewportLength) - (config_.inverted ? rowStride(row) - rows_[row].length : 0); + const double rowFrom = startOf(row, viewportLength) - (config_.inverted ? rowStride(row) - rows_[row].length : 0); if (rowFrom >= end || rowFrom + rowStride(row) <= start) continue; for (int i = 0; i < rows_[row].count; i++) out.push_back(rows_[row].first + i); } return out; } -std::optional ListLayout::pinned(const std::vector& sticky, float viewportStart, +std::optional ListLayout::pinned(const std::vector& sticky, double viewportStart, float viewportLength) const { const int top = indexAt(viewportStart, viewportLength); if (top < 0) return std::nullopt; @@ -210,7 +210,7 @@ std::optional ListLayout::pinned(const std::vector& sticky, float v if (s < 0 || s >= count()) continue; if (config_.inverted ? s < *index && (!pusher || s > *pusher) : s > *index && (!pusher || s < *pusher)) pusher = s; } - float start = viewportStart; + double start = viewportStart; if (pusher) { // The pinned row ends at the pusher with the line between them, which is its own upright and the pusher's inverted. const float line = lineAfter(rowOf_[config_.inverted ? *pusher : *index]); @@ -224,7 +224,7 @@ std::optional ListLayout::separatorRect(int index, float listCrossLeng const int row = rowOf_[index]; if (!gapAfter(row)) return std::nullopt; const ItemRect item = rectOf(index, listCrossLength, 0); - const float crossAt = config_.horizontal ? item.y : item.x; + const float crossAt = static_cast(config_.horizontal ? item.y : item.x); // Each cell draws its share of the padded width, so the row's line runs across the column gaps and lane insets. const float padded = std::max(0.f, listCrossLength - config_.paddingCrossStart - config_.paddingCrossEnd); const bool alone = fullSpan(index); @@ -239,17 +239,17 @@ std::optional ListLayout::separatorRect(int index, float listCrossLeng return ItemRect{from, main, to - from, config_.separator}; } -float ListLayout::emptyMinLength(float viewportLength) const { +double ListLayout::emptyMinLength(float viewportLength) const { return config_.emptyIndex >= 0 && config_.emptyIndex < count() ? growShare(viewportLength) : 0; } -float ListLayout::growMinLength(float viewportLength) const { +double ListLayout::growMinLength(float viewportLength) const { return growsFooter() ? growShare(viewportLength) : 0; } bool ListLayout::growsFooter() const { return config_.growFooter && config_.afterData > 0 && count() > 0; } -float ListLayout::growShare(float viewportLength) const { +double ListLayout::growShare(float viewportLength) const { int growers = 0; float grown = 0; for (int index : {config_.emptyIndex, growsFooter() ? count() - 1 : -1}) { @@ -258,8 +258,8 @@ float ListLayout::growShare(float viewportLength) const { grown += rows_[rowOf_[index]].length; } if (growers == 0) return 0; - const float used = config_.paddingStart + config_.paddingEnd + rowStart(static_cast(rows_.size())) - grown; - return std::max(0.f, viewportLength - used) / static_cast(growers); + const double used = config_.paddingStart + config_.paddingEnd + rowStart(static_cast(rows_.size())) - grown; + return std::max(0.0, viewportLength - used) / static_cast(growers); } bool ListLayout::fullSpan(int index) const { return std::binary_search(fullSpan_.begin(), fullSpan_.end(), index); } @@ -310,29 +310,29 @@ float ListLayout::rowStride(int row) const { return rows_[row].length + data + main; } -float ListLayout::rowStart(int row) const { +double ListLayout::rowStart(int row) const { for (; dirtyFrom_ < row; dirtyFrom_++) starts_[dirtyFrom_ + 1] = starts_[dirtyFrom_] + rowStride(dirtyFrom_); return starts_[row]; } -float ListLayout::slack(float viewportLength) const { +double ListLayout::slack(float viewportLength) const { if (!config_.stackFromEnd && !justified()) return 0; - const float length = config_.paddingStart + rowStart(static_cast(rows_.size())) + config_.paddingEnd; - const float free = std::max(0.f, viewportLength - length); + const double length = config_.paddingStart + rowStart(static_cast(rows_.size())) + config_.paddingEnd; + const double free = std::max(0.0, viewportLength - length); return config_.stackFromEnd || config_.justify == ListLayoutConfig::Justify::End ? free : free / 2; } -float ListLayout::startOf(int row, float viewportLength) const { +double ListLayout::startOf(int row, float viewportLength) const { if (!config_.inverted) return config_.paddingStart + slack(viewportLength) + rowStart(row); return contentLength(viewportLength) - config_.paddingEnd - rowStart(row) - rows_[row].length; } -float ListLayout::anchorEdge(int row, float viewportLength) const { - const float start = startOf(row, viewportLength); +double ListLayout::anchorEdge(int row, float viewportLength) const { + const double start = startOf(row, viewportLength); return bottomAnchored() ? start + rows_[row].length : start; } -int ListLayout::rowAt(float offset, float viewportLength) const { +int ListLayout::rowAt(double offset, float viewportLength) const { const int rows = static_cast(rows_.size()); rowStart(rows); if (!config_.inverted) { @@ -341,7 +341,7 @@ int ListLayout::rowAt(float offset, float viewportLength) const { return std::max(0, static_cast(it - starts_.begin()) - 1); } // Inverted rows run up from the bottom: the row whose end, measured from item 0's bottom, reaches past the offset. - const float fromBottom = contentLength(viewportLength) - config_.paddingEnd - offset; + const double fromBottom = contentLength(viewportLength) - config_.paddingEnd - offset; auto it = std::lower_bound(starts_.begin() + 1, starts_.begin() + rows + 1, fromBottom); return std::min(rows - 1, static_cast(it - (starts_.begin() + 1))); } diff --git a/cpp/core/ListLayout.h b/cpp/core/ListLayout.h index e90f7f1..561cdfc 100644 --- a/cpp/core/ListLayout.h +++ b/cpp/core/ListLayout.h @@ -42,13 +42,13 @@ inline float paddingOf(float points, float fraction, float listWidth) { } struct ItemRect { - float x, y, width, height; + double x, y, width, height; bool operator==(const ItemRect&) const = default; }; struct Pinned { int index; - float start; // along the scroll axis, in content coordinates + double start; // along the scroll axis, in content coordinates }; struct IndexRange { @@ -58,8 +58,8 @@ struct IndexRange { }; struct LengthChange { - float anchorMoved; // how far the anchor's edge moved along the scroll axis - float contentMoved; // how far the content length moved + double anchorMoved; // how far the anchor's edge moved along the scroll axis + double contentMoved; // how far the content length moved }; // Item positions along the scroll axis from measured lengths and per-type estimates, in the platform units: points on iOS, px on Android. @@ -83,21 +83,21 @@ class ListLayout { IndexRange itemsOfRows(int firstRow, int lastRow) const; bool measured(int index) const; float estimate(int type) const; - float contentLength(float viewportLength) const; + double contentLength(float viewportLength) const; // From the start of the padding to the end of it, at the bottom of a short list that sits there. - std::pair contentSpan(float viewportLength) const; + std::pair contentSpan(float viewportLength) const; float crossLength(int index, float listCrossLength) const; // the size a cell binds at across the scroll axis float laneLength(float listCrossLength) const; // one column of a grid, the whole width with one lane ItemRect rectOf(int index, float listCrossLength, float viewportLength) const; - int indexAt(float offset, float viewportLength) const; + int indexAt(double offset, float viewportLength) const; // The item whose edge stays put while rows measure, for setLength. - int anchorAt(float viewportStart, float viewportLength) const; + int anchorAt(double viewportStart, float viewportLength) const; // The edge that stays in place, its bottom when inverted or stacking from the end, in content coordinates. - float anchorEdgeOf(int index, float viewportLength) const; - std::vector itemsIn(float start, float end, float viewportLength) const; - float emptyMinLength(float viewportLength) const; // the viewport the padding and the other rows leave - float growMinLength(float viewportLength) const; // the same for a growing footer - std::optional pinned(const std::vector& sticky, float viewportStart, float viewportLength) const; + double anchorEdgeOf(int index, float viewportLength) const; + std::vector itemsIn(double start, double end, float viewportLength) const; + double emptyMinLength(float viewportLength) const; // the viewport the padding and the other rows leave + double growMinLength(float viewportLength) const; // the same for a growing footer + std::optional pinned(const std::vector& sticky, double viewportStart, float viewportLength) const; // Relative to the item's origin: the gap after its row in content order, none after the last row. std::optional separatorRect(int index, float listCrossLength) const; // Inverted or stacked from the end: the list keeps its end where it was. @@ -127,14 +127,14 @@ class ListLayout { bool gapAfter(int row) const; bool isData(int index) const; // not the header, the footer or the empty row float lineAfter(int row) const; // the separator after the row, 0 when it has none - float rowStart(int row) const; // from the start of item 0, inside the padding - float slack(float viewportLength) const; // the space short content leaves above its rows: all of it at the end, half centered + double rowStart(int row) const; // from the start of item 0, inside the padding + double slack(float viewportLength) const; // the space short content leaves above its rows: all of it at the end, half centered bool growsFooter() const; // What the other rows leave of the viewport, split evenly between the empty row and a growing footer, as two flex: 1 rows. - float growShare(float viewportLength) const; - float startOf(int row, float viewportLength) const; // in content coordinates - float anchorEdge(int row, float viewportLength) const; - int rowAt(float offset, float viewportLength) const; // offset in content coordinates + double growShare(float viewportLength) const; + double startOf(int row, float viewportLength) const; // in content coordinates + double anchorEdge(int row, float viewportLength) const; + int rowAt(double offset, float viewportLength) const; // offset in content coordinates ListLayoutConfig config_; std::vector fullSpan_; @@ -142,7 +142,7 @@ class ListLayout { std::unordered_map means_; std::vector rows_; std::vector rowOf_; - mutable std::vector starts_; // rows_.size() + 1 prefix sums, valid up to dirtyFrom_ + mutable std::vector starts_; // rows_.size() + 1 prefix sums, valid up to dirtyFrom_ mutable int dirtyFrom_ = 0; }; diff --git a/cpp/tests/list_layout_test.cpp b/cpp/tests/list_layout_test.cpp index 6faf705..accc200 100644 --- a/cpp/tests/list_layout_test.cpp +++ b/cpp/tests/list_layout_test.cpp @@ -699,3 +699,14 @@ TEST_CASE("stacked from the end, centered content keeps its anchor's bottom wher CHECK(layout.setLength(0, 200, 2, 600).anchorMoved == 0); CHECK(layout.rectOf(2, 400, 600).y == 500); } + +TEST_CASE("positions stay exact past 2^24") { + ListLayout layout; + ListLayoutConfig config; + config.estimatedItemLength = 101; + layout.configure(config, {}); + layout.reset(200000, [](int) { return 0; }); + CHECK(layout.rectOf(166113, 400, 800).y == 16777413.0); + CHECK(layout.contentLength(800) == 20200000.0); + CHECK(layout.indexAt(16777413.0 + 50, 800) == 166113); +}