caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* enter_blocking_section() and string modifications
@ 2008-04-02  3:13 Reed Wilson
  2008-04-02  4:02 ` [Caml-list] " Jacques Garrigue
  0 siblings, 1 reply; 4+ messages in thread
From: Reed Wilson @ 2008-04-02  3:13 UTC (permalink / raw)
  To: caml-list

Hi guys!

I'm currently writing a multi-threaded program, and part of the program 
is low-level string manipulation. I found that (with the code I'm using) 
making a tight C loop is quite a bit faster than using OCaml, so I'm 
using an external function for that.

My question is: can I use enter_blocking_section() for character 
replacement on an OCaml string? I know I can't use it for allocating 
anything on the heap due to the possibility of the GC doing funny things 
with it, but would straight replacement of existing string data be OK?

I did a test which runs OK, but "has not crashed" <> "will not crash"...

Thanks,
Reed


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] enter_blocking_section() and string modifications
  2008-04-02  3:13 enter_blocking_section() and string modifications Reed Wilson
@ 2008-04-02  4:02 ` Jacques Garrigue
  2008-04-02  4:26   ` Reed Wilson
  0 siblings, 1 reply; 4+ messages in thread
From: Jacques Garrigue @ 2008-04-02  4:02 UTC (permalink / raw)
  To: cedilla; +Cc: caml-list

From: Reed Wilson <cedilla@gmail.com>

> I'm currently writing a multi-threaded program, and part of the program 
> is low-level string manipulation. I found that (with the code I'm using) 
> making a tight C loop is quite a bit faster than using OCaml, so I'm 
> using an external function for that.
> 
> My question is: can I use enter_blocking_section() for character 
> replacement on an OCaml string? I know I can't use it for allocating 
> anything on the heap due to the possibility of the GC doing funny things 
> with it, but would straight replacement of existing string data be OK?

In general, you can't. The GC may move strings around.
However, if you disable compaction, and if your string is on the old
heap (i.e. it is not young), and you make sure it cannot be
deallocated (the usual CAMLparam macros should be enough for that),
then it should be ok.
Of course there is no point in going through this pain if you're
not sure that concurrency is going to improve performance.

Here is a snippet from ml_gpointer.c, in lablgtk, which copies an
abstract block to the old heap when it is young. Such a block is then
stable as long as there is no compaction.

CAMLprim value ml_stable_copy (value v)
{
    if (Is_block(v) && (char*)(v) < young_end && (char*)(v) > young_start)
    {
        CAMLparam1(v);
        mlsize_t i, wosize = Wosize_val(v);
        int tag = Tag_val(v);
        value ret;
        if (tag < No_scan_tag) invalid_argument("ml_stable_copy");
        ret = alloc_shr (wosize, tag);
        for (i=0; i < wosize; i++) Field(ret,i) = Field(v,i);
        CAMLreturn(ret);
    }
    return v;
}

Jacques Garrigue


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] enter_blocking_section() and string modifications
  2008-04-02  4:02 ` [Caml-list] " Jacques Garrigue
@ 2008-04-02  4:26   ` Reed Wilson
  2008-04-02  8:13     ` Romain Beauxis
  0 siblings, 1 reply; 4+ messages in thread
From: Reed Wilson @ 2008-04-02  4:26 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: caml-list

Jacques Garrigue wrote:
> From: Reed Wilson <cedilla@gmail.com>
> 
>> I'm currently writing a multi-threaded program, and part of the program 
>> is low-level string manipulation. I found that (with the code I'm using) 
>> making a tight C loop is quite a bit faster than using OCaml, so I'm 
>> using an external function for that.
>>
>> My question is: can I use enter_blocking_section() for character 
>> replacement on an OCaml string? I know I can't use it for allocating 
>> anything on the heap due to the possibility of the GC doing funny things 
>> with it, but would straight replacement of existing string data be OK?
> 
> In general, you can't. The GC may move strings around.
> However, if you disable compaction, and if your string is on the old
> heap (i.e. it is not young), and you make sure it cannot be
> deallocated (the usual CAMLparam macros should be enough for that),
> then it should be ok.
> Of course there is no point in going through this pain if you're
> not sure that concurrency is going to improve performance.
> 
> Here is a snippet from ml_gpointer.c, in lablgtk, which copies an
> abstract block to the old heap when it is young. Such a block is then
> stable as long as there is no compaction.
> 
> CAMLprim value ml_stable_copy (value v)
> {
>     if (Is_block(v) && (char*)(v) < young_end && (char*)(v) > young_start)
>     {
>         CAMLparam1(v);
>         mlsize_t i, wosize = Wosize_val(v);
>         int tag = Tag_val(v);
>         value ret;
>         if (tag < No_scan_tag) invalid_argument("ml_stable_copy");
>         ret = alloc_shr (wosize, tag);
>         for (i=0; i < wosize; i++) Field(ret,i) = Field(v,i);
>         CAMLreturn(ret);
>     }
>     return v;
> }
> 
> Jacques Garrigue

Thanks! That's exactly what I needed to know. All of my strings are 
around 8MB, so they'll all be allocated directly on the old heap. I'll 
test to see how much memory is used up by disabling compaction.

And in this case, the concurrency is definitely worth it. With 2 threads 
and 2 processors, using enter_blocking_section() raises CPU usage from 
~50% (i.e. 100% of 1 processor) to ~75%, with a corresponding increase 
in throughput.

Thanks again!
Reed


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] enter_blocking_section() and string modifications
  2008-04-02  4:26   ` Reed Wilson
@ 2008-04-02  8:13     ` Romain Beauxis
  0 siblings, 0 replies; 4+ messages in thread
From: Romain Beauxis @ 2008-04-02  8:13 UTC (permalink / raw)
  To: caml-list

Le Wednesday 02 April 2008 05:26:27 Reed Wilson, vous avez écrit :
> > Here is a snippet from ml_gpointer.c, in lablgtk, which copies an
> > abstract block to the old heap when it is young. Such a block is then
> > stable as long as there is no compaction.
> >
> > CAMLprim value ml_stable_copy (value v)
> > {
> >     if (Is_block(v) && (char*)(v) < young_end && (char*)(v) >
> > young_start) {
> >         CAMLparam1(v);
> >         mlsize_t i, wosize = Wosize_val(v);
> >         int tag = Tag_val(v);
> >         value ret;
> >         if (tag < No_scan_tag) invalid_argument("ml_stable_copy");
> >         ret = alloc_shr (wosize, tag);
> >         for (i=0; i < wosize; i++) Field(ret,i) = Field(v,i);
> >         CAMLreturn(ret);
> >     }
> >     return v;
> > }
> >
> > Jacques Garrigue
>
> Thanks! That's exactly what I needed to know. All of my strings are
> around 8MB, so they'll all be allocated directly on the old heap. I'll
> test to see how much memory is used up by disabling compaction.
>
> And in this case, the concurrency is definitely worth it. With 2 threads
> and 2 processors, using enter_blocking_section() raises CPU usage from
> ~50% (i.e. 100% of 1 processor) to ~75%, with a corresponding increase
> in throughput.

Isn't it possible to register a global variable for that purpose ?


Romain
-- 
He say 'im that he's the first one
Who discover Jamaica, I an' I say that:
What about the Awarak indians ?


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-04-02  8:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-02  3:13 enter_blocking_section() and string modifications Reed Wilson
2008-04-02  4:02 ` [Caml-list] " Jacques Garrigue
2008-04-02  4:26   ` Reed Wilson
2008-04-02  8:13     ` Romain Beauxis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).