[squeak-dev] The Inbox: Files-dtl.166.mcz

Levente Uzonyi leves at caesar.elte.hu
Sun Sep 4 19:56:52 UTC 2016


Error handling is slow. It creates many additional objects (blocks, 
contexts, exceptions). What #adoptInstance: actually saves here is the 
reallocation of a 2k buffer. So I suppose the error handler is slower 
than creating a new collection, but this time it's slower for both Spur 
and V3.

The real problem is that it seems #adoptInstance: doesn't work in V3. Why 
is that?

Levente

P.S.: I wrote a small script to see the performance difference and ran it 
on Spur. It shows that my assumption above was incorrect. Still, the 
overhead is rather high, so there's an alternative workaround at the 
bottom, which should be quicker on both V3 and Spur than using exceptions. 
But I still think that #adoptInstance: should work on V3, too.

| s |
s := ByteString new: 2048.
{
"Old behavior"
[
 	s := s asByteArray.
 	s := s asString ].
"Simulate failing adoptInstance: with #error. The actual overhead is 
higher, because the stack is deeper."
[
 	[ self error ]
 		on: Error
 		do: [ s := s asByteArray ].
 	[ self error ]
 		on: Error
 		do: [ s := s asString ] ].
"New behavior."
[
 	ByteArray adoptInstance: s.
 	ByteString adoptInstance: s ].
"Proposed workaround."
[
 	[ ByteArray adoptInstance: s ]
 		on: Error
 		do: [ s := s asByteArray ].
 	[ ByteString adoptInstance: s ]
 		on: Error
 		do: [ s := s asString ] ].
"Alternative workaround."
[
 	Smalltalk isRunningSpur
 		ifTrue: [ ByteArray adoptInstance: s ]
 		ifFalse: [ s := s asByteArray ].
 	Smalltalk isRunningSpur
 		ifTrue: [ ByteString adoptInstance: s ]
 		ifFalse: [ s := s asByteString ] ] }
 	collect: [ :block |
 		Smalltalk garbageCollect.
 		block bench ].
#(
 	'182,000 per second. 5.5 microseconds per run.'
 	'106,000 per second. 9.43 microseconds per run.'
 	'10,400,000 per second. 96.6 nanoseconds per run.'
 	'3,940,000 per second. 254 nanoseconds per run.'
 	'9,050,000 per second. 110 nanoseconds per run.')

P.P.S.: By using (Character format bitAnd: 16rFFFF) = 0 instead of 
Smalltalk isRunningSpur, the overhead of the alternative 
solution will disappear on Spur.

On Sun, 4 Sep 2016, David T. Lewis wrote:

> I don't know if this is a good idea, so it goes in the inbox.
>
> Dave
>
>
> On Sun, Sep 04, 2016 at 07:09:28PM +0000, commits at source.squeak.org wrote:
>> David T. Lewis uploaded a new version of Files to project The Inbox:
>> http://source.squeak.org/inbox/Files-dtl.166.mcz
>>
>> ==================== Summary ====================
>>
>> Name: Files-dtl.166
>> Author: dtl
>> Time: 4 September 2016, 3:09:29.495898 pm
>> UUID: 85945e53-39b3-4300-a08c-d189a4b9960d
>> Ancestors: Files-tfel.165
>>
>> Let the Files package be  identical for Spur and V3 images by adding error handling in StandardFileStream>>binary and StandardFileStream>>ascii
>>
>> =============== Diff against Files-tfel.165 ===============
>>
>> Item was changed:
>>   ----- Method: StandardFileStream>>ascii (in category 'properties-setting') -----
>>   ascii
>>   	"Read and/or write in ASCII mode."
>>   	buffer1
>>   		ifNil: [ buffer1 := ByteString new: 1 ]
>> + 		ifNotNil: [ [ ByteString adoptInstance: buffer1 ]
>> + 						on: Error "V3 image"
>> + 						do: [ buffer1 := ByteString new: 1 ] ].
>> + 	collection ifNotNil: [ [ ByteString adoptInstance: collection ]
>> + 							on: Error
>> + 							do: [ collection := collection asString ] ].
>> - 		ifNotNil: [ ByteString adoptInstance: buffer1 ].
>> - 	collection ifNotNil: [ ByteString adoptInstance: collection ].
>>   	lastWritten ifNotNil:
>>   		[ lastWritten isInteger ifTrue: [ lastWritten := lastWritten asCharacter ] ]!
>>
>> Item was changed:
>>   ----- Method: StandardFileStream>>binary (in category 'properties-setting') -----
>>   binary
>>   	"Read and/or write in binary mode."
>>   	buffer1
>>   		ifNil: [ buffer1 := ByteArray new: 1 ]
>> + 		ifNotNil: [ [ ByteArray adoptInstance: buffer1 ]
>> + 						on: Error "V3 image"
>> + 						do: [ buffer1 := ByteArray new: 1 ] ].
>> + 	collection ifNotNil: [ [ ByteArray adoptInstance: collection ]
>> + 							on: Error
>> + 							do: [ collection := collection asByteArray ] ].
>> - 		ifNotNil: [ ByteArray adoptInstance: buffer1 ].
>> - 	collection ifNotNil: [ ByteArray adoptInstance: collection ].
>>   	lastWritten ifNotNil:
>>   		[ lastWritten isCharacter ifTrue: [ lastWritten := lastWritten asInteger ] ]!
>>
>
>


More information about the Squeak-dev mailing list