caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: cedilla@gmail.com
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] enter_blocking_section() and string modifications
Date: Wed, 02 Apr 2008 13:02:05 +0900 (JST)	[thread overview]
Message-ID: <20080402.130205.125897873.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <47F2F9BC.4020502@gmail.com>

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


  reply	other threads:[~2008-04-02  4:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-02  3:13 Reed Wilson
2008-04-02  4:02 ` Jacques Garrigue [this message]
2008-04-02  4:26   ` [Caml-list] " Reed Wilson
2008-04-02  8:13     ` Romain Beauxis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080402.130205.125897873.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@yquem.inria.fr \
    --cc=cedilla@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).