Jens Thiele | 10 Apr 20:48

c-wrapper: performance problem

Hi,

I am playing around with the really nice c-wrapper module. (thanks
alot!)
I stumbled across a performance problem:
Converting some c byte array (char*) to a u8vector is really slow. It
semms the bytes are copied byte for byte in scheme. But maybe my
approach is just plain wrong?

Attached a test script and some profile output (using -ptime).

Note: the profile output is for running the script with the line 24:
    (time (c-byte-array-≥u8vector2 (cast (c-array <c-uchar> bytes) array)))
commented out and for 50000 and 100000 bytes.

#!/usr/bin/gosh
;; -*- coding: utf-8; mode: scheme -*-
;; test: performance problem with char/byte* -> u8vector
(use c-wrapper)
(use gauche.uvector)
(use gauche.sequence)

(c-load '("malloc.h"))

;; todo: really slow
(define (c-byte-array-≥u8vector1 array)
  (cast <u8vector> array))

(Continue reading)

KOGURO, Naoki | 11 Apr 14:50

Re: c-wrapper: performance problem

Hi Jens,

On 2008/04/11, at 3:52, Jens Thiele wrote:
> I stumbled across a performance problem:
> Converting some c byte array (char*) to a u8vector is really slow. It
> semms the bytes are copied byte for byte in scheme. But maybe my
> approach is just plain wrong?

You're right, the cast method has not been optimized for uvector yet.
If you want to use bulk copy, you can use memcpy as you do in C, such  
like the code below.

(define buf (make-u8vector 100))
;; ary is C byte array.
(memcpy buf ary 100)

An uvector is used in pointer context, the pointer of the uvector's  
storage is passed to C function.

--
KOGURO, Naoki <naoki <at> koguro.net>

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Jens Thiele | 11 Apr 17:47

Re: c-wrapper: performance problem

"KOGURO, Naoki" <naoki <at> koguro.net> writes:

> Hi Jens,
>
> On 2008/04/11, at 3:52, Jens Thiele wrote:
>> I stumbled across a performance problem:
>> Converting some c byte array (char*) to a u8vector is really slow. It
>> semms the bytes are copied byte for byte in scheme. But maybe my
>> approach is just plain wrong?
>
> You're right, the cast method has not been optimized for uvector yet.
> If you want to use bulk copy, you can use memcpy as you do in C, such  
> like the code below.
>
> (define buf (make-u8vector 100))
> ;; ary is C byte array.
> (memcpy buf ary 100)
>
> An uvector is used in pointer context, the pointer of the uvector's  
> storage is passed to C function.

Ah ok. For now I use:
(define (c-byte-array-≥u8vector3 array)
  (let1 ret (make-u8vector (size-of array))
	(memcpy ret array (size-of array))
	ret))

$ ./c-wrapper-perfomance-problem 1000000
;(time (c-byte-array-≥u8vector1 (cast (c-array <c-uchar> bytes) array)))
; real  25.281
(Continue reading)


Gmane