* [Caml-list] Accessing record fields
@ 2013-09-15 10:30 José Romildo Malaquias
2013-09-15 10:38 ` Jacques-Henri Jourdan
2013-09-16 7:52 ` David MENTRE
0 siblings, 2 replies; 4+ messages in thread
From: José Romildo Malaquias @ 2013-09-15 10:30 UTC (permalink / raw)
To: caml-list
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Accessing record fields
2013-09-15 10:30 [Caml-list] Accessing record fields José Romildo Malaquias
@ 2013-09-15 10:38 ` Jacques-Henri Jourdan
2013-09-16 7:37 ` Goswin von Brederlow
2013-09-16 7:52 ` David MENTRE
1 sibling, 1 reply; 4+ messages in thread
From: Jacques-Henri Jourdan @ 2013-09-15 10:38 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 955 bytes --]
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
>
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 555 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Accessing record fields
2013-09-15 10:38 ` Jacques-Henri Jourdan
@ 2013-09-16 7:37 ` Goswin von Brederlow
0 siblings, 0 replies; 4+ messages in thread
From: Goswin von Brederlow @ 2013-09-16 7:37 UTC (permalink / raw)
To: caml-list
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Accessing record fields
2013-09-15 10:30 [Caml-list] Accessing record fields José Romildo Malaquias
2013-09-15 10:38 ` Jacques-Henri Jourdan
@ 2013-09-16 7:52 ` David MENTRE
1 sibling, 0 replies; 4+ messages in thread
From: David MENTRE @ 2013-09-16 7:52 UTC (permalink / raw)
To: José Romildo Malaquias, caml users
Hello,
2013/9/15 José Romildo Malaquias <j.romildo@gmail.com>:
> Which one would be preferred in this case: f or g?
Use the one which is the more readable and maintainable in the long
term, especially for micro-optimization like this.
Best regard,
david
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-09-16 7:53 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-15 10:30 [Caml-list] Accessing record fields José Romildo Malaquias
2013-09-15 10:38 ` Jacques-Henri Jourdan
2013-09-16 7:37 ` Goswin von Brederlow
2013-09-16 7:52 ` David MENTRE
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox