[MLton] Vector.fromList[] performance

Matthew Fluet fluet at cs.cornell.edu
Tue Oct 24 13:30:26 PDT 2006


> I have a largeish program that uses vectors a lot: it's a geometry
> code that is written in arbitrary-dimensional fashion.  Some of the
> code is in special dimension, so I often find myself converting tuples
> (x,y) into vectors using Vector.fromList[x,y].
>
> Unfortunately, the performance of this utterly sucks: I don't know how
> to read the generated bytecode to see exactly what's happening, but I
> can't fathom how it could be so bad without paying for building the
> list, then converting it to a vector.

Well, building the list and then converting it to a vector is exactly what 
it does, since that is what 'Vector.fromList [x,y]' does.

> How hard would it be to special-case Vector.fromList on a literal
> list?

It's not trivial.  'Vector.fromList' is implemented in SML, so the 
compiler doesn't treat it specially.  Trying to identify exactly this code 
would be pretty tough.

On the other hand, a loop-unrolling optimization would likely help both 
this idom and other code.

> SML/NJ allows this using the #[] construct, which helps SML/NJ
> on this test program knock the pants off mlton.  But really the
> compiler should just handle it automatically so that I can write
> standard-compliant code.

Right.  You could probably use MLton.Vector.unfoldi or the even more 
powerful MLton.Vector.create (in SVN) to build the vector directly, but it 
suffers from the same portability problem.



More information about the MLton mailing list