caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re:  fancy GC question
@ 2000-12-12 12:18 Damien Doligez
  0 siblings, 0 replies; 5+ messages in thread
From: Damien Doligez @ 2000-12-12 12:18 UTC (permalink / raw)
  To: caml-list

>From: Markus Mottl <mottl@miss.wu-wien.ac.at>

>When I allocate an integer array in OCaml, which is always boxed, both
>the pointers to and the elements are obviously contiguous in memory.

There's no pointer in an integer array.


>  int *ar = (int *) &Field(v_ar, 0);

>But is this really always safe if only C writes to the array? What about
>e.g. heap compactions and other GC-actions? Can other effects mess up the
>fact that the pointers map continuously on a contiguous chunk of memory
>(of integers)?

Heap compaction can move the array and break your code (unless you
make sure to reset your ar variable after each compaction).  Future
versions of the GC may move the array under other circumstances.  And
if your array is small enough and was allocated in the minor heap,
then the minor GC will move it too.

>If yes, this would, of course, require the traditional use of the
>"Field"-macro for every access. Otherwise, one could squeeze out a bit
>more performance in some (probably rare) cases.

Make sure you really need that performance and understand the
maintenance cost before using such tricks.

-- Damien



^ permalink raw reply	[flat|nested] 5+ messages in thread
* Re: fancy GC question
@ 2000-12-14 22:10 Markus Mottl
  0 siblings, 0 replies; 5+ messages in thread
From: Markus Mottl @ 2000-12-14 22:10 UTC (permalink / raw)
  To: OCAML

On Tue, 12 Dec 2000, Damien Doligez wrote:
> >When I allocate an integer array in OCaml, which is always boxed, both
> >the pointers to and the elements are obviously contiguous in memory.
> 
> There's no pointer in an integer array.

Ok, they are actually not boxed but tagged (= no pointer) - sorry for
the confusion.

> Heap compaction can move the array and break your code (unless you
> make sure to reset your ar variable after each compaction).  Future
> versions of the GC may move the array under other circumstances.  And
> if your array is small enough and was allocated in the minor heap,
> then the minor GC will move it too.

So (as Xavier mentioned) I have to make absolutely sure that no allocation
can happen during manipulation of the array in C.

> Make sure you really need that performance and understand the
> maintenance cost before using such tricks.

In fact, this piece of code had been in use in the PCRE since the very
beginning without ever causing problems. It is only now that I discovered
that I should better ask whether this is really safe: there is indeed
no allocation there so I can leave it as is and safe the allocation of
an extra array for C, which would otherwise add quite a bit of overhead
in the C-interface.

Best regards,
Markus Mottl

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl



^ permalink raw reply	[flat|nested] 5+ messages in thread
* fancy GC question
@ 2000-12-12  3:05 Markus Mottl
  2000-12-12 15:38 ` Xavier Leroy
  0 siblings, 1 reply; 5+ messages in thread
From: Markus Mottl @ 2000-12-12  3:05 UTC (permalink / raw)
  To: OCAML

Hello,

I am not sure whether the following "trick" is really safe, but it seems
to work:

When I allocate an integer array in OCaml, which is always boxed, both
the pointers to and the elements are obviously contiguous in memory.
One could exploit this in C-interfaces under the restriction that the
array is never changed by the OCaml-runtime, e.g.:

  int *ar = (int *) &Field(v_ar, 0);

And then one can read/write directly into the integer array without
having to follow an indirection (an intermediate pointer) by treating
"ar" as a normal C array.

But is this really always safe if only C writes to the array? What about
e.g. heap compactions and other GC-actions? Can other effects mess up the
fact that the pointers map continuously on a contiguous chunk of memory
(of integers)?

If yes, this would, of course, require the traditional use of the
"Field"-macro for every access. Otherwise, one could squeeze out a bit
more performance in some (probably rare) cases.

- Markus Mottl

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl



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

end of thread, other threads:[~2000-12-15 12:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-12-12 12:18 fancy GC question Damien Doligez
  -- strict thread matches above, loose matches on Subject: below --
2000-12-14 22:10 Markus Mottl
2000-12-12  3:05 Markus Mottl
2000-12-12 15:38 ` Xavier Leroy
2000-12-13 23:06   ` Markus Mottl

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).