Slava,

In 'benchmark.mandel' you build up a byte vector. The two words which 
contribute to the byte vector are 'ppm-header' which is called once up front 
and 'render' which is called many times. The word 'mandel' ties it all 
together to and converts the byte vector to a byte array, suitable for filing 
out. So here are those words:

: ppm-header ( -- )
    "P6\n" % width # " " % height # "\n255\n" % ; inline

: render ( -- )
    height [ width swap '[ _ c pixel color % ] each ] each ; inline

: mandel ( -- data )
    buf-size <byte-vector>
    [ building [ ppm-header render ] with-variable ] [ B{ } like ] bi ;

I don't think this code merits the usage of 'building'. The 'ppm-header' 
and 'render' words can operate on a byte vector passed on the stack. Their 
stack effects would be:

        ( data -- data )

In this style, 'mandel' is much simpler:

: mandel ( -- data )
  buf-size <byte-vector>
    ppm-header
    render
  >byte-array ;

It's clear that you push a new byte vector on the stack, add the header to it, 
render the data to it, and finally yield a byte array.

OK, so what would render look like? You just replace the '%' with 'append!' :

: render ( data -- data )
  height [ width swap '[ _ c pixel color append! ] each ] each ; inline

I've written about 'append!' before. It's trivial:

        : append! ( a b -- ab ) over push-all ;

I can't really improve 'ppm-header' in terms of concision because of your 
usage of '%' and '#'; they pack alot of power. '#' hides 'number>string' and 
a 'push-all'. I'd probably write it like this:

: ppm-header ( data -- data )
  width number>string height number>string
  -> WIDTH HEIGHT
    { "P6\n" WIDTH " " HEIGHT "\n255\n" }
  concat
  append! ; inline

In theory this version of 'render' should be faster since the byte vector is 
kept on the stack.

Ed

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to