caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: ygrek <ygrekheretix@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] no_scan_tag and int array
Date: Sun, 07 Mar 2010 01:48:51 +0100	[thread overview]
Message-ID: <87eijxot1o.fsf@frosties.localdomain> (raw)
In-Reply-To: <20100306112645.a74bb0c4.ygrekheretix@gmail.com> (ygrek's message of "Sat, 6 Mar 2010 11:26:45 +0200")

ygrek <ygrekheretix@gmail.com> writes:

> Hello,
>
> Consider this code:
>
> open Printf
>
> let measure f =
>   let t = Unix.gettimeofday () in
>   let () = f () in
>   printf "%.4f sec" (Unix.gettimeofday () -. t)
>
> let () =
>   let gc () = for i = 1 to 10 do Gc.full_major () done in
>   let a = Array.make 4_000_000 0 in
>   measure gc;
>   printf " normal %u (%u)\n%!" (Array.length a) (Gc.stat ()).Gc.live_words;
>
>   Obj.set_tag (Obj.repr a) (Obj.no_scan_tag);
>   measure gc;
>   printf " no_scan_tag %u (%u)\n%!" (Array.length a) (Gc.stat ()).Gc.live_words;
>
>   measure gc;
>   printf " no array (%u)\n%!" (Gc.stat ()).Gc.live_words;
>   ()
>
> Output looks like :
>
> 0.2281 sec normal 4000000 (4000165)
> 0.0002 sec no_scan_tag 4000000 (4000165)
> 0.0002 sec no array (164)
>
> So, as expected, setting No_scan_tag on the array of integers prevents GC from uselessly 
> scanning the huge chunk of memory. Looks like polymorphic array functions still work fine and

What polymorphic array function? I don't think Array.length is a good
test as it looks at the header of the array to see how many words the
array has.

> GC correctly reclaims array memory when it is not referenced anymore.
> Apparantly this trick is not allowed for float array as they have a special tag set.

But does the GC scan float arrays?

> The question is - how safe is this? And even more, could the compiler itself set this tag?

>From the GC side this should be perfectly save. But all the array
functions should be checked and not just Array.length before doing this.

And yes, the compiler could mark arrays with primitive types (int, char,
unit,...) and even type x = A | B | C for not scanning. This also
applies to records, tuples and (polymorphic) variant types that contain
no pointer.

It would break though if someone declares an int array and then stores a
pointer in there. But if you do something illegal then you get to keep
the segfaults.

MfG
        Goswin


  reply	other threads:[~2010-03-07  0:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-06  9:26 ygrek
2010-03-07  0:48 ` Goswin von Brederlow [this message]
2010-03-07 14:07   ` [Caml-list] " ygrek
2010-03-29 15:34 ` 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=87eijxot1o.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    --cc=ygrekheretix@gmail.com \
    /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).