Quantcast

[erlang-questions] Introspecting the atom table

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

[erlang-questions] Introspecting the atom table

Eric Merritt
Guys,

 Is there anyway to introspect the atom table? I would like to be able
to take a string and check if that string has been interned in the
atom table. Even better would be a bif that converts the string to an
atom if that atom already exists and doesn't otherwise. I suspect that
this type of functionality doesn't exist, but I thought I would ask
and make sure.

Thanks,
 Eric
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

Kostis Sagonas-2
Eric Merritt wrote:
> Guys,
>
>  Is there anyway to introspect the atom table? I would like to be able
> to take a string and check if that string has been interned in the
> atom table. Even better would be a bif that converts the string to an
> atom if that atom already exists and doesn't otherwise. I suspect that
> this type of functionality doesn't exist, but I thought I would ask
> and make sure.

Handle with care:


Eshell V5.5.2.1  (abort with ^G)
1> list_to_existing_atom("erlang").
erlang
2> list_to_existing_atom("gazonk").

=ERROR REPORT==== 20-Dec-2006::23:06:58 ===
Error in process <0.29.0> with exit value:
{badarg,[{erlang,list_to_existing_atom,["gazonk"]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}

** exited: {badarg,[{erlang,list_to_existing_atom,["gazonk"]},
                     {erl_eval,do_apply,5},
                     {shell,exprs,6},
                     {shell,eval_loop,3}]} **

Kostis
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

Michael McDaniel-2
On Wed, Dec 20, 2006 at 11:07:48PM +0100, Kostis Sagonas wrote:

> Eric Merritt wrote:
> > Guys,
> >
> >  Is there anyway to introspect the atom table? I would like to be able
> > to take a string and check if that string has been interned in the
> > atom table. Even better would be a bif that converts the string to an
> > atom if that atom already exists and doesn't otherwise. I suspect that
> > this type of functionality doesn't exist, but I thought I would ask
> > and make sure.
>
> Handle with care:
>
>
> Eshell V5.5.2.1  (abort with ^G)
> 1> list_to_existing_atom("erlang").
> erlang
> 2> list_to_existing_atom("gazonk").
>
> =ERROR REPORT==== 20-Dec-2006::23:06:58 ===
> Error in process <0.29.0> with exit value:
> {badarg,[{erlang,list_to_existing_atom,["gazonk"]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}
>
> ** exited: {badarg,[{erlang,list_to_existing_atom,["gazonk"]},
>                      {erl_eval,do_apply,5},
>                      {shell,exprs,6},
>                      {shell,eval_loop,3}]} **
>
> Kostis
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  and here is how I might use it where RegName could be a
  new server or an existing one already known.  RegName,
  for example, may be passed in to a fun which executes the
  following ...


  net_adm:ping(

    try list_to_existing_atom(RegName)
    catch Class:Error ->
        list_to_atom(RegName)
    end

  )


~M
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

Kostis Sagonas-2
Michael McDaniel wrote:

> On Wed, Dec 20, 2006 at 11:07:48PM +0100, Kostis Sagonas wrote:
>> Eric Merritt wrote:
>>> Guys,
>>>
>>>  Is there anyway to introspect the atom table? I would like to be able
>>> to take a string and check if that string has been interned in the
>>> atom table. Even better would be a bif that converts the string to an
>>> atom if that atom already exists and doesn't otherwise. I suspect that
>>> this type of functionality doesn't exist, but I thought I would ask
>>> and make sure.
>> Handle with care:
>>
>>
>> Eshell V5.5.2.1  (abort with ^G)
>> 1> list_to_existing_atom("erlang").
>> erlang
>> 2> list_to_existing_atom("gazonk").
>>
>> =ERROR REPORT==== 20-Dec-2006::23:06:58 ===
>> Error in process <0.29.0> with exit value:
>> {badarg,[{erlang,list_to_existing_atom,["gazonk"]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}
>>
>> ** exited: {badarg,[{erlang,list_to_existing_atom,["gazonk"]},
>>                      {erl_eval,do_apply,5},
>>                      {shell,exprs,6},
>>                      {shell,eval_loop,3}]} **
>>
>> Kostis
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
>   and here is how I might use it where RegName could be a
>   new server or an existing one already known.  RegName,
>   for example, may be passed in to a fun which executes the
>   following ...
>
>
>   net_adm:ping(
>
>     try list_to_existing_atom(RegName)
>     catch Class:Error ->
>         list_to_atom(RegName)
>     end
>
>   )

Admittedly I am very tired, but why would you write the code above
rather than simply write:

   net_adm:ping( list_to_atom(RegName) )

?

Kostis
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

Michael McDaniel-2
On Wed, Dec 20, 2006 at 11:33:58PM +0100, Kostis Sagonas wrote:

> Michael McDaniel wrote:
> > On Wed, Dec 20, 2006 at 11:07:48PM +0100, Kostis Sagonas wrote:
> >> Eric Merritt wrote:
> >>> Guys,
> >>>
> >>>  Is there anyway to introspect the atom table? I would like to be able
> >>> to take a string and check if that string has been interned in the
> >>> atom table. Even better would be a bif that converts the string to an
> >>> atom if that atom already exists and doesn't otherwise. I suspect that
> >>> this type of functionality doesn't exist, but I thought I would ask
> >>> and make sure.
> >> Handle with care:
> >>
> >>
> >> Eshell V5.5.2.1  (abort with ^G)
> >> 1> list_to_existing_atom("erlang").
> >> erlang
> >> 2> list_to_existing_atom("gazonk").
> >>
> >> =ERROR REPORT==== 20-Dec-2006::23:06:58 ===
> >> Error in process <0.29.0> with exit value:
> >> {badarg,[{erlang,list_to_existing_atom,["gazonk"]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}
> >>
> >> ** exited: {badarg,[{erlang,list_to_existing_atom,["gazonk"]},
> >>                      {erl_eval,do_apply,5},
> >>                      {shell,exprs,6},
> >>                      {shell,eval_loop,3}]} **
> >>
> >> Kostis
> > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> >
> >   and here is how I might use it where RegName could be a
> >   new server or an existing one already known.  RegName,
> >   for example, may be passed in to a fun which executes the
> >   following ...
> >
> >
> >   net_adm:ping(
> >
> >     try list_to_existing_atom(RegName)
> >     catch Class:Error ->
> >         list_to_atom(RegName)
> >     end
> >
> >   )
>
> Admittedly I am very tired, but why would you write the code above
> rather than simply write:
>
>    net_adm:ping( list_to_atom(RegName) )
>
> ?
>
> Kostis
______________________________________________________________________

 My understanding is that "an atom is forever".  In long running
 code I don't want to keep creating new atoms if I already have
 one.  The above I use where I have sporadic communication between
 remote servers that could come online or offline.

 This was one example where I might not want to keep creating new
 atoms so use the try/catch idiom.

~M
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

Joel Reymont
In reply to this post by Eric Merritt
Why would you want to check if an atom is in the atom table?

I thought the atom table was a has table of sorts that did not house  
duplicate atoms.

Am I wrong or is it a huge time saver to check if the string is in  
the atom table before attempting to create an atom?

        Thanks, Joel

On Dec 20, 2006, at 9:59 PM, Eric Merritt wrote:

> Is there anyway to introspect the atom table? I would like to be able
> to take a string and check if that string has been interned in the
> atom table.

--
http://wagerlabs.com/





_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

yarivvv
In reply to this post by Michael McDaniel-2
Do repeated calls to list_to_atom(Str) with the same Str value
actually create new atoms, or does list_to_atom() check if the atom
already exists? (I thought it was the latter)

Yariv

>  My understanding is that "an atom is forever".  In long running
>  code I don't want to keep creating new atoms if I already have
>  one.  The above I use where I have sporadic communication between
>  remote servers that could come online or offline.
>
>  This was one example where I might not want to keep creating new
>  atoms so use the try/catch idiom.
>
> ~M
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

Eric Merritt
In reply to this post by Joel Reymont
I my case, its because I want to make it very easy to send 'matchable'
messages into the system from the outside world. The fact that the
atom table isn't garbage collected makes that somewhat dangerous. I
can mitigate that danger by only converting strings to atoms for atoms
that already exist.  There are dangers to even this approach. However,
those dangers can be mitigated as well.

On 12/20/06, Joel Reymont <[hidden email]> wrote:

> Why would you want to check if an atom is in the atom table?
>
> I thought the atom table was a has table of sorts that did not house
> duplicate atoms.
>
> Am I wrong or is it a huge time saver to check if the string is in
> the atom table before attempting to create an atom?
>
>         Thanks, Joel
>
> On Dec 20, 2006, at 9:59 PM, Eric Merritt wrote:
>
> > Is there anyway to introspect the atom table? I would like to be able
> > to take a string and check if that string has been interned in the
> > atom table.
>
> --
> http://wagerlabs.com/
>
>
>
>
>
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

Eric Merritt
In reply to this post by Kostis Sagonas-2
Kostis,

 Any specific caveats that I should be aware of?

> Handle with care:
>
>
> Eshell V5.5.2.1  (abort with ^G)
> 1> list_to_existing_atom("erlang").
> erlang
> 2> list_to_existing_atom("gazonk").
>
> =ERROR REPORT==== 20-Dec-2006::23:06:58 ===
> Error in process <0.29.0> with exit value:
> {badarg,[{erlang,list_to_existing_atom,["gazonk"]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}
>
> ** exited: {badarg,[{erlang,list_to_existing_atom,["gazonk"]},
>                      {erl_eval,do_apply,5},
>                      {shell,exprs,6},
>                      {shell,eval_loop,3}]} **
>
> Kostis
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

Robert Virding-3
In reply to this post by yarivvv
It checks if the atom exists, you can only have 1 copy of an atom!
That's why testing them equality is so fast.

Robert

Yariv Sadan wrote:

> Do repeated calls to list_to_atom(Str) with the same Str value
> actually create new atoms, or does list_to_atom() check if the atom
> already exists? (I thought it was the latter)
>
> Yariv
>
>
>> My understanding is that "an atom is forever".  In long running
>> code I don't want to keep creating new atoms if I already have
>> one.  The above I use where I have sporadic communication between
>> remote servers that could come online or offline.
>>
>> This was one example where I might not want to keep creating new
>> atoms so use the try/catch idiom.
>>
>>~M
>>_______________________________________________
>>erlang-questions mailing list
>>[hidden email]
>>http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

yarivvv
In reply to this post by Eric Merritt
In ErlyWeb, I ran into a similar situation, where browser requests
would have to be matched against controller/function names. What I did
was create a gb_trees structure with string keys being the names of
the valid controllers and the values being the equivalent atom values
as well as lists of controller function names. Instead of converting
controller/function names of incoming requests to atoms, ErlyWeb looks
them up in the gb_trees structure for the application. This is how
ErlyWeb avoids allocating atoms for incoming requests.

I hope this approach has similar performance to checking the existence
of atoms. Does anybody think the latter is better?

Regards,
Yariv

On 12/20/06, Eric Merritt <[hidden email]> wrote:

> I my case, its because I want to make it very easy to send 'matchable'
> messages into the system from the outside world. The fact that the
> atom table isn't garbage collected makes that somewhat dangerous. I
> can mitigate that danger by only converting strings to atoms for atoms
> that already exist.  There are dangers to even this approach. However,
> those dangers can be mitigated as well.
>
> On 12/20/06, Joel Reymont <[hidden email]> wrote:
> > Why would you want to check if an atom is in the atom table?
> >
> > I thought the atom table was a has table of sorts that did not house
> > duplicate atoms.
> >
> > Am I wrong or is it a huge time saver to check if the string is in
> > the atom table before attempting to create an atom?
> >
> >         Thanks, Joel
> >
> > On Dec 20, 2006, at 9:59 PM, Eric Merritt wrote:
> >
> > > Is there anyway to introspect the atom table? I would like to be able
> > > to take a string and check if that string has been interned in the
> > > atom table.
> >
> > --
> > http://wagerlabs.com/
> >
> >
> >
> >
> >
> >
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

Jani Hakala
"Yariv Sadan" <[hidden email]> writes:

> Instead of converting
> controller/function names of incoming requests to atoms, ErlyWeb looks
> them up in the gb_trees structure for the application. This is how
> ErlyWeb avoids allocating atoms for incoming requests.
>
> I hope this approach has similar performance to checking the existence
> of atoms. Does anybody think the latter is better?
>
gb_trees is O(log(n)) and atom things are O(1) (atom table seems to be
a hash), and list_to_existing_atom seems to be a BIF. I would be
surprised if list_to_existing_atom() didn't win in a performance
test.

Jani Hakala
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

yarivvv
I originally thought about using a dict (O(1)) instead of a gb_trees,
but I read that gb_trees can be faster for a small number of keys, so
I went with gb_trees. I guess I should do some benchmarking to see
which is fastest for a typical ErlyWeb app: gb_trees, dict or
list_to_existing_atom(),

Ugh, running benchmarks is sooo boring... :)

Yariv

> gb_trees is O(log(n)) and atom things are O(1) (atom table seems to be
> a hash), and list_to_existing_atom seems to be a BIF. I would be
> surprised if list_to_existing_atom() didn't win in a performance
> test.
>
> Jani Hakala
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

Michael McDaniel-2
In reply to this post by yarivvv
 Aha, thank you for the question and the answer.  I have a few
 places in my code to change the try/catch I've been using.  I
 do not remember where/when I started thinking new atoms would
 be created and changed my code from simple list_to_atom funs.

~M

On Wed, Dec 20, 2006 at 07:41:27PM -0500, Yariv Sadan wrote:

> Do repeated calls to list_to_atom(Str) with the same Str value
> actually create new atoms, or does list_to_atom() check if the atom
> already exists? (I thought it was the latter)
>
> Yariv
>
> >  My understanding is that "an atom is forever".  In long running
> >  code I don't want to keep creating new atoms if I already have
> >  one.  The above I use where I have sporadic communication between
> >  remote servers that could come online or offline.
> >
> >  This was one example where I might not want to keep creating new
> >  atoms so use the try/catch idiom.
> >
> > ~M
> > _______________________________________________
> > erlang-questions mailing list
> > [hidden email]
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> >
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
> !DSPAM:52,4589d85218341742597741!
>
>

--
Michael McDaniel
Portland, Oregon, USA
http://autosys.us
+1 503 283 5284
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

Michael McDaniel-2
In reply to this post by Robert Virding-3

 Aha, thank you for the question and the answer.  I have a few
 places in my code to change the try/catch I've been using.  I
 do not remember where/when I started thinking new atoms would
 be created and changed my code from simple list_to_atom funs.

~M
(had a bit of email problem, pasted answer on wrong msg before!)


On Thu, Dec 21, 2006 at 02:37:33AM +0100, Robert Virding wrote:

> It checks if the atom exists, you can only have 1 copy of an atom!
> That's why testing them equality is so fast.
>
> Robert
>
> Yariv Sadan wrote:
> > Do repeated calls to list_to_atom(Str) with the same Str value
> > actually create new atoms, or does list_to_atom() check if the atom
> > already exists? (I thought it was the latter)
> >
> > Yariv
> >
> >
> >> My understanding is that "an atom is forever".  In long running
> >> code I don't want to keep creating new atoms if I already have
> >> one.  The above I use where I have sporadic communication between
> >> remote servers that could come online or offline.
> >>
> >> This was one example where I might not want to keep creating new
> >> atoms so use the try/catch idiom.
> >>
> >>~M
> >>_______________________________________________
> >>erlang-questions mailing list
> >>[hidden email]
> >>http://www.erlang.org/mailman/listinfo/erlang-questions
> >>
> >
> > _______________________________________________
> > erlang-questions mailing list
> > [hidden email]
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> >
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
> !DSPAM:52,4589e57e18341971831101!
>
>

--
Michael McDaniel
Portland, Oregon, USA
http://autosys.us
+1 503 283 5284
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

Ulf Wiger-2
In reply to this post by yarivvv
Den 2006-12-21 02:54:47 skrev Yariv Sadan <[hidden email]>:

> In ErlyWeb, I ran into a similar situation, where browser requests
> would have to be matched against controller/function names. What I did
> was create a gb_trees structure with string keys being the names of
> the valid controllers and the values being the equivalent atom values
> as well as lists of controller function names. Instead of converting
> controller/function names of incoming requests to atoms, ErlyWeb looks
> them up in the gb_trees structure for the application. This is how
> ErlyWeb avoids allocating atoms for incoming requests.

I don't think that performance will be much of an issue in this
particular application - whether you use gb_trees, dict, or
the atom table. The data set is likely to be reasonably small
(that is, not in the tens of thousand).

If the names actually correspond to function names, you can take
advantage of the fact that the function names of all loaded modules
exist in the atom table. Then, list_to_existing_atom/1 is a
blindingly fast way to test whether the name can possibly be a
valid function name. A problem is of course that you have to make
sure that the module is loaded first.

If you knew of a usable meta programming facility, you could
generate code for the mapping,

   valid_function_name(ControllerStr, FunctionStr) -> {M,F}

Since you'll be adding controllers at "operator frequency"
(i.e. it's a manual process), the compile time should be
negligible.

This way, you create the atoms once, when compiling and loading
the module, and the "lookup" becomes about as fast as you can
make it (except perhaps if you use binaries rather than strings).

BR,
Ulf W
--
Ulf Wiger
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [erlang-questions] Introspecting the atom table

Kostis Sagonas-2
In reply to this post by Eric Merritt
Eric Merritt wrote:
> Kostis,
>
> Any specific caveats that I should be aware of?

The only reservation that I have about this, is that you better have a
DAMN good reason to use it.

Most -- though not all -- users that think they need the
list_to_existing_atom/1 built-in, typically do not really need it.
Often there is a much better/cleaner way to structure the
application that does not use list_to_existing_atom/1.

Hence my comment below.

Kostis

>> Handle with care:
>>
>>
>> Eshell V5.5.2.1  (abort with ^G)
>> 1> list_to_existing_atom("erlang").
>> erlang
>> 2> list_to_existing_atom("gazonk").
>>
>> =ERROR REPORT==== 20-Dec-2006::23:06:58 ===
>> Error in process <0.29.0> with exit value:
>> {badarg,[{erlang,list_to_existing_atom,["gazonk"]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}
>>
>>
>> ** exited: {badarg,[{erlang,list_to_existing_atom,["gazonk"]},
>>                      {erl_eval,do_apply,5},
>>                      {shell,exprs,6},
>>                      {shell,eval_loop,3}]} **
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Loading...