From b93b6b1560a08e5e707caf09d59ddd38e37a8593 Mon Sep 17 00:00:00 2001 From: agessaman Date: Tue, 4 Aug 2026 09:47:06 -0700 Subject: [PATCH] perf(mqtt): keep slot JWT tokens in PSRAM where available ensureSlotAuthToken()/releaseSlotAuthToken() now use the bridge's existing psram_malloc/psram_free rather than malloc/free, so a JWT slot's 768-byte token comes out of PSRAM instead of internal DRAM. On PSRAM boards this is the larger half of the auth_token work: the previous commit reclaims 768 B per slot that never creates a token, while this reclaims 768 B per slot that does -- up to 3840 B of internal DRAM on a fully configured five-slot board, which is where the mbedTLS working set is competing for space. No effect on non-PSRAM boards: psram_malloc falls back to internal DRAM, so the buffer stays exactly where the previous commit left it. Same for a BOARD_HAS_PSRAM board whose PSRAM fails to initialise. Safe to move because every access is a CPU copy on the bridge task, never DMA, an ISR, or a cache-disabled window: JWTHelper memcpy's the token into this buffer, and esp-mqtt copies it out of _mqtt_cfg into its own internal-DRAM storage when connect() applies the config. This is unlike the PSRAM-backed MQTT task stack that was tried and reverted for resetting Heltec V4 boards, where the fault was PSRAM execution context rather than a plain buffer read. Split from the previous commit so it can be reverted alone if hardware soak shows any PSRAM-related instability on the JWT path. flash non-PSRAM 1592801 -> 1592865 B (+64) PSRAM 1555213 -> 1555237 B (+24) 267/267 native tests pass; both observer envs and an nRF52 repeater build clean. --- src/helpers/bridges/MQTTBridge.cpp | 12 +++++++++--- src/helpers/bridges/MQTTBridge.h | 3 ++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/helpers/bridges/MQTTBridge.cpp b/src/helpers/bridges/MQTTBridge.cpp index 5d42f00f7..ebb63887a 100644 --- a/src/helpers/bridges/MQTTBridge.cpp +++ b/src/helpers/bridges/MQTTBridge.cpp @@ -1672,12 +1672,18 @@ bool MQTTBridge::ensureSlotClient(int index) { // Allocate this slot's JWT token buffer. Called only from createSlotAuthToken(), the // sole writer, so a slot on a non-JWT preset (or no preset at all) never allocates. -// Internal DRAM, matching where the buffer lived when it was inline in MQTTSlot. +// +// PSRAM where the board has it (psram_malloc falls back to internal DRAM otherwise), +// which is what moves the token off internal heap for slots that DO use JWT. Safe +// because the only readers are CPU copies on the bridge task: JWTHelper memcpy's the +// token in here, and esp-mqtt copies it out of _mqtt_cfg into its own internal-DRAM +// storage when connect() applies the config. No DMA, no ISR, and no cache-disabled +// window -- unlike the PSRAM task stack that reset Heltec V4 boards. bool MQTTBridge::ensureSlotAuthToken(int index) { if (index < 0 || index >= RUNTIME_MQTT_SLOTS) return false; MQTTSlot& slot = _slots[index]; slot.auth_token = static_cast(MQTTRuntimeBufferLifecycle::allocateIfMissing( - slot.auth_token, AUTH_TOKEN_SIZE, malloc)); + slot.auth_token, AUTH_TOKEN_SIZE, psram_malloc)); if (slot.auth_token == nullptr) { MQTT_DEBUG_PRINTLN("MQTT%d: out of memory allocating auth token", index + 1); return false; @@ -1693,7 +1699,7 @@ void MQTTBridge::releaseSlotAuthToken(int index) { if (index < 0 || index >= RUNTIME_MQTT_SLOTS) return; MQTTSlot& slot = _slots[index]; slot.auth_token = static_cast( - MQTTRuntimeBufferLifecycle::release(slot.auth_token, free)); + MQTTRuntimeBufferLifecycle::release(slot.auth_token, psram_free)); slot.token_expires_at = 0; slot.last_token_renewal = 0; } diff --git a/src/helpers/bridges/MQTTBridge.h b/src/helpers/bridges/MQTTBridge.h index d1c72e3f6..4c7436c2f 100644 --- a/src/helpers/bridges/MQTTBridge.h +++ b/src/helpers/bridges/MQTTBridge.h @@ -88,7 +88,8 @@ private: // JWT auth state (used by preset JWT slots and custom slots with audience set). // nullptr until this slot first creates a token, so a slot that is unconfigured, - // capped off, or on a non-JWT preset never pays for AUTH_TOKEN_SIZE. Allocated by + // capped off, or on a non-JWT preset never pays for AUTH_TOKEN_SIZE; slots that do + // use JWT keep their buffer in PSRAM where the board has it. Allocated by // ensureSlotAuthToken() and then held for the client's lifetime -- never freed per // reconnect (alloc/free churn is a fragmentation source) and never freed on // teardown, because setCredentials() hands this exact pointer to the client and