From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Accessing record fields
Date: Mon, 16 Sep 2013 09:37:39 +0200 [thread overview]
Message-ID: <20130916073739.GA9309@frosties> (raw)
In-Reply-To: <52358E3A.7030802@ens.fr>
On Sun, Sep 15, 2013 at 12:38:50PM +0200, Jacques-Henri Jourdan wrote:
> I think both are very close.
>
> I would say that f is faster, because r.a is loaded only once. However,
> this can increase register pressure...
>
> I would say to use whichever is more easy to use a a given context, and
> to tune specifically the code if this is the bottleneck: this is very
> unlikely to be the performance bottleneck, and there won't be a big
> performance improvement anyway...
>
>
> --
> JH
>
> Le 15/09/2013 12:30, José Romildo Malaquias a écrit :
> > Hello.
> >
> > OCaml offers at least two ways of accessing a record field: using the
> > dot notation, and doing pattern matching.
> >
> > Does one of them deliver better performance than the other?
> >
> > This may be relevant when a field is accessed multiple times.
> >
> > For instance:
> >
> > type trec = { a : int; mutable b: int }
> >
> > let f {a;b} = a * a + b
> >
> > let g r = r.a * r.a + r.b
> >
> > Which one would be preferred in this case: f or g?
> >
> > Romildo
Why not look at the assembly code they generate?
(here OCaml version 4.00.1 on amd64)
0000000000404030 <camlBla__test_f_1011>:
404030: 48 89 c3 mov %rax,%rbx
404033: 48 8b 03 mov (%rbx),%rax
404036: 48 8b 7b 08 mov 0x8(%rbx),%rdi
40403a: 48 89 c3 mov %rax,%rbx
40403d: 48 d1 fb sar %rbx
404040: 48 ff c8 dec %rax
404043: 48 0f af c3 imul %rbx,%rax
404047: 48 01 f8 add %rdi,%rax
40404a: c3 retq
40404b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)
0000000000404050 <camlBla__test_g_1014>:
404050: 48 8b 78 08 mov 0x8(%rax),%rdi
404054: 48 8b 18 mov (%rax),%rbx
404057: 48 d1 fb sar %rbx
40405a: 48 8b 00 mov (%rax),%rax
40405d: 48 ff c8 dec %rax
404060: 48 0f af c3 imul %rbx,%rax
404064: 48 01 f8 add %rdi,%rax
404067: c3 retq
404068: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1)
40406f: 00
Other than reordering there are 2 differences:
1) In "f" the record is first copied to %rbx, then deconstructed while
in "g" it is deconstructed directly from %rax saving one instruction.
2) In "f" r.a is extracted once and then copied while in "g" it is
extracted twice. Not sure how much faster a "mov %rax,%rbx"
is over a "mov (%rax),%rax" if at all.
MfG
Goswin
next prev parent reply other threads:[~2013-09-16 7:37 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-09-15 10:30 José Romildo Malaquias
2013-09-15 10:38 ` Jacques-Henri Jourdan
2013-09-16 7:37 ` Goswin von Brederlow [this message]
2013-09-16 7:52 ` David MENTRE
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=20130916073739.GA9309@frosties \
--to=goswin-v-b@web.de \
--cc=caml-list@inria.fr \
/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