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 5E1D27EE49 for ; Mon, 16 Sep 2013 09:37:41 +0200 (CEST) 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.15.14; 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.15.14; 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.15.14; 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: As4BAEi0NlLU4w8Om2dsb2JhbABav06CQoJ2gRgWDgEBAQEBBgsLCRQogiUBAQUyAVYLGAklDwUoiCQBFrAmH4lejiCBWhaDCIEAA5d6hh2OfoFv X-IPAS-Result: As4BAEi0NlLU4w8Om2dsb2JhbABav06CQoJ2gRgWDgEBAQEBBgsLCRQogiUBAQUyAVYLGAklDwUoiCQBFrAmH4lejiCBWhaDCIEAA5d6hh2OfoFv X-IronPort-AV: E=Sophos;i="4.90,913,1371074400"; d="scan'208";a="33001555" Received: from mout.web.de ([212.227.15.14]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES128-SHA; 16 Sep 2013 09:37:40 +0200 Received: from frosties.localnet ([95.208.119.3]) by smtp.web.de (mrweb102) with ESMTPSA (Nemesis) id 0MZUWH-1VeWCH1biP-00LGt8 for ; Mon, 16 Sep 2013 09:37:40 +0200 Received: from mrvn by frosties.localnet with local (Exim 4.80) (envelope-from ) id 1VLTNL-0002hY-P5 for caml-list@inria.fr; Mon, 16 Sep 2013 09:37:39 +0200 Date: Mon, 16 Sep 2013 09:37:39 +0200 From: Goswin von Brederlow To: caml-list@inria.fr Message-ID: <20130916073739.GA9309@frosties> References: <20130915103027.GA21344@jrm> <52358E3A.7030802@ens.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <52358E3A.7030802@ens.fr> User-Agent: Mutt/1.5.21 (2010-09-15) X-Provags-ID: V03:K0:WsJCxyiPIWb/ibGf50ztB6gL+dBtSGoH5PkKw4OtuHDggo1uqau 4FLBnVupoyZrAcHqX++TSdQLne7rmMQutuSG/az7hgkm3PW2M/vNZLVeCwsKH8tY+DvhUMd geIsC+PeAavlUzGbzo6eyrRLs43x8sNwK6ncRU1NfirMy716dbvQyW6LiDEq1FnHsUahC5B YNe722onoxcQjMY1YCvVA== Subject: Re: [Caml-list] Accessing record fields 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 : 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 : 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