String representation in erlang

Peter-Henry Mander erlang@REDACTED
Tue Sep 13 16:13:24 CEST 2005


Hi Thinus,

Did you consider using binaries? They will probably be more space
efficient and can do more besides. If you're thinking of a transmission
protocol, binaries are the way to go.

Pete.

On Tue, 2005-09-13 at 12:10 +0200, Thinus Pollard wrote:
> Hi there
> 
> According to the Erlang efficiency guide a string is internally represented as 
> a list of integers, thus consuming 2 words (8 bytes on a 32bit platform) of 
> memory *per* character.
> 
> The attached code is an attempt at reducing the memory footprint of strings in 
> erlang (passing between functions etc etc).
> 
> The basic idea is to pack a string into n byte sized integers and unpacking 
> them on the other side. The text file called compare.txt also shows the 
> memory needed to represent strings in normal erlang strings and this string 
> packing.
> 
> Normal erlang strings are 2 words/character. The packed representation uses 1 
> word of memory per list element plus n bytes/wordsize per integer element, 
> where every integer element contain n characters.
> 
> Deficiencies:
> If the string length is not divisible by n, space is wasted (the string gets 
> padded with zeros). 
> 
> Usage:
> Pick your the integer representation length.
> packstring/1 takes a string returns a list of n byte integers
> unpackstring/1 takes an integer representation and returns a string.
> 
> There is a simple test suite in test/0.
> 
> If anyone can improve upon this code, please do. If this was an exercise in 
> futility, please let my know, I've only been programming erlang for 2 weeks 
> and still need to learn all the gotchas ;)
> 





More information about the erlang-questions mailing list