erlang:atom_prefix/2 - zero-allocation atom prefix matching BIF

Checking whether an atom starts with a given prefix is a common pattern, but today it requires converting to a list/binary first:

lists:prefix(“erl_”, atom_to_list(Module))

This allocates on every call and creates GC pressure unnecessarily — the atom’s bytes are already sitting in the atom table.

I have a working BIF implementation of erlang:atom_prefix/2 that compares directly against the atom table’s internal UTF-8 bytes. Zero allocation, zero GC.

erlang:atom_prefix(hello_world, <<“hello”>>). %% true
erlang:atom_prefix(hello_world, “world”). %% false

The pattern already appears in at least 5 places in OTP (diameter_info, diameter_service, ct_netconfc, dialyzer tests) and 5 places in Elixir (code formatter, Mix help, type checker, autocomplete) via Atom.to_string/1 |> String.starts_with?/2.

Would this be a useful addition?

3 Likes