caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Immutable strings
Date: Tue, 29 Jul 2014 11:46:06 +0200	[thread overview]
Message-ID: <20140729094606.GB13382@frosties> (raw)
In-Reply-To: <CACLX4jQx=06KPK14PbMvQDOd8qams1w_kCYKd=6KVkDvJu6HjA@mail.gmail.com>

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

  reply	other threads:[~2014-07-29  9:46 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-04 19:18 Gerd Stolpmann
2014-07-04 20:31 ` Anthony Tavener
2014-07-04 20:38   ` Malcolm Matalka
2014-07-04 23:44   ` Daniel Bünzli
2014-07-05 11:04   ` Gerd Stolpmann
2014-07-16 11:38     ` Damien Doligez
2014-07-04 21:01 ` Markus Mottl
2014-07-05 11:24   ` Gerd Stolpmann
2014-07-08 13:23     ` Jacques Garrigue
2014-07-08 13:37       ` Alain Frisch
2014-07-08 14:04         ` Jacques Garrigue
2014-07-28 11:14   ` Goswin von Brederlow
2014-07-28 15:51     ` Markus Mottl
2014-07-29  2:54       ` Yaron Minsky
2014-07-29  9:46         ` Goswin von Brederlow [this message]
2014-07-29 11:48         ` John F. Carr
2014-07-07 12:42 ` Alain Frisch
2014-07-08 12:24   ` Gerd Stolpmann
2014-07-09 13:54     ` Alain Frisch
2014-07-09 18:04       ` Gerd Stolpmann
2014-07-10  6:41         ` Nicolas Boulay
2014-07-14 17:40       ` Richard W.M. Jones
2014-07-08 18:15 ` mattiasw
2014-07-08 19:24   ` Daniel Bünzli
2014-07-08 19:27     ` Raoul Duke
2014-07-09 14:15   ` Daniel Bünzli
2014-07-14 17:45   ` Richard W.M. Jones
2014-07-21 15:06 ` Alain Frisch
     [not found]   ` <20140722.235104.405798419265248505.Christophe.Troestler@umons.ac.be>
2014-08-29 16:30     ` Damien Doligez

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=20140729094606.GB13382@frosties \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    /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).