Version française
Home     About     Download     Resources     Contact us    
Browse thread
Question on performance/style issue
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nathaniel Gray <n8gray@g...>
Subject: Re: [Caml-list] Question on performance/style issue
On 2/24/06, Alessandro Baretta <a.baretta@studio.baretta.com> wrote:
> I am very fond of the following "duality" operator.
>
> let (++) x f = f x
>
> I use to write complex computations legibly: instead of
>  > h(g(f(x)))
> I write
>  > x ++ f ++ g ++ h
>
> What is the impact of the this programming style on execution performance?

Looks bad:
================ simpleops.ml =================
let a x = print_string x; x ^ "."
let b x = x ^ x
let c x = "frotz" ^ x

let (++) x f = f x

let test_oper _ =
   " Hello!" ++ a ++ b ++ a ++ b ++ c ++ a ++ b ++ c

let test_normal _ =
   c (b (a (c (b (a (b (a " Hello!")))))))

let _ = test_oper ()
let _ = test_normal ()
============ end simpleops.ml ============

Compiled on a PPC powerbook with:
ocamlopt.opt -S -c simpleops.ml

============= from simpleops.s =============
	.globl	_camlSimpleops__test_oper_66
	.text
	.align	2
_camlSimpleops__test_oper_66:
	mflr	r0
	addi	r1, r1, -32
	stw	r0, 28(r1)
L105:
	addis	r11, 0, ha16(_camlSimpleops+4)
	lwz	r4, lo16(_camlSimpleops+4)(r11)
	addis	r11, 0, ha16(_camlSimpleops)
	lwz	r5, lo16(_camlSimpleops)(r11)
	stw	r4, 0(r1)
	addis	r11, 0, ha16(_camlSimpleops)
	lwz	r4, lo16(_camlSimpleops)(r11)
	addis	r11, 0, ha16(_camlSimpleops+4)
	lwz	r6, lo16(_camlSimpleops+4)(r11)
	lwz	r27, 0(r4)
	addis	r11, 0, ha16(_camlSimpleops+8)
	lwz	r7, lo16(_camlSimpleops+8)(r11)
	addis	r11, 0, ha16(_camlSimpleops)
	lwz	r8, lo16(_camlSimpleops)(r11)
	addis	r11, 0, ha16(_camlSimpleops+4)
	lwz	r9, lo16(_camlSimpleops+4)(r11)
	addis	r11, 0, ha16(_camlSimpleops+8)
	lwz	r10, lo16(_camlSimpleops+8)(r11)
	stw	r9, 20(r1)
	stw	r10, 24(r1)
	stw	r8, 16(r1)
	stw	r7, 12(r1)
	stw	r6, 8(r1)
	stw	r5, 4(r1)
	addis	r3, 0, ha16(_camlSimpleops__8)
	addi	r3, r3, lo16(_camlSimpleops__8)
	mtctr	r27
L106:	bctrl
	lwz	r4, 0(r1)
	lwz	r25, 0(r4)
	mtctr	r25
L107:	bctrl
	lwz	r4, 4(r1)
	lwz	r23, 0(r4)
	mtctr	r23
L108:	bctrl
	lwz	r4, 8(r1)
	lwz	r21, 0(r4)
	mtctr	r21
L109:	bctrl
	lwz	r4, 12(r1)
	lwz	r19, 0(r4)
	mtctr	r19
L110:	bctrl
	lwz	r4, 16(r1)
	lwz	r17, 0(r4)
	mtctr	r17
L111:	bctrl
	lwz	r4, 20(r1)
	lwz	r15, 0(r4)
	mtctr	r15
L112:	bctrl
	lwz	r4, 24(r1)
	lwz	r10, 0(r4)
	mtctr	r10
	lwz	r11, 28(r1)
	addi	r1, r1, 32
	mtlr	r11
	bctr
=========================================

Compare that with
=========================================
	.globl	_camlSimpleops__test_normal_67
	.text
	.align	2
_camlSimpleops__test_normal_67:
	mflr	r0
	addi	r1, r1, -16
	stw	r0, 12(r1)
L113:
	addis	r3, 0, ha16(_camlSimpleops__7)
	addi	r3, r3, lo16(_camlSimpleops__7)
L114:	bl	_camlSimpleops__a_57
	mr	r4, r3
L115:	bl	_camlPervasives__$5e_135
L116:	bl	_camlSimpleops__a_57
	mr	r4, r3
L117:	bl	_camlPervasives__$5e_135
L118:	bl	_camlSimpleops__c_61
L119:	bl	_camlSimpleops__a_57
	mr	r4, r3
L120:	bl	_camlPervasives__$5e_135
	lwz	r11, 12(r1)
	addi	r1, r1, 16
	mtlr	r11
	b	_camlSimpleops__c_61
============== end simpleops.s =============

So no, it looks like the call to ++ isn't compiled away, at least in
this case.  I also tried with -inline 99 and it didn't help, but I
don't know if that's even a valid value for inline.

Cheers,
-n8

--
>>>-- Nathaniel Gray -- Caltech Computer Science ------>
>>>-- Mojave Project -- http://mojave.cs.caltech.edu -->