Skip to content

feat(Util): get or save :persistent_term - #3414

Merged
thecristen merged 3 commits into
mainfrom
cbj/persistent-term
Aug 17, 2026
Merged

feat(Util): get or save :persistent_term#3414
thecristen merged 3 commits into
mainfrom
cbj/persistent-term

Conversation

@thecristen

Copy link
Copy Markdown
Collaborator

Scope

Slack thread

From a linked page from the linked thread:

Erlang's persistent_term is a great option if you want to keep a large state in memory and you modify it rarely or ideally, never. It solves a problem that you'll run into if you use :ets or a GenServer to store a large state in memory: whenever a process fetches your large state, the BEAM will copy the entire state and return it to the calling process. Additionally, the state is copied again and again whenever the process uses it. If your state is large, this will seriously impact your processing speed and memory usage.

That's where persistent_term comes in. Instead of returning a copy of your state, it returns a reference to it. That way, only one version of the state stays in memory, but processes can still look up data and pass it around without problems.

I think we have an opportunity to use this tool, so long as we're careful to avoid updates or deletes to the things we store in :persistent_term.

Everything gets reset on redeploy, so we don't have to worry about unexpected stale states.

Implementation

  • Added a function which either retrieves an item from :persistent_term, or computes and returns the result (while storing it for future use).
  • Uses it in two places which I noticed while profiling the homepage:
    • Generating our Content Security Policy, which is identical for every response
    • Formatting module names for use in cache keys, which will be consistent for each module name

Screenshots

No change!

How to test

Not much to test here.

@thecristen
thecristen requested a review from a team as a code owner August 12, 2026 17:41
@thecristen
thecristen requested a review from joshlarson August 12, 2026 17:41

@lvachon1 lvachon1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very cool feature, I look forward to seeing how we can apply it further.

@thecristen
thecristen enabled auto-merge (squash) August 17, 2026 17:51
@thecristen
thecristen merged commit d8eaf34 into main Aug 17, 2026
44 of 45 checks passed
@thecristen
thecristen deleted the cbj/persistent-term branch August 17, 2026 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants