From: Yaron Minsky <yminsky@janestreet.com>
To: Gabriel Scherer <gabriel.scherer@gmail.com>
Cc: Nicolas Braud-Santoni <nicolas.braudsantoni@gmail.com>,
"caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] How much optimized is the 'a option type ?
Date: Fri, 17 Jan 2014 17:29:38 -0500 [thread overview]
Message-ID: <CACLX4jTjfmEX2vY0ruS5_RRZMo80MRV-z6Ttvp=Q0vqj8Q7T_g@mail.gmail.com> (raw)
In-Reply-To: <CAPFanBFsP4ggyA7ED46H6aa4ygP=oMC3XYUskHec-ZO9OrkdZQ@mail.gmail.com>
I agree that representation hacking (which is what Obj.magic is all
about) is something that should be done rarely, and I don't think the
Queue example justifies it. There are other hacks, though, that make
sense, like the hack for lazy values, which I think is totally worth
it.
We've had to do a number of Obj.magic hacks in Core_kernel to get it
to perform adequately. I think those hacks are a reasonably good
place to look for inspiration as to how to make OCaml more hospitable
for those with stringent performance requirements.
For what it's worth, GADTs have been a big win in this regard, by
making access to existentials cheap and easy, which can avoid a lot of
pointless allocation of closures in complex libraries. I'd like to
see this improve yet further by allowing for existential values to be
created without an extra allocated block, which the GADT approach
currently requires.
If you're interested, you can look at Core_stack, Flat_queue, Dequeue
and Pool to see some examples where we've needed to use Obj.magic for
performance reasons.
y
On Fri, Jan 17, 2014 at 9:24 AM, Gabriel Scherer
<gabriel.scherer@gmail.com> wrote:
>> The fact that the stdlib
>> needs to use Obj.magic to get the necessary performance is, I think, a
>> sign that something important is missing from the language.
>
> I think this specific example needs to die. Yes, there is an Obj.magic
> in the implementation of Queue, and this mistake was committed more
> than a decade ago. But we could perfectly remove this use of Obj.magic
> and use different implementation techniques to get equally efficient
> (or better) code. But there is no reason to change code that works
> well enough.
>
> That some Obj.magic remains in some old code is *no excuse* to use it
> today or tomorrow.
>
> On Fri, Jan 17, 2014 at 3:02 PM, Yaron Minsky <yminsky@janestreet.com> wrote:
>> I also agree with Gabriel that an option-specific optimization is not
>> clearly the right move.
>>
>> But I wonder if a more general optimization that provided the
>> possibility of minting "fast-path" variants. i.e., one could have an
>> annotation that marked a given branch of a variant as the
>> "no-indirection" one, i.e., the one that doesn't lead to the
>> allocation of an extra block:
>>
>> type ('a,'b) result =
>> | Ok of 'a [@@no_indirection]
>> | Error of 'b
>>
>> would lead to a type where [Ok x == x]. Some cleverness is required
>> then for the representation of the [Error] branch. In particular,
>> you'd need some dynamic test you could run to see if you were using a
>> value that was not the fast-path one.
>>
>> The thing that I don't know if there's a solution for is the nesting
>> problem. i.e., can you effectively distinguish:
>>
>> Ok (Ok (Error x))
>>
>> from
>>
>> Error x
>>
>> since they would have the same physical representation. I'm not sure
>> if some variant of the counting trick used for options would work here
>> or not. But if you could get this, it would make it possible to avoid
>> a large number of dirty Obj.magic hacks that people need to do to
>> build efficient datastructures in practice. The fact that the stdlib
>> needs to use Obj.magic to get the necessary performance is, I think, a
>> sign that something important is missing from the language. I'm not
>> sure if this is quite it, to be clear.
>>
>> y
>>
>>
>> On Fri, Jan 17, 2014 at 8:46 AM, Nicolas Braud-Santoni
>> <nicolas.braudsantoni@gmail.com> wrote:
>>> On 17/01/2014 12:23, Jonathan Kimmitt wrote:
>>>> In my humble opinion the main purpose of Some _ | None is to avoid the
>>>> requirement for a nil pointer in OCaml. If an external function wants to
>>>> return nil in order to indicate, for example that a certain resource is not
>>>> available, it can return None instead and this prevents dereferencing a nil
>>>> pointer in OCaml because the None cannot be dereferenced.
>>> Yes.
>>> This doesn't forbid the compiler from representing 'a option values as
>>> pointers.
>>> Indeed, the type system already enforces that the None case is handled
>>> and the representation of None and Some _ do not matter.
>>>
>>> That said, I agree with Gabriel Scherer : adding optimizations specific
>>> to 'a option might refrain people wanting to switch to more appropriate
>>> datatypes.
>>> However, would is be possible to “optimize away” all types of the form
>>> “type 'a t = X of 'a | A | B | ...” (with at most one non-constant
>>> constructor) ?
>>> Would it be worth doing ?
>>>
>>>
>>> Kind regards,
>>> Nicolas
>>>
>>
>> --
>> Caml-list mailing list. Subscription management and archives:
>> https://sympa.inria.fr/sympa/arc/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs
next prev parent reply other threads:[~2014-01-17 22:29 UTC|newest]
Thread overview: 53+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-01-17 7:35 Damien Guichard
2014-01-17 7:55 ` David House
2014-01-17 8:16 ` Julien Blond
2014-01-17 8:40 ` David House
2014-01-17 9:10 ` Gabriel Scherer
2014-01-17 9:22 ` Simon Cruanes
2014-01-17 17:57 ` Gerd Stolpmann
2014-01-18 1:35 ` Jon Harrop
2014-01-19 6:19 ` oleg
2014-01-21 1:51 ` Francois Berenger
2014-01-18 1:01 ` Jon Harrop
2014-01-24 10:06 ` Alain Frisch
2014-01-24 10:16 ` Alain Frisch
2014-01-24 13:32 ` Yaron Minsky
[not found] ` <CAK=fH+jfi=GsMYBZzmuo=V5UAWimyxiiamY2+DkLg6F0i8XHGw@mail.gmail.com>
2014-01-17 9:11 ` David House
2014-01-17 11:23 ` Jonathan Kimmitt
2014-01-17 13:46 ` Nicolas Braud-Santoni
2014-01-17 13:56 ` Frédéric Bour
2014-01-17 14:02 ` Yaron Minsky
2014-01-17 14:09 ` Simon Cruanes
2014-01-17 22:52 ` Yaron Minsky
2014-01-18 1:37 ` Jon Harrop
2014-01-17 14:24 ` Gabriel Scherer
2014-01-17 22:29 ` Yaron Minsky [this message]
2014-01-18 1:27 ` Jon Harrop
2014-01-18 1:18 ` Jon Harrop
2014-01-20 10:16 ` Goswin von Brederlow
2014-01-20 11:23 ` Jonathan Kimmitt
2014-01-21 2:05 ` Francois Berenger
2014-01-22 21:22 ` Jon Harrop
2014-01-22 21:26 ` Jon Harrop
2014-01-23 9:29 ` Goswin von Brederlow
2014-01-23 23:20 ` Jon Harrop
2014-01-23 23:28 ` Yotam Barnoy
2014-01-24 8:22 ` Jon Harrop
2014-01-24 8:34 ` Andreas Rossberg
2014-01-24 16:56 ` Jon Harrop
2014-01-27 15:29 ` Goswin von Brederlow
2014-01-27 16:18 ` Yotam Barnoy
2014-01-29 7:56 ` Goswin von Brederlow
2014-01-29 8:32 ` Jon Harrop
2014-01-29 16:11 ` Yotam Barnoy
2014-01-30 18:43 ` Yotam Barnoy
2014-02-01 15:58 ` Goswin von Brederlow
2014-01-30 21:31 ` Jon Harrop
2014-01-30 21:43 ` Yotam Barnoy
2014-01-31 8:26 ` Jon Harrop
2014-02-01 15:40 ` Goswin von Brederlow
2014-01-27 10:03 ` Goswin von Brederlow
2014-01-17 14:36 ` Markus Mottl
2014-01-17 15:49 ` Yotam Barnoy
2014-01-17 16:22 ` Markus Mottl
2014-01-20 10:09 ` Goswin von Brederlow
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='CACLX4jTjfmEX2vY0ruS5_RRZMo80MRV-z6Ttvp=Q0vqj8Q7T_g@mail.gmail.com' \
--to=yminsky@janestreet.com \
--cc=caml-list@inria.fr \
--cc=gabriel.scherer@gmail.com \
--cc=nicolas.braudsantoni@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