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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 56FC37EC6E for ; Wed, 29 Jan 2014 08:56:25 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of goswin-v-b@web.de) identity=pra; client-ip=212.227.17.11; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="goswin-v-b@web.de"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of goswin-v-b@web.de) identity=mailfrom; client-ip=212.227.17.11; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="goswin-v-b@web.de"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mout.web.de) identity=helo; client-ip=212.227.17.11; receiver=mail2-smtp-roc.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: AqECAM+z6FLU4xELnGdsb2JhbABZu1GFUIEFFg4BAQEBAQYNCQkUKIIlAQEBBCcTPxALGAklDwUoIYgDARjATR+IVBeOJ1gHgySBFASYJ4YyEo8KgWc X-IPAS-Result: AqECAM+z6FLU4xELnGdsb2JhbABZu1GFUIEFFg4BAQEBAQYNCQkUKIIlAQEBBCcTPxALGAklDwUoIYgDARjATR+IVBeOJ1gHgySBFASYJ4YyEo8KgWc X-IronPort-AV: E=Sophos;i="4.95,740,1384297200"; d="scan'208";a="55727074" Received: from mout.web.de ([212.227.17.11]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES128-SHA; 29 Jan 2014 08:56:20 +0100 Received: from frosties.localnet ([37.49.32.119]) by smtp.web.de (mrweb001) with ESMTPSA (Nemesis) id 0LoYFm-1VbpLl1FxS-00gUa6 for ; Wed, 29 Jan 2014 08:56:19 +0100 Received: from mrvn by frosties.localnet with local (Exim 4.80) (envelope-from ) id 1W8Q0Q-0006bv-IK; Wed, 29 Jan 2014 08:56:18 +0100 Date: Wed, 29 Jan 2014 08:56:18 +0100 From: Goswin von Brederlow To: Yotam Barnoy Cc: Ocaml Mailing List Message-ID: <20140129075618.GA25332@frosties> References: <20140120101654.GI26447@frosties> <08bc01cf17b8$9263d070$b72b7150$@ffconsultancy.com> <20140123092925.GB20624@frosties> <01c401cf1891$b1fb1360$15f13a20$@ffconsultancy.com> <026101cf18dd$756c13d0$60443b70$@ffconsultancy.com> <030501cf1925$45380fa0$cfa82ee0$@ffconsultancy.com> <20140127152944.GA29326@frosties> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-Provags-ID: V03:K0:Col+5i/0O7fgsfx2nNWe8v6mz/CKumpOlcCn8HwAmvc7FIIfHxy DHGtvHyQakErRpDEsKVLRNQ4SZ2PHn5F3aEQvTt9y8QcaBNxfLCNX+7r5/UYZ9XW1A5aJfm LPckKW1JBydJJUIozxfRhJKmn/wwzeh/D/Qwj33kh/SD/FVBTwLd6g2pSsxL7mIPQTRShv5 yUslZG9x14ZTvo2XuDqIQ== Subject: Re: [Caml-list] How much optimized is the 'a option type ? On Mon, Jan 27, 2014 at 11:18:23AM -0500, Yotam Barnoy wrote: > > > > But you can't just put a float 42.0 on the heap or even stack when the > > GC might get called. That needs to be boxed in some way to avoid it > > getting misread as pointer. > > > > > It wouldn't be too hard to add a word of metadata to the stack to tell the > GC what's a pointer and what isn't. Haskell does this for function calls > (in fact, if the metadata doesn't fit in a word, it allocates a separate > metadata structure), and F# has this from the .NET runtime which has full > type metadata for everything. > > Problem is that a value is a fixed size and fits in a register. A > > tuple does not. (int * float) even takes the space of 3 values on > > 32bit. You can unbox that in the optimizer for local use but in memory > > and in function calls you need to pass this as box. Otherwise > > polymorphic functions break. > > > > Putting larger structures into an array without boxing also only works > > for immutable objects and by copying them every time they are passed > > around. You can't copy a mutable and you can't pass a pointer to the > > middle of an array to another function or the GC might freak out. > > > > Leaving it to the optimizer is problematic because it might cause a lot of > unneeded boxing and unboxing. > Haskell has the {- #UNPACK -} pragma to unbox types. You have to be really > careful in haskell with this, because you're also changing the evaluation > semantics to be strict. This makes for really ugly optimized haskell code, > but maybe we can do something similar (but not as ugly). F# inherits .NET's > struct types, which are similarly limited, but also useful. Of course, once > you unbox, all parametric polymorphism is lost, but because you have > control over it, you can decide where it's worthwhile. > > An example of unpack usage in haskell: > > data T = T {-# UNPACK #-} !(Int,Int) > > which would be equivalent to something like > type t = (int * int) [@u] > > Note that the whole tuple is unboxed. > > In haskell, you can now do > > f :: T -> Int > f (T(i1,i2)) = i1 + i2 > > and in ocaml you'd do > > let f : t -> int = fun (i1,i2) = i1 + i2 > > Of course, this would require more metadata for the stack. For the heap, > you'd probably want to just use a new tag or the custom tag. For ocaml > you'd also probably have to stipulate that polymorphic marshalling cannot > be performed on this type, and neither can polymorphic comparison -- you'd > have to have specific marshalling/comparison functions, which aren't too > difficult to generate automatically. > > -Yotam That would make it a new type, incompatible with the old one. So a function expecting 'a wouldn't accept 'a [@u] and everything would be fine (but not nice). And then you can define the semantic that types like that are passed and returned in registers if small enough. As for allocation. On the stack you would have one big block for all local variables and put unboxed structures there. You couldn't put the values on the heap. So if you return a unpacked type then it has to be copied (implizit if it is passed in regs) on return or the caller has to pass a pointer to where the result should go as implizit argument. MfG Goswin