Re: List and String concatenation speed.

Matthew Fluet <[email protected]> Tue, 30 Aug 2016 15:44:39 -0400
Newsgroups gmane.comp.lang.ml.mlton.user
Message-ID <CAMrhFL7ovbVKpxDDDfgHhCP5FaRyD351b8F2oP7s6n5cjLSFLA@mail.gmail.com>
On Tue, Aug 30, 2016 at 10:39 AM, Kostirya <[email protected]> wrote:
> Many thanks!
> I use
>
> 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
>
> and got 20 times performance boost!

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.


> 2016-08-30 16:57 GMT+03:00 Matthew Fluet <[email protected]>:
>>
>> No, that wouldn't be any faster; in fact, you've added a bit of overhead.
>>
>> The `Word8Array.array` needs to initialize the array, by writing 0w0
>> element by element into the array.  Then there is a little bit of FFI
>> overhead.  Then the `Byte.bytesToString` converts a mutable array to
>> an immutable vector, by copying element by element.
>>
>> Within the Basis Library implementation, we are able to use unsafe
>> features.  In particular, we are able to create an uninitialized array
>> to receive the elements and then we are able to perform a cast from
>> the array to vector.  We can consider adding a memmove like primitive
>> to improve these kinds of operations, but it would almost certainly
>> need to be within the compiler/Basis Library.
>>
>> On Tue, Aug 30, 2016 at 9:07 AM, Kostirya <[email protected]> wrote:
>> > Hello.
>> > I've decided to use FFI for String.^.
>> > However, the speed has not been increased (slowed down, in fact).
>> > The code is lower. I think I've done everything properly?
>> >
>> > 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;
>> > }
>> >
>> >
>> > 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 = Word8Array.array(a_length + b_length, 0w0)
>> >             in
>> >                 string_concat_memmove(arr, a, a_length, b, b_length);
>> >                 Byte.bytesToString (Word8Array.vector arr)
>> >             end
>> >         end
>> >
>> > Best Regards.
>> > Nick.
>> >
>> > 2016-08-29 16:46 GMT+03:00 Kostirya <[email protected]>:
>> >>
>> >> Yes. Poly/ML use memmove to to implement String.^
>> >>
>> >>
>> >> 2016-08-29 16:21 GMT+03:00 Matthew Fluet <[email protected]>:
>> >>>
>> >>> >> poly --script string_concat.sml
>> >>> > 0.462269
>> >>> >
>> >>> >> mlton string_concat.sml && ./string_concat
>> >>> > 2.713323
>> >>>
>> >>> It looks like Poly/ML uses memmove to implement String.^, which would
>> >>> be more efficient for character sequences.
>> >>
>> >>
>> >
>> > --
>> > 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
>> >
>>
>> --
>> 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
>
>
> --
> 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
>

-- 
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].


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