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...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

RunArrayYou are using an OrderedCollection.How do you compactly represent an OrderedCollection or Array where you have the sameelement many times in a row?The simplest solution is just to use an OrderedCollection. During early development this isprobably the right answer. When you begin working with realistic sized data sets, you may discoverthat you are using more memory than you can afford.The classic example of this is text editing. Conceptually, each character in an editor has its ownfont, size, and decoration. The obvious representation of this is as an OrderedCollection of Styleobjects that parallels the characters. This might even work for short text. When you are dealingwith a whole book, though, the extra four bytes for the slot in the OrderedCollection plus the 12bytes for the Style header plus the 12 bytes for the variables in the Style plus all the bytes for theFont, Size, and Decoration objects really adds up.If you look at typical text, you’ll see that most of the information is redundant. There will be 50characters with one style, 200 with another, 90 with another, and so on. A RunArray stores this as“50 of style 1, 200 of style 2, 90 of style 3”. Each count-object pair is called a “run”.If we stored this information in an OrderedCollection, we would need 340 elements. A RunArrayneeds only three runs (each with two references).Storage efficiency comes at a cost. First, if you really have a different object in each element, aRunArray will take twice as much storage as an Array or OrderedCollection. Second, the timenecessary to access an element at the end of a RunArray is proportional to the number of runs. TheVisualWorks implementation avoids some of this cost by caching the position of the last elementfetched.Use a RunArray to compress long runs of the same element.The beauty of using a RunArray is that client code can’t know whether they have an Array, anOrderedCollection, or a RunArray, since they all support the same messages. You can code withwhichever one works best, then change your mind later without any appreciable cost.VisualWorks 2 implements RunArray. VisualAge and Visual<strong>Smalltalk</strong> do not at this writing.<strong>Coding</strong> <strong>Patterns</strong> page 88 of 147 9/30/2006

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

Saved successfully!

Ooh no, something went wrong!