Lazy Optimization: Patterns for Efficient Smalltalk Programming


By: K. Auer, K. Beck
Published in: PLoPD2
Pages: 19-42

Creation of efficient Smalltalk programs. Most patterns could be applied in most languages.

Category: Performance, Smalltalk Idioms

Summary: Creation of efficient Smalltalk programs. Most patterns could be applied in most languages.

Url: http://www.rolemodelsoft.com/patterns/

Pattern: Performance Assessment

Pages: 21-23

To begin early program development, confident that performance won't be a problem, write a short (e.g., two-page) performance assessment of the system. Identify critical resources outside the programming language. Estimate how big and fast the final system will be. Develop prototypes for areas of concern if absolutely necessary.

Category: Performance, Smalltalk Idioms

Pattern: Lazy Optimization

Pages: 23-25

To achieve acceptable performance with the least cost, ignore efficiency through most of the development cycle. Tune performance once the program is running correctly and the design reflects your best understanding of how the cost should be structured. The changes will be limited in scope or will illuminate opportunities for better design.

Category: Performance, Smalltalk Idioms

Pattern: Performance Criteria

Pages: 25

Work with your client to get estimates for response time or throughput criteria for the system. Revise this list as the system matures.

Category: Performance, Smalltalk Idioms

Pattern: Threshold Switch

Pages: 26

You've applied Performance Criteria and started to improve the problem areas. To determine when you've satisfied the performance requirements, you and your client should agree that when you've reached the performance criteria, you will stop tuning. Once you've agreed, begin actual tuning. Use Performance Measurement

Category: Performance, Smalltalk Idioms

Pattern: Performance Measurement

Pages: 26-28

You've used Threshold Switch. To consistently measure performance and ensure that the system keeps running while you're tuning and know when you've triggered the threshold switches, write a routine that automatically tests and times apparent performance problems. Use a profiler to identify the biggest bottlenecks in the problem areas.

Category: Performance, Smalltalk Idioms

Pattern: Hot Spot

Pages: 28-29

You've used Performance Criteria and identified several areas that don't meet requirements. Profile most or all performance problems before putting a lot of energy into prioritizing, assigning, and fixing them. Identify where the majority of time is spent for each problem. These are the hot spots. Multiple performance problems may be the result of an approach that spans multiple areas.

Category: Performance, Smalltalk Idioms

Pattern: Experiment

Pages: 29-30

You've used Hot Spot. To find the best performance solution, treat each performance improvement as an experiment. If it doesn't get you the desired result, make a note of what you did and what it gave you, and undo it. Keep only the experiments that help you reach your goal, using Performance Criteria. You may have to apply several to get desired results.

Category: Performance, Smalltalk Idioms

Pattern: Cacheable Expression

Pages: 30-32

The profiler has identified an expression as a significant contributor to execution time. The expression is executed many times, often with the same result. Use the lowest-overhead variable to alleviate the redundancy.

Category: Performance, Smalltalk Idioms

Pattern: Caching Temporary Variable

Pages: 32-33

You're using Cacheable Expression, and it's causing a Hot Spot because it occurs in a loop. For each expensive expression in the loop that returns the same value each time through, add a temporary variable initialized before the loop begins to the value of the expression. Replace all occurrences of the expression with the temporary variable. If this doesn't solve the performance problem, try Caching Instance Variable.

Category: Performance, Smalltalk Idioms

Pattern: Caching Argument

Pages: 33-34

You're having performance problems. Similar transient objects are being created to represent parts of the end result. Reduce the number of transient objects. Keep a single transient object and pass it around as an argument.

Category: Performance, Smalltalk Idioms

Pattern: Caching State Variable

Pages: 34-37

You're using Cacheable Expression and causing a Hot Spot. The expression is needed in varied contexts, so you can't use Caching Argument or Caching Temporary Variable. To improve performance, identify the behavioral environment (Class, Class hierarchy, MetaClass) that encapsulates all the contexts where the expression is shared. Add a corresponding state variable and give it the name of the message to be cached. Rename the message to be cached by prepending "compute" to the corresponding selector. Use Lazy Initialization to compute the value of the variable. This value should be reset when an event occurs that could make this value obsolete.

Category: Smalltalk Idioms

Pattern: Simplification

Pages: 37-38

Several methods are Hot Spots in a certain context that cannot be simplified without compromising the integrity of the system for all instances of the same class. To improve the Hot Spots without causing undesirable side effects, examine the features of a class actually used by an instance or group of instances. Determine if there is another class that could provide the desired features more efficiently.

Category: Performance, Smalltalk Idioms

Pattern: Transient Reduction

Pages: 38-39

You're improving the performance of a system and you have a set of objects, A, and would like to have a different set, B. To move from A to B in the most efficient manner, reduce the number of transient objects in an operation. Use Caching Argument, Concatenating Stream, and Object Transformation

Category: Performance, Smalltalk Idioms

Pattern: Object Transformation

Pages: 39

You're using Transient Reduction. Instead of creating new objects to replace the old ones, transform the old objects into the new objects.

Category: Performance, Smalltalk Idioms

Pattern: Hypoth-A-Sized Collection

Pages: 40

In a Hot Spot, the program is spending time adding objects to a collection.When you realize that a significant number of objects will be added to a collection, be sure the collection is large enough to hold the end result.

Category: Smalltalk Idioms

Pattern: Concatenating Stream

Pages: 41

You want to improve the performance of a Smalltalk system. To concatenate several collections efficiently, use a Stream. See Concatenating Stream [Beck97]

Category: Smalltalk Idioms