foreseen

A text-based gemini browser with prefetching.
git clone git://git.laack.co/foreseen.git
Log | Files | Refs | README | LICENSE

commit c6c05661575b8e31c4b9caca8fc51767967008ec
parent 4412852aab0b3d0fc56548b6774b803c9b6adfe1
Author: Andrew Laack <andrew@laack.co>
Date:   Wed, 23 Sep 2026 13:48:54 -0500

Some fixes and improvements to prefetching

Diffstat:
MMakefile | 2+-
Minclude/browser.hpp | 4+++-
Minclude/cache.hpp | 9++++-----
Minclude/line.hpp | 1+
Minclude/site.hpp | 1-
Msrc/browser.cpp | 67++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Msrc/cache.cpp | 20++++++++++++++++++--
Msrc/main.cpp | 13+++++++++----
8 files changed, 86 insertions(+), 31 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,5 +1,5 @@ build: - g++ src/main.cpp src/cache.cpp src/link.cpp src/plaintext.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp src/browser.cpp -lssl -lcrypto -lncursesw -o browser.out + g++ -g src/main.cpp src/cache.cpp src/link.cpp src/plaintext.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp src/browser.cpp -lssl -lcrypto -lncursesw -o browser.out install: echo "Not implemented" diff --git a/include/browser.hpp b/include/browser.hpp @@ -13,11 +13,13 @@ class Browser { std::unordered_map<std::string, int> previousStatusCodes; std::vector<Link*> siteHistory; int previousIdx = -1; - Cache* cache; + Cache* visitedCache; + Cache* preFetchCache; Site* currentSite; std::vector<Line*> lines; std::vector<std::size_t> links; // these point to line indices void setLinksOfCurrentLines(); + Site* findInCacheAndPromoteIfRelevant(std::string& urlString); public: Browser(); ~Browser(); diff --git a/include/cache.hpp b/include/cache.hpp @@ -5,16 +5,15 @@ #include <optional> #include <unordered_map> -enum Importance { - IMPORTANT, - NOT_IMPORTANT -}; +const int CACHE_SIZE = 500; class Cache { private: + void evict(); std::unordered_map<std::string,Site> cache; std::mutex mutex; + std::deque<std::string> evictionQueue; public: std::optional<Site> getSite(const std::string& site); - void addSite(std::string address, Site site, Importance importance = IMPORTANT); + void addSite(std::string address, Site site); }; diff --git a/include/line.hpp b/include/line.hpp @@ -13,4 +13,5 @@ class Line { virtual std::string textToDraw() = 0; virtual int getColor() = 0; virtual LineType type() = 0; + virtual ~Line() = default; }; diff --git a/include/site.hpp b/include/site.hpp @@ -8,7 +8,6 @@ class Site{ private: std::string body; - std::vector<Line*> lines; std::string header; bool unreachable = false; public: diff --git a/src/browser.cpp b/src/browser.cpp @@ -1,6 +1,4 @@ #include "../include/browser.hpp" -#include <iostream> -#include <stdexcept> #include <string> #include <unistd.h> #include "../include/site.hpp" @@ -12,6 +10,24 @@ #include <utility> #include <vector> + +Site* Browser::findInCacheAndPromoteIfRelevant(std::string& urlString) { + Site* site = nullptr; + std::optional<Site> cachedSite = visitedCache->getSite(urlString); + if(cachedSite != std::nullopt) { + site = new Site(*cachedSite); + } + if(site == nullptr) { + std::optional<Site> cached = preFetchCache->getSite(urlString); + if(cached != std::nullopt) { + site = new Site(*cached); + visitedCache->addSite(urlString, *cached); + } + } + + return site; +} + void Browser::refresh() { goToSite(getPriorUri().value().to_string(), false, true); } @@ -46,10 +62,7 @@ void Browser::goToSite(std::string url, bool addToHistory, bool refresh) { Site* site = nullptr; if(urlString.find("gemini://") != -1 && !refresh) { - std::optional<Site> cachedSite = cache->getSite(urlString); - if(cachedSite != std::nullopt) { - site = new Site(*cachedSite); - } + site = findInCacheAndPromoteIfRelevant(urlString); } if(site == nullptr) { @@ -61,6 +74,7 @@ void Browser::goToSite(std::string url, bool addToHistory, bool refresh) { delete site; } previousIdx += 1; + delete destination; goBack(); return; } @@ -80,33 +94,42 @@ void Browser::goToSite(std::string url, bool addToHistory, bool refresh) { currentSite = site; if(urlString.find("gemini://") != -1) { - cache->addSite(urlString, *site); + visitedCache->addSite(urlString, *site); + } + + for(auto* line: lines) { + delete line; } lines = toLines(site); + setLinksOfCurrentLines(); previousStatusCodes[destination->getLinkDestination().to_string()] = site->getStatusCode(); + if (!addToHistory) { + delete destination; + } } void Browser::justCacheSite(Link link) { auto client = GeminiClient{}; - std::string urlString = link.getLinkDestination().to_string(); - Site* site = nullptr; + if (visitedCache->getSite(urlString) || preFetchCache->getSite(urlString)) { + return; + } + Site* site = nullptr; if(urlString.find("gemini://") != -1) { - std::optional<Site> cachedSite = cache->getSite(urlString); - if(cachedSite == std::nullopt) { - site = client.fetchSite(link); - } + site = client.fetchSite(link); } if(site != nullptr) { - cache->addSite(urlString, *site, NOT_IMPORTANT); + preFetchCache->addSite(urlString, *site); + delete site; } + return; } @@ -142,13 +165,23 @@ std::vector<Line*> Browser::toLines(Site* site) { Browser::Browser() { currentSite = nullptr; - cache = new Cache{}; + visitedCache = new Cache{}; + preFetchCache = new Cache{}; + } // TODO: SHould add more stuff here too, like the links stuff. Browser::~Browser() { - if (cache != nullptr) { - delete cache; + delete visitedCache; + delete preFetchCache; + delete currentSite; + + for (auto* line : lines) { + delete line; + } + + for (auto* link : siteHistory) { + delete link; } } diff --git a/src/cache.cpp b/src/cache.cpp @@ -1,21 +1,37 @@ #include "../include/cache.hpp" #include "../include/site.hpp" #include <cassert> +#include <iostream> #include <mutex> #include <optional> std::optional<Site> Cache::getSite(const std::string& site) { + std::lock_guard<std::mutex> lock(mutex); auto it = cache.find(site); + if (it != cache.end()) { return it->second; } + return std::nullopt; } -void Cache::addSite(std::string address, Site site, Importance importance) { // TODO: Use importance +void Cache::addSite(std::string address, Site site) { std::lock_guard<std::mutex> lock(mutex); assert(address.find("gemini://") != -1); - cache.insert_or_assign(address,site); + + auto it = cache.find(address); + if (it != cache.end()) { + it->second = std::move(site); + return; + } + cache.emplace(address, std::move(site)); + + evictionQueue.push_front(address); + if (evictionQueue.size() > CACHE_SIZE) { + cache.erase(evictionQueue.back()); + evictionQueue.pop_back(); + } } diff --git a/src/main.cpp b/src/main.cpp @@ -1,4 +1,5 @@ #include "../include/browser.hpp" +#include <malloc.h> #include "../include/gemini-client.hpp" #include "../include/utils.hpp" #include <algorithm> @@ -13,6 +14,7 @@ #include <utility> #include <vector> +const int SITE_CACHE_LIMIT = 10; const int THREAD_NUM = 4; std::vector<std::thread> threads(THREAD_NUM); std::vector<std::atomic<bool>> done(THREAD_NUM); @@ -21,7 +23,8 @@ std::vector<std::atomic<bool>> done(THREAD_NUM); // bc ppl use lots of emojis on gemini sites. void aggressiveCaching(Browser* bPtr, std::vector<Link> targets, int threadIdx) { - for(auto& target : targets) { + for(int i = 0 ; i < targets.size() && i < SITE_CACHE_LIMIT; ++i) { + auto& target = targets[i]; bPtr->justCacheSite(target); } done[threadIdx] = true; @@ -284,9 +287,6 @@ int main(int argc, char** argv) { int input = 0; int y = 0; - int x = 0; - - std::thread cacheThread; // this is the main loop. @@ -360,6 +360,9 @@ int main(int argc, char** argv) { } else { b.goToSite(b.getCurrentSite()->getMeta(),true); } + current = b.renderSite(); + removeNonAscii(current); + current = breakLines(current,COLS); } else { current = b.renderSite(); @@ -398,4 +401,6 @@ int main(int argc, char** argv) { t.join(); } } + + delete bPtr; }