Skip to content
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

Add matchspec BIFs maps_put and maps_remove #7326

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sverker
Copy link
Contributor

@sverker sverker commented May 30, 2023

corresponding to maps:put/3 and maps:remove/2.

This is a quick attempt to fix #7309.

The map syntax to update keys cannot simply be used as Map#{key => value} would be translated by fun2ms to something like '$1'#{key => value} which is not valid Erlang syntax. Instead use the match spec syntax to call BIFs.

1> T = ets:new(x,[]).

2> ets:insert(T, {key, #{a=>1, b=>2}}).

3> MS = ets:fun2ms(fun({Key, Map}) when is_map(Map) -> {Key, maps_put(c, 3, Map)} end).
[{{'$1','$2'},
  [{is_map,'$2'}],
  [{{'$1',{maps_put,c,3,'$2'}}}]}]

4> ets:select_replace(T, MS).
1
5> ets:tab2list(T).
[{key,#{c => 3,a => 1,b => 2}}]

ToDo: Test and documentation.

corresponding to maps:put/3 and maps:remove/2.
@sverker sverker added team:VM Assigned to OTP team VM feature labels May 30, 2023
@sverker sverker self-assigned this May 30, 2023
@github-actions
Copy link
Contributor

github-actions bot commented May 30, 2023

CT Test Results

No tests were run for this PR. This is either because the build failed, or the PR is based on a branch without GH actions tests configured.

Results for commit e3cdf6c

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

@michalmuskala
Copy link
Contributor

Could it be that fun2ms translates the map update syntax into map_put calls?

@sverker
Copy link
Contributor Author

sverker commented May 31, 2023

Could it be that fun2ms translates the map update syntax into map_put calls?

It could I suppose. I'm not familiar with the fun2ms code though.
You would still need to call maps_remove directly.

@yushli
Copy link

yushli commented May 31, 2023

Vote for

fun2ms translates the map update syntax into map_put calls

@christhekeele
Copy link
Contributor

A couple of thoughts:

  • Wouldn't it be better to have this be map_put and map_remove?

    Today's map_get and map_size equivalents do not pluralize maps.

  • Should these be added as general auto-imported BIFs?

    Today's map_get and map_size are named the same as auto-imported BIFs map_get/2 and map_size/2. Do we want this to be internally consistent or just a secret capability of match specs? Or are there equivalent auto-imported BIFS in master and I'm just behind the times?

  • My own (extreme) ignorance showing here in how this works under the hood, but the match spec docs suggest that action functions are only allowed in dbg-dialect ms's. Would this implementation only allow them in dbg-dialects? Or would this be the first set of "action functions" allowed in the ets dialect?

@sverker
Copy link
Contributor Author

sverker commented Feb 13, 2024

  • Wouldn't it be better to have this be map_put and map_remove?

Yes maybe. I called them maps_put and maps_remove to refer to maps:put and maps:remove.

  • Should these be added as general auto-imported BIFs?
    Today's map_get and map_size are named the same as auto-imported BIFs [map_get/2]

Maybe. I did this as a quick proof of concept without too much thought of the naming.

  • My own (extreme) ignorance showing here in how this works under the hood, but the match spec docs suggest that action functions are only allowed in dbg-dialect ms's. Would this implementation only allow them in dbg-dialects? Or would this be the first set of "action functions" allowed in the ets dialect?

I don't remember why I added them to ms_transform:action_function/1. They are not action functions in the sense of side effects. maps_put and maps_remove are pure functions returning new maps and will fit in with the existing ets match spec paradigm.

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

Successfully merging this pull request may close these issues.

Add support to in place add/remove key/values of map in ets:select_replace with match spec
4 participants