[erlang-questions] maps iterator

Björn-Egil Dahlberg wallentin.dahlberg@REDACTED
Wed Sep 30 02:05:11 CEST 2015


2015-09-30 1:27 GMT+02:00 zxq9 <zxq9@REDACTED>:

> To iterate through the map you would have to loop over it. How does the
> map key generator track its place in the key list without maintaining a
> list, being like ETS, or being in a separate process? Assuming that what
> you mean isn't already maps:map/2 or maybe a maps:foreach/2?
>

Easy, you keep track of it using an iterator context .. similar to a binary
match context. Don't worry about it.

Keep in mind that all of those iterations in maps:map/2, maps:fold/3 and
maps:foreach/2 are implemented using maps:to_list/1. This works but it's
wasteful. Instead we reimplement those using an iterator. Less waste. 5
words less waste per pair if we implement it as an instruction otherwise 2
words. I would prefer the instruction approach. The compiler can always
reassemble the return value to an 2-tuple if desired.

The iterator in gb_trees is not really fast either. It's faster to iterate
over gb_trees:to_list/1 in most cases but it's there and it's useful if you
want to save memory.

I'm trying to imagine what the calling code would look like where it isn't
> like maps:map/2, maps:fold/2 or a hypothetical maps:foreach/2, and maps
> don't become ETS... and drawing a blank.
>

I think most cases can be handled by those functions. That's why they are
there.

// Björn-Egil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150930/fabdaa26/attachment.htm>


More information about the erlang-questions mailing list