Saturday, July 9, 2011

Testing new Xtreams generators

So, I upgraded Squeak port of Xtreams, and just tested it on the primeFactorFactorial example:

primeFactorFactorialViaXtreams
    | primes powers factorial |
    primes := [:out | Integer primesUpTo: self + 1 do: [:prime | out put: prime ] ] reading.
    powers := primes collecting: [:p | p raisedToInteger: self - (self sumDigitsInBase: p) // (p - 1)].
    factorial := powers injecting: 1 into: [:product :power | product * power].
    ^(factorial positioning buffer: (XTRingBuffer new: 1 class: Array))
        -= 1;
        get

This looks good, except the final sentence which is not simple to read to my taste, remember it just extract the last element...

Now let's see performance:

Smalltalk garbageCollect.
Time millisecondsToRun: [10000 primeFactorFactorial].
-> 297

Smalltalk garbageCollect.
Time millisecondsToRun: [10000 primeFactorFactorialViaGeneratorAndXtream].
-> 390

Smalltalk garbageCollect.
Time millisecondsToRun: [10000 primeFactorFactorialViaXtreams].
-> 945

Huh?
MessageTally spyOn: [10000 primeFactorFactorialViaXtreams].

 - 820 tallies, 822 msec.

**Tree**
--------------------------------
Process: other processes
--------------------------------
47.8% {393ms} WeakArray class>>finalizationProcess
  39.4% {324ms} WeakFinalizationList class>>initTestPair
    |39.4% {324ms} Object class(Behavior)>>new
  8.4% {69ms} primitives
17.0% {140ms} EventSensor>>eventTickler
  17.0% {140ms} Delay>>wait
--------------------------------
Process: (40s) 86874: nil
--------------------------------
34.0% {279ms} XTPositionReadStream(XTReadStream)>>-=
  34.0% {279ms} XTPositionReadStream>>length
    34.0% {279ms} XTPositionReadStream>>++
      34.0% {279ms} XTPositionReadStream(XTReadStream)>>++
        34.0% {279ms} XTPositionReadStream>>read:into:at:
          34.0% {279ms} XTCollectReadStream>>read:into:at:
            34.0% {279ms} XTCollectReadStream>>directRead:into:at:

**Leaves**
39.4% {324ms} Object class(Behavior)>>new
34.0% {279ms} XTCollectReadStream>>directRead:into:at:
17.0% {140ms} Delay>>wait
8.4% {69ms} WeakArray class>>finalizationProcess

**Memory**
    old            -1,959,252 bytes
    young        +1,059,496 bytes
    used        -899,756 bytes
    free        -1,059,496 bytes

**GCs**
    full            3 totalling 455ms (55.0% uptime), avg 152.0ms
    incr        19 totalling 65ms (8.0% uptime), avg 3.0ms
    tenures        0
    root table    0 overflows

MessageTally is not reliable with COG VM, but clearly, only one third of execution time is spent in the primeFactorFactorialViaXtreams. It seems like something is wrong with this image, and implementing the generator via interprocess synchronisation did favour those CPU hijackers!

No comments:

Post a Comment