[squeak-dev] The Trunk: Collections-cmm.1016.mcz

Marcel Taeumel marcel.taeumel at hpi.de
Tue Jul 12 13:24:30 UTC 2022


Hi Chris --

Please make sure to have Text >> #format: be on par with your changes.

Note that you changes put more pressure on the GC:

['Hello {1}!' format: { 'Squeak' }] bench

BEFORE: '2,320,000 per second. 432 nanoseconds per run. 1.63967 % GC time.' 
AFTER: '1,340,000 per second. 746 nanoseconds per run. 16.92 % GC time.' 

Maybe we can tweak it a little bit more.

Best,

Marcel

P.S.: If you had not opted for re-layouting the entire method, I would be able to actually see what you changed there... ;-) ... pretty-print for obfuscation, I suppose? :-D
Am 07.07.2022 06:22:28 schrieb commits at source.squeak.org <commits at source.squeak.org>:
Chris Muller uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-cmm.1016.mcz

==================== Summary ====================

Name: Collections-cmm.1016
Author: cmm
Time: 6 July 2022, 11:22:16.540715 pm
UUID: 8eaa5fee-446e-4ccb-ae26-652e7a70e693
Ancestors: Collections-eem.1015

Let String>>#format: also support alphanumeric tokens (accessed via a Dictionary). Backward compatibility with classic numeric tokens via a SequenceableCollection is preserved.

=============== Diff against Collections-eem.1015 ===============

Item was changed:
----- Method: String>>format: (in category 'formatting') -----
+ format: aCollection
+ "Substitute tokens in the receiver with element values of aCollection. The tokens are indicated in curly-braces and may be either numeric, e.g., {1}, {2}, etc. and map to a SequenceableCollection, OR, alphanumeric, e.g., {name}, {date}, etc., in which case aCollection should be a Dictionary.
+ The values can be static or, with the specification of a Block element, dynamic.
+
+ Simplest examples:
+ 'foo {date} bar' format: ({'date'->Date today} as: Dictionary).
+
+ Dynamic calculation is allowed via Blocks.
+ 'foo {NOW} bar' format: ({'NOW'-> [DateAndTime now]} as: Dictionary).
+
+ Backward-compatible with numeric-only #format:
- format: arguments
- "Format the receiver with arguments.
-
- Simplest example:
'foo {1} bar' format: {Date today}.
+
+ Now with block support:
+ 'foo {1} bar' format: {[Date today]}.
+
+ Complete example with escaped characters:
-
- Complete example:
'\{ \} \\ foo {1} bar {2}' format: {12. 'string'}.
+ '\{ \} \\ foo {FOO} bar {BAR}' format: ({'FOO'->12. 'BAR'->'string'} as: Dictionary).
"
+ ^ self class new: self size * 11 // 10 "ready for +10% growth"streamContents:
+ [ : output | | lastIndex nextIndex key |
+ lastIndex := 1.
+ key := WriteStream on: (String new: 10).
+ [ "stream to output until first { or \"
+ (nextIndex := self indexOfAnyOf: FormatCharacterSet startingAt: lastIndex) = 0 ] whileFalse:
+ [ nextIndex = lastIndex ifFalse:
+ [ output next: nextIndex - lastIndex putAll: self startingAt: lastIndex ].
+ "special char hit, escape char?"
+ (self at: nextIndex) == $\
+ ifTrue: [ "transfer the escaped character."
+ output nextPut: (self at: (nextIndex := nextIndex + 1)) ]
+ ifFalse:
+ [ | nextKeyChar |
+ "${ char, parse the key"
+ [ nextKeyChar := self at: (nextIndex := nextIndex + 1).
+ nextKeyChar isAlphaNumeric ] whileTrue: [ key nextPut: nextKeyChar ].
+ nextKeyChar = $} ifFalse: [ self error: '$} expected' ].
+ output nextPutAll: (aCollection at: key contents numberOrValue) value asString.
+ key reset ].
+ lastIndex := nextIndex + 1 ].
+ lastIndex <= self size ifTrue:
+ [ output
+ next: self size - lastIndex + 1
+ putAll: self
+ startingAt: lastIndex ] ]!
- ^ self class new: self size * 11 // 10 "+10%" streamContents: [ :output |
- | lastIndex nextIndex |
- lastIndex := 1.
- [ (nextIndex := self indexOfAnyOf: FormatCharacterSet startingAt: lastIndex) = 0 ] whileFalse: [
- nextIndex = lastIndex ifFalse: [
- output next: nextIndex - lastIndex putAll: self startingAt: lastIndex ].
- (self at: nextIndex) caseOf: {
- [ $\ ] -> [ output nextPut: (self at: (nextIndex := nextIndex + 1)) ].
- [ ${ ] -> [
- "Parse the index - a positive integer in base 10."
- | digitValue collectionIndex |
- collectionIndex := 0.
- [ (digitValue := self basicAt: (nextIndex := nextIndex + 1)) between: 48 "$0 asciiValue" and: 57 "$9 asciiValue" ] whileTrue: [
- collectionIndex := collectionIndex * 10 + digitValue - 48 "$0 asciiValue". ].
- digitValue = 125 "$} asciiValue" ifFalse: [ self error: ('{1} expected' translated format: {$}}) ].
- output nextPutAll: (arguments at: collectionIndex) asString ] }.
- lastIndex := nextIndex + 1 ].
- lastIndex <= self size ifTrue: [
- output next: self size - lastIndex + 1 putAll: self startingAt: lastIndex ] ]!


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20220712/ff79273b/attachment.html>


More information about the Squeak-dev mailing list