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.
This commit is contained in:
agessaman committed 2026-08-04 09:47:06 -07:00
1 parent 9141ad854f
commit b93b6b1560
2 files changed
+11 -4

No files matched your search

+9 -3
View File
@@ -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<char*>(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<char*>(
MQTTRuntimeBufferLifecycle::release(slot.auth_token, free));
MQTTRuntimeBufferLifecycle::release(slot.auth_token, psram_free));
slot.token_expires_at = 0;
slot.last_token_renewal = 0;
}
+2 -1
View File
@@ -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