Re: List and String concatenation speed.

Kostirya <[email protected]> Wed, 31 Aug 2016 09:10:14 +0300
Newsgroups gmane.comp.lang.ml.mlton.user
Message-ID <CAG+QnTecmTApjF9eAAXcqpqyhK1U+oCq10Rh_mCBYS_FFFiR9g@mail.gmail.com>
2016-08-30 22:44 GMT+03:00 Matthew Fluet <[email protected]>:

>    Nice!  And is this implementation now faster than the Basis Library's
>    implementation of String.^?  If so, then that is good evidence that we
>    should invest in some kind of memmove-like primitive for copying data
>    between sequences.  Doing it as a primitive with compiler/Basis
>    Library support would allow us to use it for all kinds of sequences,
>    so that it could speed up things like Vector.concat and Array.copy.

Yes, faster. See below result.


> mlton -default-ann 'allowFFI true' string_concat_memmove.mlb
string_concat_memmove.c && ./string_concat_memmove
abcdef
String.^ 2.718664
string_concat_memmove 0.172177


> cat string_concat_memmove.sml
    val string_concat_memmove = _import "string_concat_memmove" :
Word8.word array * string * int * string * int -> unit;

    fun op ^ (a: string, b: string): string =
        let
            val a_length = String.size a
            and b_length = String.size b
        in
            if a_length = 0 then b else
            if b_length = 0 then a else
            let
                val arr = Primitive.Array.arrayUnsafe (a_length + b_length)
            in
                string_concat_memmove(arr, a, a_length, b, b_length);
                Primitive.String8.idFromWord8Vector
(Primitive.Vector.fromArrayUnsafe arr)
            end
        end

val _ = print ("abc" ^ "def\n")


val n = 10000
val a = "a"
val b = Byte.bytesToString(Word8Vector.tabulate(n, fn(i) =>
Byte.charToByte(#"a")));

fun runBench N name f =
  let
    fun loop 0 f = ()
      | loop i f = (f (); loop (i - 1) f)

    val t0 = Time.now ()
    val _ = loop N f
    val t1 = Time.now ()
  in
    print (name ^ " " ^ Real.toString(Time.toReal(Time.-(t1, t0))) ^ "\n")
  end

val _ = runBench 100000 "String.^" (fn () => ( String.^(a, b); ()))
val _ = runBench 100000 "string_concat_memmove" (fn () => ( a ^ b; ()))


> cat string_concat_memmove.c
#include <sys/types.h>
#include <string.h>

void string_concat_memmove(void *dst, const void *src1, size_t len1, const
void *src2, size_t len2) {
        memmove(dst, src1, len1);
        dst += len1;
        memmove(dst, src2, len2);
        return;
}

-- 
You received this message because you are subscribed to the Google Groups "MLton-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

------------------------------------------------------------------------------

_______________________________________________
MLton-user mailing list
[email protected]; [email protected]
https://lists.sourceforge.net/lists/listinfo/mlton-user