From dc101cf1135a62309d4a818b7f2fbe2a9f64bf11 Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 7 Nov 2026 19:34:43 +0000 Subject: [PATCH 1/2] Use the NVIDIA EGL driver on Linux, and handle only _blank links The web process loaded Mesa's EGL next to NVIDIA's, which pulled Mesa's gallium and LLVM in, about 50 MB of resident memory. configureGraphics() points EGL at the NVIDIA vendor when its driver loads, and leaves the setting alone when the user has set one. configureMemoryPressure() sets WebKit's memory-pressure thresholds when DW_MEMORY_LIMIT_MB is set. It is off by default and was only used for measurement: it did not change the footprint at startup. The page script now intercepts only links with target="_blank". Every other link is left to the page, so external links that are not new windows navigate this window. --- src/Host.linux.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++ src/Host.windows.cpp | 4 +++ src/PageScript.h | 10 +++--- src/main.cpp | 6 ++++ 4 files changed, 94 insertions(+), 4 deletions(-) diff --git a/src/Host.linux.cpp b/src/Host.linux.cpp index deece0d..708ac45 100644 --- a/src/Host.linux.cpp +++ b/src/Host.linux.cpp @@ -1,12 +1,14 @@ // The desktop functions the host defines for the library on Linux. The pointer and the monitors' work areas come from the library // itself, which already loads GTK. +#include #include #include #include #include #include +#include #include #include #include @@ -86,3 +88,79 @@ std::optional httpGet(const std::string& url) { } return body; } + +// The graphics setup before the window opens. With the NVIDIA driver installed, EGL uses only that driver: loading Mesa's EGL as +// well pulls Mesa's gallium and LLVM into the web process, about 50 MB of resident memory with no benefit on this machine. +void configureGraphics() { + if (std::getenv("__EGL_VENDOR_LIBRARY_FILENAMES")) { + return; + } + const char* nvidiaVendor = "/usr/share/glvnd/egl_vendor.d/10_nvidia.json"; + if (!std::filesystem::exists(nvidiaVendor)) { + return; + } + // Only when the driver's library loads: otherwise EGL would have no driver at all. + if (void* driver = dlopen("libEGL_nvidia.so.0", RTLD_LAZY)) { + dlclose(driver); + setenv("__EGL_VENDOR_LIBRARY_FILENAMES", nvidiaVendor, 1); + } +} + +namespace { + +// The WebKit functions the memory-pressure settings use. The library is already loaded by the time they're called. +struct MemoryPressureApi { + void* (*web_context_get_default)() = nullptr; + void* (*web_context_get_website_data_manager)(void*) = nullptr; + void* (*settings_new)() = nullptr; + void (*settings_set_memory_limit)(void*, unsigned int) = nullptr; + void (*settings_set_conservative_threshold)(void*, double) = nullptr; + void (*settings_set_strict_threshold)(void*, double) = nullptr; + void (*settings_set_kill_threshold)(void*, double) = nullptr; + void (*settings_set_poll_interval)(void*, double) = nullptr; + void (*data_manager_set_settings)(void*, void*) = nullptr; + void (*settings_free)(void*) = nullptr; +}; + +template +bool bindSymbol(void* library, Fn& target, const char* name) { + target = reinterpret_cast(dlsym(library, name)); + return target != nullptr; +} + +} // namespace + +// Sets WebKit's memory-pressure thresholds for the web process, when DW_MEMORY_LIMIT_MB is set. Off by default; set for the test. +void configureMemoryPressure() { + const char* limitText = std::getenv("DW_MEMORY_LIMIT_MB"); + if (!limitText || !*limitText) { + return; + } + void* library = dlopen("libwebkit2gtk-4.1.so.0", RTLD_LAZY | RTLD_NOLOAD); + if (!library) { + return; + } + MemoryPressureApi api; + const bool bound = bindSymbol(library, api.web_context_get_default, "webkit_web_context_get_default") && + bindSymbol(library, api.web_context_get_website_data_manager, "webkit_web_context_get_website_data_manager") && + bindSymbol(library, api.settings_new, "webkit_memory_pressure_settings_new") && + bindSymbol(library, api.settings_set_memory_limit, "webkit_memory_pressure_settings_set_memory_limit") && + bindSymbol(library, api.settings_set_conservative_threshold, "webkit_memory_pressure_settings_set_conservative_threshold") && + bindSymbol(library, api.settings_set_strict_threshold, "webkit_memory_pressure_settings_set_strict_threshold") && + bindSymbol(library, api.settings_set_kill_threshold, "webkit_memory_pressure_settings_set_kill_threshold") && + bindSymbol(library, api.settings_set_poll_interval, "webkit_memory_pressure_settings_set_poll_interval") && + bindSymbol(library, api.data_manager_set_settings, "webkit_website_data_manager_set_memory_pressure_settings") && + bindSymbol(library, api.settings_free, "webkit_memory_pressure_settings_free"); + if (bound) { + void* manager = api.web_context_get_website_data_manager(api.web_context_get_default()); + void* settings = api.settings_new(); + api.settings_set_memory_limit(settings, static_cast(std::strtoul(limitText, nullptr, 10))); + api.settings_set_conservative_threshold(settings, 0.3); + api.settings_set_strict_threshold(settings, 0.5); + api.settings_set_kill_threshold(settings, 0.95); + api.settings_set_poll_interval(settings, 2.0); + api.data_manager_set_settings(manager, settings); + api.settings_free(settings); + } + dlclose(library); +} diff --git a/src/Host.windows.cpp b/src/Host.windows.cpp index b9f6683..ea8aad6 100644 --- a/src/Host.windows.cpp +++ b/src/Host.windows.cpp @@ -86,3 +86,7 @@ std::optional httpGet(const std::string& url) { } return body; } + +// Windows has no EGL driver choice to make, and WebKit's memory-pressure thresholds are only tuned on Linux for now. +void configureGraphics() {} +void configureMemoryPressure() {} diff --git a/src/PageScript.h b/src/PageScript.h index 0941ba6..06695e7 100644 --- a/src/PageScript.h +++ b/src/PageScript.h @@ -97,8 +97,9 @@ inline constexpr const char* kPageScript = R"JS( new MutationObserver(() => { title.textContent = document.title || 'Discord'; }) .observe(document.head || document.documentElement, {subtree: true, childList: true, characterData: true}); - // Web addresses on other sites open in the default browser. Addresses on Discord's own site load in this window, even when the - // page asks for a new one: this window is the app, so a new window would be a second copy of it. + // Only links that ask for a new window (target="_blank") are handled here. Web addresses on other sites open in the default browser. + // Addresses on Discord's own site load in this window, even when the page asks for a new one: this window is the app, so a new + // window would be a second copy of it. Every other link is left to the app. const webUrl = href => { try { const url = new URL(href, location.href); @@ -109,13 +110,14 @@ inline constexpr const char* kPageScript = R"JS( document.addEventListener('click', e => { if (e.button !== 0) return; const anchor = e.target && e.target.closest ? e.target.closest('a[href]') : null; - const url = anchor && webUrl(anchor.href); + if (!anchor || anchor.target !== '_blank') return; + const url = webUrl(anchor.href); if (!url) return; if (url.hostname !== location.hostname) { e.preventDefault(); e.stopPropagation(); post('openExternal', [url.href]); - } else if (anchor.target === '_blank') { + } else { // Same-site links stay in this window. The click isn't stopped, so Discord's own router still handles it and changes the // channel without a page load. anchor.removeAttribute('target'); diff --git a/src/main.cpp b/src/main.cpp index f1c404b..6ab6071 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,6 +21,10 @@ void launchUrl(const std::string& url); // The folder the program keeps its settings in. Each platform's host file defines it. std::filesystem::path settingsFolder(); +// Graphics setup before the window opens, and the web process's memory-pressure thresholds once it has. Each host file defines them. +void configureGraphics(); +void configureMemoryPressure(); + namespace { constexpr const char* kDiscordUrl = "https://discord.com/app"; @@ -187,9 +191,11 @@ class DiscordWindow : public WebViewWindow { int main() { try { setApplicationName("Discord"); + configureGraphics(); initializeWindowing(); DiscordWindow window; window.open(); + configureMemoryPressure(); window.restoreFrame(); // Checks for a newer release in the background. The result comes back on the window's thread, and only while the window is // still running: the flag is cleared before the window goes. From 4aa18c326b1c6102b4fe13fc41be80d742976e2d Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 7 Nov 2026 19:35:53 +0000 Subject: [PATCH 2/2] Remove the graphics and memory-pressure changes Keeps the link handling from the previous commit. The NVIDIA EGL selection and the DW_MEMORY_LIMIT_MB hook are taken out: the hook did not change the startup footprint, and the EGL change is not in this branch. --- src/Host.linux.cpp | 78 -------------------------------------------- src/Host.windows.cpp | 4 --- src/main.cpp | 6 ---- 3 files changed, 88 deletions(-) diff --git a/src/Host.linux.cpp b/src/Host.linux.cpp index 708ac45..deece0d 100644 --- a/src/Host.linux.cpp +++ b/src/Host.linux.cpp @@ -1,14 +1,12 @@ // The desktop functions the host defines for the library on Linux. The pointer and the monitors' work areas come from the library // itself, which already loads GTK. -#include #include #include #include #include #include -#include #include #include #include @@ -88,79 +86,3 @@ std::optional httpGet(const std::string& url) { } return body; } - -// The graphics setup before the window opens. With the NVIDIA driver installed, EGL uses only that driver: loading Mesa's EGL as -// well pulls Mesa's gallium and LLVM into the web process, about 50 MB of resident memory with no benefit on this machine. -void configureGraphics() { - if (std::getenv("__EGL_VENDOR_LIBRARY_FILENAMES")) { - return; - } - const char* nvidiaVendor = "/usr/share/glvnd/egl_vendor.d/10_nvidia.json"; - if (!std::filesystem::exists(nvidiaVendor)) { - return; - } - // Only when the driver's library loads: otherwise EGL would have no driver at all. - if (void* driver = dlopen("libEGL_nvidia.so.0", RTLD_LAZY)) { - dlclose(driver); - setenv("__EGL_VENDOR_LIBRARY_FILENAMES", nvidiaVendor, 1); - } -} - -namespace { - -// The WebKit functions the memory-pressure settings use. The library is already loaded by the time they're called. -struct MemoryPressureApi { - void* (*web_context_get_default)() = nullptr; - void* (*web_context_get_website_data_manager)(void*) = nullptr; - void* (*settings_new)() = nullptr; - void (*settings_set_memory_limit)(void*, unsigned int) = nullptr; - void (*settings_set_conservative_threshold)(void*, double) = nullptr; - void (*settings_set_strict_threshold)(void*, double) = nullptr; - void (*settings_set_kill_threshold)(void*, double) = nullptr; - void (*settings_set_poll_interval)(void*, double) = nullptr; - void (*data_manager_set_settings)(void*, void*) = nullptr; - void (*settings_free)(void*) = nullptr; -}; - -template -bool bindSymbol(void* library, Fn& target, const char* name) { - target = reinterpret_cast(dlsym(library, name)); - return target != nullptr; -} - -} // namespace - -// Sets WebKit's memory-pressure thresholds for the web process, when DW_MEMORY_LIMIT_MB is set. Off by default; set for the test. -void configureMemoryPressure() { - const char* limitText = std::getenv("DW_MEMORY_LIMIT_MB"); - if (!limitText || !*limitText) { - return; - } - void* library = dlopen("libwebkit2gtk-4.1.so.0", RTLD_LAZY | RTLD_NOLOAD); - if (!library) { - return; - } - MemoryPressureApi api; - const bool bound = bindSymbol(library, api.web_context_get_default, "webkit_web_context_get_default") && - bindSymbol(library, api.web_context_get_website_data_manager, "webkit_web_context_get_website_data_manager") && - bindSymbol(library, api.settings_new, "webkit_memory_pressure_settings_new") && - bindSymbol(library, api.settings_set_memory_limit, "webkit_memory_pressure_settings_set_memory_limit") && - bindSymbol(library, api.settings_set_conservative_threshold, "webkit_memory_pressure_settings_set_conservative_threshold") && - bindSymbol(library, api.settings_set_strict_threshold, "webkit_memory_pressure_settings_set_strict_threshold") && - bindSymbol(library, api.settings_set_kill_threshold, "webkit_memory_pressure_settings_set_kill_threshold") && - bindSymbol(library, api.settings_set_poll_interval, "webkit_memory_pressure_settings_set_poll_interval") && - bindSymbol(library, api.data_manager_set_settings, "webkit_website_data_manager_set_memory_pressure_settings") && - bindSymbol(library, api.settings_free, "webkit_memory_pressure_settings_free"); - if (bound) { - void* manager = api.web_context_get_website_data_manager(api.web_context_get_default()); - void* settings = api.settings_new(); - api.settings_set_memory_limit(settings, static_cast(std::strtoul(limitText, nullptr, 10))); - api.settings_set_conservative_threshold(settings, 0.3); - api.settings_set_strict_threshold(settings, 0.5); - api.settings_set_kill_threshold(settings, 0.95); - api.settings_set_poll_interval(settings, 2.0); - api.data_manager_set_settings(manager, settings); - api.settings_free(settings); - } - dlclose(library); -} diff --git a/src/Host.windows.cpp b/src/Host.windows.cpp index ea8aad6..b9f6683 100644 --- a/src/Host.windows.cpp +++ b/src/Host.windows.cpp @@ -86,7 +86,3 @@ std::optional httpGet(const std::string& url) { } return body; } - -// Windows has no EGL driver choice to make, and WebKit's memory-pressure thresholds are only tuned on Linux for now. -void configureGraphics() {} -void configureMemoryPressure() {} diff --git a/src/main.cpp b/src/main.cpp index 6ab6071..f1c404b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,10 +21,6 @@ void launchUrl(const std::string& url); // The folder the program keeps its settings in. Each platform's host file defines it. std::filesystem::path settingsFolder(); -// Graphics setup before the window opens, and the web process's memory-pressure thresholds once it has. Each host file defines them. -void configureGraphics(); -void configureMemoryPressure(); - namespace { constexpr const char* kDiscordUrl = "https://discord.com/app"; @@ -191,11 +187,9 @@ class DiscordWindow : public WebViewWindow { int main() { try { setApplicationName("Discord"); - configureGraphics(); initializeWindowing(); DiscordWindow window; window.open(); - configureMemoryPressure(); window.restoreFrame(); // Checks for a newer release in the background. The result comes back on the window's thread, and only while the window is // still running: the flag is cleared before the window goes.