From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 4BEE4BBCA for ; Wed, 2 Apr 2008 06:26:38 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlcCAHun8kfRVZK1c2dsb2JhbACRTwEMAwQFCRSUDYYj X-IronPort-AV: E=Sophos;i="4.25,591,1199660400"; d="scan'208";a="24483450" Received: from wa-out-1112.google.com ([209.85.146.181]) by mail4-smtp-sop.national.inria.fr with ESMTP; 02 Apr 2008 06:26:37 +0200 Received: by wa-out-1112.google.com with SMTP id k17so2890023waf.3 for ; Tue, 01 Apr 2008 21:26:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding; bh=Yp7sXFpxH/83NA8Br90W5Ne9/ICvbPHY7F0DYk5SPJQ=; b=a/ceqKsAoI3rviDMxqDZEfh2ToBCRzkuUs6Dbg1F6qEpnS4pQhHYPPsNM7l8webl4BjWO1CsSzfj/pZ12FuIR5Fur4ls7Rch78cbqECM6scgTMUB/WUcTgZUzjdSFl62Pfxjqwwa7oXtSxHYCP/9lptR5qSxrlqCzCwRqpWKZPM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding; b=KthFTZw7DGPFar9QQKXW9kPWrcL/D4i0OjT7llsqSeLsTUdKKLsrYg4KGNwb0x6RfpIksqUwU1hiPWsg9nrPUyqgHUEtXvbskALrAnkfW0+EZsGyr+Sw0eNA5iVFti3eJnXnSQpDGYoK0aMuZ5LnkHex0noJ9mSVxbYUVbL5rxs= Received: by 10.114.170.1 with SMTP id s1mr13898966wae.133.1207110396275; Tue, 01 Apr 2008 21:26:36 -0700 (PDT) Received: from ?192.168.1.100? ( [75.11.165.6]) by mx.google.com with ESMTPS id l23sm2445289waf.5.2008.04.01.21.26.31 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 01 Apr 2008 21:26:35 -0700 (PDT) Message-ID: <47F30AF3.1070100@gmail.com> Date: Tue, 01 Apr 2008 21:26:27 -0700 From: Reed Wilson User-Agent: Thunderbird 2.0.0.12 (Windows/20080213) MIME-Version: 1.0 To: Jacques Garrigue Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] enter_blocking_section() and string modifications References: <47F2F9BC.4020502@gmail.com> <20080402.130205.125897873.garrigue@math.nagoya-u.ac.jp> In-Reply-To: <20080402.130205.125897873.garrigue@math.nagoya-u.ac.jp> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; low-level:01 ocaml:01 ocaml:01 allocating:01 compaction:01 deallocated:01 camlparam:01 lablgtk:01 compaction:01 camlprim:01 camlparam:01 mlsize:01 wosize:01 wosize:01 val:01 Jacques Garrigue wrote: > From: Reed Wilson > >> 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