{"action":"created","issue":{"url":"https://api.github.com/repos/ocaml/ocaml/issues/606","repository_url":"https://api.github.com/repos/ocaml/ocaml","labels_url":"https://api.github.com/repos/ocaml/ocaml/issues/606/labels{/name}","comments_url":"https://api.github.com/repos/ocaml/ocaml/issues/606/comments","events_url":"https://api.github.com/repos/ocaml/ocaml/issues/606/events","html_url":"https://github.com/ocaml/ocaml/pull/606","id":158906801,"number":606,"title":"Unboxed types","user":{"login":"damiendoligez","id":19104,"avatar_url":"https://avatars.githubusercontent.com/u/19104?v=3","gravatar_id":"","url":"https://api.github.com/users/damiendoligez","html_url":"https://github.com/damiendoligez","followers_url":"https://api.github.com/users/damiendoligez/followers","following_url":"https://api.github.com/users/damiendoligez/following{/other_user}","gists_url":"https://api.github.com/users/damiendoligez/gists{/gist_id}","starred_url":"https://api.github.com/users/damiendoligez/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/damiendoligez/subscriptions","organizations_url":"https://api.github.com/users/damiendoligez/orgs","repos_url":"https://api.github.com/users/damiendoligez/repos","events_url":"https://api.github.com/users/damiendoligez/events{/privacy}","received_events_url":"https://api.github.com/users/damiendoligez/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/ocaml/ocaml/labels/caml-weekly-news","name":"caml-weekly-news","color":"c7def8"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":42,"created_at":"2016-06-07T12:08:37Z","updated_at":"2016-07-12T22:53:33Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/ocaml/ocaml/pulls/606","html_url":"https://github.com/ocaml/ocaml/pull/606","diff_url":"https://github.com/ocaml/ocaml/pull/606.diff","patch_url":"https://github.com/ocaml/ocaml/pull/606.patch"},"body":"This PR introduces an annotation and an optimization for concrete types that have only one constructor with one argument, and immutable records with only one field. In both cases, the default representation is a memory block with header and one field. This memory block represents no useful information and we can get rid of it.\r\nExample:\r\n```\r\ntype t = A of string\r\nlet x = A \"toto\"\r\nmatch x with A s -> s\r\n```\r\nIn this case, the pattern-matching does not even read the header of the block that represents `A s` because it contains no useful information. With this patch, if we add a `[@@unboxed]` annotation on the type definition, the compiler will suppress the indirection block and represent the value directly as the string:\r\n```\r\ntype t = A of string [@@unboxed]\r\nlet x = A \"toto\"\r\nassert (Obj.repr x == Obj.repr (match x with A s -> s))\r\n```\r\n\r\nThis is useful (for example):\r\n- when defining type aliases that we don't want to mix up\r\n- when introducing polymorphic values by defining a record type with a single polymorphic field\r\n- when using a single-constructor, single-field GADT to introduce an existential type\r\n\r\nSome questions\r\n============\r\n- About the name: I called these \"unboxed types\". Is there any objection and/or better idea?\r\n- About the annotation: `unboxed` is already used for a different (but related) purpose. Is it a good idea to reuse it?\r\n- About the annotation: currently you have to activate this optimization with an annotation. Should it be the default instead? See below for compatibility considerations.\r\n- About annotations: there is a trap in the current version: if you add a `[@unboxed]` annotation on the constructor or on the record field, it is ignored. Should it trigger a warning or error, or even just trigger the optimization anyway? (In fact, the same problem already exists with `[@immediate]`).\r\n\r\nFuture work\r\n=========\r\nSome more work is needed to make it work nicely with the float array and float record optimizations: currently, if you write:\r\n```\r\ntype t = A of {f : float} [@@unboxed]\r\ntype r = {f1 : t; f2 : t; f3: float}\r\ntype s = {f4 : float; f5 : float; f6: float}\r\n```\r\nthen a value of type `t` is represented as a float, but a value of type `r` is not optimized into a float array, unlike type `s`.\r\n\r\nA related problem is the optimization of array access for `t array` and the interference with the `[@@immediate]` annotation (`type t = A of int [@@unboxed] [@@immediate]` currently fails).\r\n\r\nCompatibility\r\n==========\r\nIf activated by default, this optimization will break the FFI because it changes the representation of values. It will also break the compatibility with old marshalled values.\r\n\r\nA more subtle incompatibility is with `let rec`:\r\n```\r\ntype t = A of t [@@unboxed]\r\nlet rec x = A x\r\n```\r\nThis must be rejected because it is (compiled as) the same as `let rec x = x`. The current compiler rejects it without any need for a patch but #556 will be made slightly more complex by this PR.\r\n"},"comment":{"url":"https://api.github.com/repos/ocaml/ocaml/issues/comments/232206211","html_url":"https://github.com/ocaml/ocaml/pull/606#issuecomment-232206211","issue_url":"https://api.github.com/repos/ocaml/ocaml/issues/606","id":232206211,"user":{"login":"bobzhang","id":747051,"avatar_url":"https://avatars.githubusercontent.com/u/747051?v=3","gravatar_id":"","url":"https://api.github.com/users/bobzhang","html_url":"https://github.com/bobzhang","followers_url":"https://api.github.com/users/bobzhang/followers","following_url":"https://api.github.com/users/bobzhang/following{/other_user}","gists_url":"https://api.github.com/users/bobzhang/gists{/gist_id}","starred_url":"https://api.github.com/users/bobzhang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bobzhang/subscriptions","organizations_url":"https://api.github.com/users/bobzhang/orgs","repos_url":"https://api.github.com/users/bobzhang/repos","events_url":"https://api.github.com/users/bobzhang/events{/privacy}","received_events_url":"https://api.github.com/users/bobzhang/received_events","type":"User","site_admin":false},"created_at":"2016-07-12T22:53:33Z","updated_at":"2016-07-12T22:53:33Z","body":"this looks nice, a simple question, will this get unboxed?\r\n```ocaml\r\ntype ('a,'b) t = A of 'a -> 'b \r\n```"}}
{"actor":{"display_login":"bobzhang"}}