diff --git a/src/render.cpp b/src/render.cpp index 8905739..075ef3b 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -178,11 +178,37 @@ Page_Info_Node *viewer_jump_to_page(App_State &app, u64 page_idx) return nullptr; page_idx = (page_idx + app.rndata.n_pages) % app.rndata.n_pages; - // @Speed - Page_Info_Node *page = app.rndata.pages; - for (u64 i = 0; i < page_idx; ++i) { - page = page->next; - assert(page); + // // @Speed + // Page_Info_Node *page = app.rndata.pages; + // for (u64 i = 0; i < page_idx; ++i) { + // page = page->next; + // assert(page); + // } + + // Find the page_idx-th page. We could just iterate n times over the pages list, + // but skimming through the clusters first should be faster in most cases. + Page_Info_Node *page = nullptr; + for (u64 i = 0; i < app.rndata.n_clusters - 1; ++i) { + const Cluster_Info &cluster = app.rndata.clusters[i]; + const Cluster_Info &next_cluster = app.rndata.clusters[i]; + if (cluster.first_page_idx <= page_idx && page_idx < next_cluster.first_page_idx) { + u64 idx_in_cluster = page_idx - cluster.first_page_idx; + page = cluster.first_page; + for (u64 j = 0; j < idx_in_cluster; ++j) { + page = page->next; + } + break; + } + } + if (!page) { + // page was not in the first N - 1 clusters, so it must be in the last one. + const Cluster_Info &cluster = app.rndata.clusters[app.rndata.n_clusters - 1]; + assert(page_idx >= cluster.first_page_idx); + u64 idx_in_cluster = page_idx - cluster.first_page_idx; + page = cluster.first_page; + for (u64 j = 0; j < idx_in_cluster; ++j) { + page = page->next; + } } app.viewer.latest_page_gone_to = page_idx;