* OCaml and Boehm
@ 2009-04-10 20:13 Lukasz Stafiniak
2009-04-11 9:46 ` [Caml-list] " Basile STARYNKEVITCH
2009-04-13 17:36 ` Xavier Leroy
0 siblings, 2 replies; 18+ messages in thread
From: Lukasz Stafiniak @ 2009-04-10 20:13 UTC (permalink / raw)
To: Caml
Hi,
Is the OCaml runtime Boehm-safe? That is, can it be run with Boehm
turned on and traversing OCaml's heap? (So that the OCaml heap can
provide roots to Boehm.) And if not, could it be patched to make it
Boehm-safe in this sense?
Thanks,
Łukasz
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [Caml-list] OCaml and Boehm
2009-04-10 20:13 OCaml and Boehm Lukasz Stafiniak
@ 2009-04-11 9:46 ` Basile STARYNKEVITCH
2009-04-11 10:42 ` Jon Harrop
` (2 more replies)
2009-04-13 17:36 ` Xavier Leroy
1 sibling, 3 replies; 18+ messages in thread
From: Basile STARYNKEVITCH @ 2009-04-11 9:46 UTC (permalink / raw)
To: Lukasz Stafiniak; +Cc: Caml
Lukasz Stafiniak wrote:
> Hi,
>
> Is the OCaml runtime Boehm-safe? That is, can it be run with Boehm
> turned on and traversing OCaml's heap? (So that the OCaml heap can
> provide roots to Boehm.) And if not, could it be patched to make it
> Boehm-safe in this sense?
>
>
Probably not. Because I am not sure of what you mean by Boehm-safe,
since Boehm's GC is conservative and do not make much promises.
And very probably, Ocaml runtime cannot realistically be make Boehm-safe
or even Boehm compatible.
There is a reason for all this. The major strength of Ocaml runtime is
its robustness and its efficiency. The efficiency of Ocaml GC is of
paramount importance (it supports very high allocation rate, which is
out of reach for the Boehm collector; and big allocation rate is typical
of most functional programs; the typical allocation speed within the
Ocaml runtime is at least one or two order of magnitudes faster than
that of GC_malloc; a typical GC_malloc is by itself a bit slower than a
libc malloc.).
However, it could happen that you might run some Boehm GC code inside
Ocaml if you are lucky. You should be sure that no pointer go from
GC-Boehm zone into Ocaml zone.
But your question is really too vague. What is the Boehm-GC based code
you want to run within an Ocaml application, and what is that application?
Regards.
--
Basile STARYNKEVITCH http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mines, sont seulement les miennes} ***
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [Caml-list] OCaml and Boehm
2009-04-11 9:46 ` [Caml-list] " Basile STARYNKEVITCH
@ 2009-04-11 10:42 ` Jon Harrop
[not found] ` <4a708d20904110511o7d390807r3d29400cf96d6f35@mail.gmail.com>
2009-04-12 3:34 ` Goswin von Brederlow
2 siblings, 0 replies; 18+ messages in thread
From: Jon Harrop @ 2009-04-11 10:42 UTC (permalink / raw)
To: caml-list
On Saturday 11 April 2009 10:46:13 Basile STARYNKEVITCH wrote:
> Lukasz Stafiniak wrote:
> > Hi,
> >
> > Is the OCaml runtime Boehm-safe? That is, can it be run with Boehm
> > turned on and traversing OCaml's heap? (So that the OCaml heap can
> > provide roots to Boehm.) And if not, could it be patched to make it
> > Boehm-safe in this sense?
>
> Probably not. Because I am not sure of what you mean by Boehm-safe,
> since Boehm's GC is conservative and do not make much promises.
Boehm's GC breaks on various code, most notably code that "hides" pointers to
reachable data in an indirect form. The most famous example that breaks Boehm
is the (admittedly awful and technically illegal) pointer bump trick used in
Numerical Recipies to bring the wonderous starting-at-one indexes of Fortran
to the C world. Boehm fails to recognise the bumped pointers as references
into reachable data and goes right ahead and deallocates all of your
numerical arrays for you.
> There is a reason for all this. The major strength of Ocaml runtime is
> its robustness and its efficiency. The efficiency of Ocaml GC is of
> paramount importance (it supports very high allocation rate, which is
> out of reach for the Boehm collector; and big allocation rate is typical
> of most functional programs; the typical allocation speed within the
> Ocaml runtime is at least one or two order of magnitudes faster than
> that of GC_malloc; a typical GC_malloc is by itself a bit slower than a
> libc malloc.).
Regarding OCaml vs libc malloc, I had always assumed that but HLVM appears to
have proven otherwise. It uses malloc and free directly with no optimization
whatsoever yet it is now well within 2x the performance of OCaml on the
allocation and GC intensive 10-queens benchmark.
Boehm's allocator is asymptotically slower than malloc, of course.
--
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e
^ permalink raw reply [flat|nested] 18+ messages in thread
[parent not found: <4a708d20904110511o7d390807r3d29400cf96d6f35@mail.gmail.com>]
* Re: [Caml-list] OCaml and Boehm
2009-04-11 9:46 ` [Caml-list] " Basile STARYNKEVITCH
2009-04-11 10:42 ` Jon Harrop
[not found] ` <4a708d20904110511o7d390807r3d29400cf96d6f35@mail.gmail.com>
@ 2009-04-12 3:34 ` Goswin von Brederlow
2009-04-12 12:09 ` Lukasz Stafiniak
2 siblings, 1 reply; 18+ messages in thread
From: Goswin von Brederlow @ 2009-04-12 3:34 UTC (permalink / raw)
To: Basile STARYNKEVITCH; +Cc: Lukasz Stafiniak, Caml
Basile STARYNKEVITCH <basile@starynkevitch.net> writes:
> Lukasz Stafiniak wrote:
>> Hi,
>>
>> Is the OCaml runtime Boehm-safe? That is, can it be run with Boehm
>> turned on and traversing OCaml's heap? (So that the OCaml heap can
>> provide roots to Boehm.) And if not, could it be patched to make it
>> Boehm-safe in this sense?
>>
>>
> Probably not. Because I am not sure of what you mean by Boehm-safe,
> since Boehm's GC is conservative and do not make much promises.
It makes the promise that a block that is pointed too is never freed.
(at least for some form of pointed too)
> And very probably, Ocaml runtime cannot realistically be make
> Boehm-safe or even Boehm compatible.
Correct me if I'm wrong but aren't pointers in ocaml always pointing
inside allocated memory instead of its begining? Each block has a
header and the ocaml value points to after the header. So every ocaml
value would be 4/8 bytes offset to the real address of a block.
Can Boehm cope with that at all?
> However, it could happen that you might run some Boehm GC code inside
> Ocaml if you are lucky. You should be sure that no pointer go from
> GC-Boehm zone into Ocaml zone.
And probably the other way around too given the above. But you need
bindings for any non ocaml code you call so you already have stub
functions between ocaml and boehm using code. Any values that pass
between the two you can register with each GC. But I fear Boehm has no
finalize_* equivalent which you would need to tell the ocaml GC a
value as been droped when the Boehm GC frees something.
So I would guess you can't pass ocaml pointers into Boehm GC at all
without manually handling the resource.
> But your question is really too vague. What is the Boehm-GC based code
> you want to run within an Ocaml application, and what is that
> application?
>
> Regards.
MfG
Goswin
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [Caml-list] OCaml and Boehm
2009-04-12 3:34 ` Goswin von Brederlow
@ 2009-04-12 12:09 ` Lukasz Stafiniak
0 siblings, 0 replies; 18+ messages in thread
From: Lukasz Stafiniak @ 2009-04-12 12:09 UTC (permalink / raw)
To: Goswin von Brederlow; +Cc: Basile STARYNKEVITCH, Caml
On Sun, Apr 12, 2009 at 5:34 AM, Goswin von Brederlow <goswin-v-b@web.de> wrote:
>
> Correct me if I'm wrong but aren't pointers in ocaml always pointing
> inside allocated memory instead of its begining? Each block has a
> header and the ocaml value points to after the header. So every ocaml
> value would be 4/8 bytes offset to the real address of a block.
>
> Can Boehm cope with that at all?
Boehm is only supposed to collect the C(++)land values. One can
register the OCaml-boxed pointers that refer to these values as
Boehm-GC roots, and deregister them (?) in the OCaml-finalizers for
the pointers. I don't see anything scary with this approach, provided
(?) is possible. Is the performance penalty as big as with smart
pointers?
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [Caml-list] OCaml and Boehm
2009-04-10 20:13 OCaml and Boehm Lukasz Stafiniak
2009-04-11 9:46 ` [Caml-list] " Basile STARYNKEVITCH
@ 2009-04-13 17:36 ` Xavier Leroy
1 sibling, 0 replies; 18+ messages in thread
From: Xavier Leroy @ 2009-04-13 17:36 UTC (permalink / raw)
To: Lukasz Stafiniak; +Cc: Caml
> Is the OCaml runtime Boehm-safe? That is, can it be run with Boehm
> turned on and traversing OCaml's heap? (So that the OCaml heap can
> provide roots to Boehm.)
I conjecture the answer is "yes", although it's hard to tell for sure
without a precise specification of what is/is not OK with the
Boehm-Demers-Weiser collector.
>From the standpoint of this collector, OCaml's heap is just a set of
large-ish blocks allocated with malloc() (*) and containing a zillion
pointers within those blocks. OCaml doesn't play any dirty tricks
with pointers: no xoring of two pointers, no pointers represented as
offsets from a base, no pointers one below or one above a malloc-ed
block. Most pointers are word-aligned but we sometimes play tricks
with the low 2 bits.
Of course, almost all Caml pointers point inside those malloc-ed
blocks, not to the beginning, but I'm confident that the B-D-W collector
can handle this, otherwise it would fail on pretty much any existing C
code.
This said, I agree with Basile that what you're trying to achieve
(coexistence between several GCs) is risky, and that a design based on
message passing and separated memory spaces would be more robust, if
feasible.
- Xavier Leroy
(*) In 3.10 and earlier releases, OCaml sometimes used mmap() instead
of malloc() to obtain these blocks. Starting from 3.11, malloc() is
the only interface OCaml uses to obtain memory from the OS.
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [Caml-list] OCaml and Boehm
@ 2009-04-11 19:17 Ed Keith
2009-04-11 20:36 ` Jon Harrop
0 siblings, 1 reply; 18+ messages in thread
From: Ed Keith @ 2009-04-11 19:17 UTC (permalink / raw)
To: caml-list
--- On Sat, 4/11/09, Jon Harrop <jon@ffconsultancy.com> wrote:
> From: Jon Harrop <jon@ffconsultancy.com>
> Subject: Re: [Caml-list] OCaml and Boehm
> To: caml-list@yquem.inria.fr
> Date: Saturday, April 11, 2009, 10:27 AM
>
> Also, don't forget that many people incorrectly claim that smart pointers
> deallocate at the earliest possible point when, in fact, they typically keep
> values alive longer than necessary.
Could elaborate on this? I'm having a hard time envisioning a situation where GC could free memory that smart pointers would not free.
-EdK
Ed Keith
e_d_k@yahoo.com
Blog: edkeith.blogspot.com
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [Caml-list] OCaml and Boehm
2009-04-11 19:17 Ed Keith
@ 2009-04-11 20:36 ` Jon Harrop
2009-04-12 3:25 ` Goswin von Brederlow
0 siblings, 1 reply; 18+ messages in thread
From: Jon Harrop @ 2009-04-11 20:36 UTC (permalink / raw)
To: caml-list
On Saturday 11 April 2009 20:17:58 Ed Keith wrote:
> --- On Sat, 4/11/09, Jon Harrop <jon@ffconsultancy.com> wrote:
> > From: Jon Harrop <jon@ffconsultancy.com>
> > Subject: Re: [Caml-list] OCaml and Boehm
> > To: caml-list@yquem.inria.fr
> > Date: Saturday, April 11, 2009, 10:27 AM
> >
> > Also, don't forget that many people incorrectly claim that smart pointers
> > deallocate at the earliest possible point when, in fact, they typically
> > keep values alive longer than necessary.
>
> Could elaborate on this? I'm having a hard time envisioning a situation
> where GC could free memory that smart pointers would not free.
Smart pointers deallocate when values fall out of scope. GC deallocates when
it runs and values are unreachable.
Consider:
let () =
let x = ..
f x
g()
The value "x" stays in scope to the end of the block so a smart pointer will
not deallocate it. The GC may well run during "g", realise that "x" is
unreachable and deallocate it.
Note that there are further unwanted side effects of smart pointers here.
Specifically, having to keep "x" around until the end of scope increases
register pressure and makes it more likely to values will be spilled, which
is a substantial performance cost.
--
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [Caml-list] OCaml and Boehm
2009-04-11 20:36 ` Jon Harrop
@ 2009-04-12 3:25 ` Goswin von Brederlow
0 siblings, 0 replies; 18+ messages in thread
From: Goswin von Brederlow @ 2009-04-12 3:25 UTC (permalink / raw)
To: Jon Harrop; +Cc: caml-list
Jon Harrop <jon@ffconsultancy.com> writes:
> On Saturday 11 April 2009 20:17:58 Ed Keith wrote:
>> --- On Sat, 4/11/09, Jon Harrop <jon@ffconsultancy.com> wrote:
>> > From: Jon Harrop <jon@ffconsultancy.com>
>> > Subject: Re: [Caml-list] OCaml and Boehm
>> > To: caml-list@yquem.inria.fr
>> > Date: Saturday, April 11, 2009, 10:27 AM
>> >
>> > Also, don't forget that many people incorrectly claim that smart pointers
>> > deallocate at the earliest possible point when, in fact, they typically
>> > keep values alive longer than necessary.
>>
>> Could elaborate on this? I'm having a hard time envisioning a situation
>> where GC could free memory that smart pointers would not free.
>
> Smart pointers deallocate when values fall out of scope. GC deallocates when
> it runs and values are unreachable.
>
> Consider:
>
> let () =
> let x = ..
> f x
> g()
>
> The value "x" stays in scope to the end of the block so a smart pointer will
> not deallocate it. The GC may well run during "g", realise that "x" is
> unreachable and deallocate it.
>
> Note that there are further unwanted side effects of smart pointers here.
> Specifically, having to keep "x" around until the end of scope increases
> register pressure and makes it more likely to values will be spilled, which
> is a substantial performance cost.
How about cyclic records:
type r = { next : r }
let rec r1 { next = r2 } and r2 = { next = r1 }
Smart pointers will never free that since r1 kees r2 alive and r2
keeps r1 alive.
MfG
Goswin
^ permalink raw reply [flat|nested] 18+ messages in thread
end of thread, other threads:[~2009-04-14 5:25 UTC | newest]
Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-10 20:13 OCaml and Boehm Lukasz Stafiniak
2009-04-11 9:46 ` [Caml-list] " Basile STARYNKEVITCH
2009-04-11 10:42 ` Jon Harrop
[not found] ` <4a708d20904110511o7d390807r3d29400cf96d6f35@mail.gmail.com>
[not found] ` <49E09C2D.4080906@starynkevitch.net>
2009-04-11 14:11 ` Lukasz Stafiniak
2009-04-11 14:27 ` Jon Harrop
2009-04-11 14:40 ` Lukasz Stafiniak
2009-04-11 20:40 ` Jon Harrop
2009-04-11 15:03 ` Basile STARYNKEVITCH
2009-04-11 20:41 ` Jon Harrop
2009-04-13 9:42 ` Christoph Bauer
2009-04-13 13:15 ` Lukasz Stafiniak
2009-04-14 5:25 ` Goswin von Brederlow
2009-04-12 3:34 ` Goswin von Brederlow
2009-04-12 12:09 ` Lukasz Stafiniak
2009-04-13 17:36 ` Xavier Leroy
2009-04-11 19:17 Ed Keith
2009-04-11 20:36 ` Jon Harrop
2009-04-12 3:25 ` Goswin von Brederlow
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox