From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id ABECC7FA4D for ; Tue, 29 Jul 2014 11:46:10 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of goswin-v-b@web.de) identity=pra; client-ip=212.227.15.3; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="goswin-v-b@web.de"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of goswin-v-b@web.de) identity=mailfrom; client-ip=212.227.15.3; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="goswin-v-b@web.de"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mout.web.de) identity=helo; client-ip=212.227.15.3; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="postmaster@mout.web.de"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AoIBAK5s11PU4w8DnGdsb2JhbABZ12oBgRQWEAEBAQEBBg0JCRQphAMBAQQBJxNECwsYCSUPBSiIYQEMDLctH4dLF49TFoMZgRsFm0uHBxORAA X-IPAS-Result: AoIBAK5s11PU4w8DnGdsb2JhbABZ12oBgRQWEAEBAQEBBg0JCRQphAMBAQQBJxNECwsYCSUPBSiIYQEMDLctH4dLF49TFoMZgRsFm0uHBxORAA X-IronPort-AV: E=Sophos;i="5.01,756,1400018400"; d="scan'208";a="73164072" Received: from mout.web.de ([212.227.15.3]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 29 Jul 2014 11:46:10 +0200 Received: from frosties.localnet ([78.43.112.61]) by smtp.web.de (mrweb004) with ESMTPSA (Nemesis) id 0MBp3P-1XItrr0iEJ-00Am8b for ; Tue, 29 Jul 2014 11:46:08 +0200 Received: from mrvn by frosties.localnet with local (Exim 4.82) (envelope-from ) id 1XC3yw-0003fc-Og for caml-list@inria.fr; Tue, 29 Jul 2014 11:46:06 +0200 Date: Tue, 29 Jul 2014 11:46:06 +0200 From: Goswin von Brederlow To: caml-list@inria.fr Message-ID: <20140729094606.GB13382@frosties> References: <1404501528.4384.4.camel@e130> <20140728111452.GA26816@frosties> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-Provags-ID: V03:K0:juGkwUmtN0A0rHFIkzu1x4IfyPSiioG2+e78yIe4hCFeq/rWSQf PlufnxHBSrwu9Gw2yR7JQvBhQrFhbTx1R9VzBWdixNfUaH0d8k2h+aYrpJtWwcDN8pEBTnR jBcbN7mkPtWXuiGBLcEkzlN600BPgpOvaL7Gf8DaUmG/Y8LG7Rqf9pkQQRzvlqnmml9EEfb x4kuMJ6G8oZ3i3M9N/+cw== Subject: Re: [Caml-list] Immutable strings On Mon, Jul 28, 2014 at 10:54:36PM -0400, Yaron Minsky wrote: > This isn't my idea, but it seems worth repeating: perhaps it would > make sense to have an unmovable byte-array type that had the same > memory representation as Bytes.t, with the extra guarantee that the > collector wouldn't move it. > > You could imagine representing this as a private type: > > module Immovable_bytes : sig > type t = private Bytes.t > val create : int -> t > end > > with a special creation function for creating these immovable strings. > This would avoid some of the current need to write what is effectively > the same code twice, once for bigarrays and once for Bytes.t's. In > particular, you could modify an Immovable_bytes by first up-casting it > to a Bytes.t. But you could only actually create one by going through > the special creation function. Include Bytes and then redefine all the functions creating new Bytes to use the special create. That way you can use all the Bytes functions without upcasting. > I'm not sure if the runtime details could be made to work out, but if > they could, I think it would be a bit nicer than the current world. > > y I think that is simple enough to do. Simply have your create call malloc and set the right header for the block and return the address + header_size. Also add a Gc.finalise to free the memory when the block becomes unreachable. Or can you only finalise blocks inside the ocaml heap? A slight problem though is that sometimes alignment is important (e.g linux AIO needs block aligned data). You would have to allocate the memory to be 4/8 bytes shy of block alignment, which means allocating a chunk one block bigger and aligning the data within that larger block. It also means you need to store the real address of the block before or after the data and complicates your free function. For page aligned data of page size that means you need to allocate (2 * PAGE_SIZE + 16) bytes of data whereas a bigarray only needs PAGE_SIZE + the ocaml block on the major heap. In general it could be nice to include `Immovable as phantom type next to `Const, `Read and `Write. Then you could have immovable strings, bytes, ... MfG Goswin