RE: Block evaluation with n+1 arguments

Steven Kelly <[email protected]> Tue, 11 Apr 2023 09:44:11 +0000
Newsgroups gmane.comp.lang.smalltalk.vwnc
Message-ID <HE1P194MB0027552F3BC3E49F30668DFBA69A9@HE1P194MB0027.EURP194.PROD.OUTLOOK.COM>
Steffen Märcker wrote Thursday, April 6, 2023 4:29 PM UTC+3:
> I want to evaluate a block an argument 'arg1' and additional n arguments
> given in an array 'args'. The following code does the trick:
> 
>     block valueWithArguments: (Array with: arg1) , args.
> 
> Is there a way to do this without the overhead of creating a new Array?

You're actually creating two new Arrays there, first explicitly via #with: and then implicitly via #,
If you really need to avoid time or memory overhead, this is about twice as fast:
a := Array new: (newSize := args size + 1).
a at: 1 put: arg1.
a replaceFrom: 2 to: newSize with: args startingAt: 1.

A version with streams is no faster than the original:
(a := Array new: args size + 1) writeStream nextPut: arg1; nextPutAll: args.

In recent VW 9.x versions you can simply write {arg1}, args — but that's slower than the original.

Steve