Pseudo-Messages

Search
 

Smalltalk MT has a unique feature calling inlining where the compiler can create assembler directly instead of using a message send. This is purely a performance optimization but it can give your application a major boost. Let's have a look at a small example:

String handling is a common activity in Smalltalk. Create two methods:

method1
    method1
    | str char |

    str := 'abc'.
    ^Time millisecondsToRun: [
        10000000 timesRepeat: [char := (str at: 1) asInteger]
    ]

method2
    | str char |

    str := 'abc'.
    ^Time millisecondsToRun: [
        10000000 timesRepeat: [char := str _byteAt: 1]
    ]

Once you have entered the method, click right on the method name in the Class Hierarchy Browser and select Messages. Messages will show you what messages are being sent by a method. In method one, you will see asInteger, at: and millisecondsToRun: are being sent. And the first two messages are being sent 10 million times. This method executes for me in about 795 milliseconds.

Method two uses an inline method _byteAt to pull the integer value out of the string. When the compiler encounters _byteAt: it generates the necessary machine code at the point. If you look at Messages for method two, you will only see millisecondsToRun:. Method two executes for me in about 35 milliseconds.

The difference here is at: and asInteger are no longer being called and we are avoiding 20 million message sends (at and asInteger). This translates into a 20 times speed increase using a Pseudo message.

Be aware that Inlined messages cannot be cascaded. An attempt to use an inlined message in a cascaded statement simply sends a regular message. If no corresponding implementation exists, the result is a DNU (does not understand) exception

There any Inlined Messages for many of the lower level accessing functions and these are areas where Smalltalk MT will excel over traditional Smalltalk.