Skip to content

Add persistent_term:put_new #9695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Benjamin-Philip
Copy link
Contributor

This PR adds the persistent_term:put_new to conditionally add a key-value only if the key is new.

Closes #9681.

/cc @jhogberg.

Copy link
Contributor

github-actions bot commented Apr 7, 2025

CT Test Results

    3 files    141 suites   50m 9s ⏱️
1 615 tests 1 560 ✅ 54 💤 1 ❌
2 335 runs  2 259 ✅ 74 💤 2 ❌

For more details on these failures, see this check.

Results for commit 5ec1438.

♻️ This comment has been updated with latest results.

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

@rickard-green rickard-green added the team:VM Assigned to OTP team VM label Apr 14, 2025
@jhogberg jhogberg self-assigned this Apr 14, 2025
@josevalim
Copy link
Contributor

Instead of adding put_new, would it be more useful to add a has_key function that could be used in more cases? I recognize put_new has the benefit of being atomic but I assume that the value you need to store is likely expensive to compute anyway, so you want to check if the key exists before you do any work.

@Benjamin-Philip
Copy link
Contributor Author

Benjamin-Philip commented Apr 20, 2025

I think a has_key would be a good addition, but not as a replacement to put_new. Like you said put_new has the benefit of being atomic which enables one to "correctly" (in a formal sense) update parallely out of the box. See the linked issue for my use case.

You can define a rudimentary has_key already by setting Default in get/2 to undefined or some other more specific atom. Nevertheless, has_key would be faster and less error prone.

TLDR; I think we should add both functions.

@Benjamin-Philip Benjamin-Philip marked this pull request as ready for review April 20, 2025 18:57
@Benjamin-Philip
Copy link
Contributor Author

@jhogberg, I think this PR is review ready now. Sorry about the delay - I was busy the past 2 weeks...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team:VM Assigned to OTP team VM
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add persistent_term:put_new/2
4 participants