Version française
Home     About     Download     Resources     Contact us    
Browse thread
Pervasives.compare: slow?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: Pervasives.compare: slow?
> I used Pervasives.compare as a comparison function between hash values
> (16-byte strings). Pervasives.compare is a polymorphic hack that works by
> induction on the type information left for the garbage collector; even if
> you specify Pervasives.compare : string -> string -> int, it still invokes
> the polymorphic function.
> 
> There is apparently a 20-25% performance penalty using this form instead
> of a simple comparison procedure for 16-byte strings. I suspect the
> performance hit is even higher for more complex data structures.
> 
> Would it be possible to have ad hoc generated comparison functions? That
> sounds like it needs including polytypic features into the language, which
> is some very big stuff.

Actually, there is some "polytypic" optimisations going on in the
compiler.  For instance, the compiler will replace the generic "="
function by integer equality if both arguments are known to be of type
int.

Such "type-based strength reduction", as I like to call it, is
currently applied to all boolean comparisons (=, <>, <, <=, >, >=) but
not to "compare".  It could be done for "compare" as well; it's just
that no one has yet expressed a strong need for this...

- Xavier Leroy