13.07.2015 Views

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ByteArrayYou need to represent an Array of small numbers.How do you represent an Array of numbers in the range 0..255?Software engineering was born in a resource starved environment. There have never been enoughcycles or enough memory to do what we want. While this was more true in the past than it is today,the increasing demands of application consumers will always push the available resources.One of the most creative reactions to the lack of available memory has been the ways softwareengineering has found to use eight bit bytes. The EBCDIC and ASCII codes encode printablecharacters as bytes. The P-Code and <strong>Smalltalk</strong> byte code instruction sets encode virtual machineinstructions as bytes. The concatenation of four or eight bytes can form a memory address.One the one hand, you would like your <strong>Smalltalk</strong> program to be as insulated as possible fromrepresentation decisions, in order to be as flexible as possible. On the other hand, someconsideration of space efficiency must be possible so you can code in a still-finite environment.There are times when you really mean to communicate that information is stored as eight bit bytes.Then, using a more general representation would actually be misleading. You want to use therepresentation that most directly communicates your intent.The collection classes provide an excellent means for achieving the flexibility of a hiddenrepresentation and the space efficiency of encoding information as bytes. Choosing a representationthat stores only bytes effectively communicates that the objects contained in that collection canonly be SmallIntegers in the range 0..255.Use a ByteArray to store objects that consist solely of SmallIntegers in the range 0..255.When SmallIntegers are in a ByteArray, they are represented much as you would represent them inC or assembly language- simple eight bit patterns. The code for at:put: converts from <strong>Smalltalk</strong>’sformat for SmallIntegers into eight bit patterns. The code for at: does the reverse conversion.If you are storing lots of information as bytes, you may find dramatic increases in speed ordecreases in memory footprint after changing a few references to “Array” into references to“ByteArray”. For current <strong>Smalltalk</strong> systems with 32-bit object references, the savings is a factor offour. If you have lots of small ByteArrays, don’t expect the same savings, because the spaceoverhead of each ByteArray (typically 8 or 12 bytes) will be large compared to the informationstored in it.<strong>Coding</strong> <strong>Patterns</strong> page 98 of 147 9/30/2006

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!