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

Reduce atom leak #42

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

Conversation

williamthome
Copy link
Contributor

I've noticed that convert to_atom is used often.
This PR can help to reduce the atom leak by first trying to convert to an existing atom.

@@ -56,8 +56,14 @@ next_context_var(CState, #ws{nr=Nr} = Ws) ->

%% @doc Convert a list or binary to an atom.
-spec to_atom(string()|binary()|atom()) -> atom().
to_atom(L) when is_list(L) -> erlang:list_to_atom(L);
to_atom(B) when is_binary(B) -> erlang:binary_to_atom(B, 'utf8');
to_atom(L) when is_list(L) -> case catch erlang:list_to_existing_atom(L) of
Copy link
Member

Choose a reason for hiding this comment

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

It no need to add this logic here by reason that if atom already exists in the system, it will not create or re create new one, simple check:

1> erlang:memory(atom_used).
256891
2> list_to_atom("zotonic").
zotonic
3> erlang:memory(atom_used).
258010
4> list_to_atom("zotonic"). 
zotonic
5> erlang:memory(atom_used).
258010
6> list_to_existing_atom("zotonic").
zotonic
7> erlang:memory(atom_used).        
258010

As you can see, the size of atoms is not growing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, @vkatsuba! Always very helpful and informative.
I saw this pattern somewhere, and it stuck in my head. Your simple test proves that I'm thinking wrong about *_to_existing_atom.

Copy link
Member

Choose a reason for hiding this comment

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

@williamthome always welcome! You can also read about some best practice at https://erlef.github.io/security-wg/secure_coding_and_deployment_hardening. You are thinking about atom leak correct here and this is can be issue. But implementation can take some time and deep discussion with @zotonic/developers I suppose.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the informative link.
Yes, atom leak can be a problem, but I was wrong in thinking that list_to_atom or binary_to_atom always increases memory usage.

src/template_compiler_utils.erl Show resolved Hide resolved
@williamthome
Copy link
Contributor Author

So, the @vkatsuba review shows me that this PR does not make much (or no) sense.
Can be closed if no benefit is seen here.

@vkatsuba
Copy link
Member

So, the @vkatsuba review shows me that this PR does not make much (or no) sense. Can be closed if no benefit is seen here.

Not sure that this PR need to close, but I suppose it can be adapted a little bit.

@vkatsuba
Copy link
Member

@williamthome actually it looks like here is already provided some issue #4 with short possible solution approach. Please, take a look.

@mworrell
Copy link
Member

Maybe we can open a separate issue to discuss this.

On a related note I saw that we have a confusion about template variables being a binary or an atom. This can result in two keys (one binary and one atom) of the same variable.

I think it is best (due to atom leaks) to have variables as binaries.

@williamthome
Copy link
Contributor Author

@mworrell, what do you think about this PR? Should I adapt it and change all keys to binary?

@mworrell
Copy link
Member

mworrell commented Mar 28, 2023

Those atoms/binaries are more the keys for variables like in the with and include tags. I think they are now handled as atoms, as I saw some confusion when I started rendering a template in Zotonic using binary identifiers.

I think the more correct was is here to map those to binaries. That will mean that we will also need to make some changes on the Zotonic side, as scomps (etc) are probably using the atom keys.

@williamthome
Copy link
Contributor Author

Oh, this change can break things and causes a huge refactor.
@mworrell, I can also help with the Zotonic side if you think this change must be made, but maybe this change can be a problem for existing sites that uses custom scomps, filters, etc.

@mworrell
Copy link
Member

Yes, we can also postpone this change till the 1.1 or 2.0

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.

3 participants