List Info

Thread: "ocaml_beginners"::[] performance question




"ocaml_beginners"::[] performance question
country flaguser name
United States
2007-05-16 08:51:04

hello guys,

can the ocaml compiler figure out that this code can be simplified

let id x = x
let a = ref 1
a := id !a

(I've a datastructure that sometimes is used with id function but in
my other application with succ)

peter

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] performance question
country flaguser name
United Kingdom
2007-05-16 10:02:23

On Wed, May 16, 2007 at 03:51:04PM +0200, Peter Halacsy wrote:
> hello guys,
>
> can the ocaml compiler figure out that this code can be simplified
>
> let id x = x
> let a = ref 1
> a := id !a

I suspect that the answer is no.

I had a look at the assembler generated by 'ocamlopt -S' but these new
relocatable x86-64 opcodes are rather hard to follow ... However it
did seem to be calling the id function rather than optimising it away.

Rich.

.quad 2048
.globl camlTest
camlTest:
.space 16
.data
.quad 2295
camlTest__1:
.quad camlTest__id_57
.quad 3
.text
.align 16
.globl camlTest__id_57
camlTest__id_57:
.L100:
ret
.text
.align 16
.globl camlTest__entry
camlTest__entry:
subq $8, %rsp
.L101:
movq $camlTest__1, %rax
movq %rax, camlTest(%rip)
call caml_alloc1
.L102:
leaq 8(%r15), %rax
movq $1024, -8(%rax)
movq $3, (%rax)
movq %rax, camlTest + 8(%rip)
movq camlTest + 8(%rip), %rbx
movq camlTest + 8(%rip), %rax
movq (%rax), %rax
movq %rax, (%rbx)
movq $1, %rax
addq $8, %rsp
ret
.text
.globl camlTest__code_end
camlTest__code_end:
.data
.globl camlTest__data_end
camlTest__data_end:
.long 0
.globl camlTest__frametable
camlTest__frametable:
.quad 1
.quad .L102
.word 16
.word 0
.align 8

--
Richard Jones
Red Hat

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] performance question
country flaguser name
United States
2007-05-16 15:00:54


On May 16, 2007, at 5:02 PM, Richard Jones wrote:

> On Wed, May 16, 2007 at 03:51:04PM +0200, Peter Halacsy wrote:
>> hello guys,
>>
>> can the ocaml compiler figure out that this code can be simplified
>>
>> let id x = x
>> let a = ref 1
>> a := id !a
>
> I suspect that the answer is no.
>
> I had a look at the assembler generated by 'ocamlopt -S' but these new
> relocatable x86-64 opcodes are rather hard to follow ... However it
> did seem to be calling the id function rather than optimising it away.
>

>; Rich.
>

thanks Rhich.

I was asking this because I'm working on data structure of key, value
pairs (simple hash map) It has a function

let update table key intizializer updated =
'a t -> key -> (unit -> 'a) -> ('a -> 'a) -> 'a

My add_or_replace funtcion is

let add_or_replace table key value =
update table key (fun () -> value) (fun old -> value)

the find function uses id

let find table key =
update table key (fun () -> raise Not_found) (fun old -> old)

this is elegant but I don't know how effective (update is twice
faster then find + add)

peter

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] performance question
country flaguser name
United Kingdom
2007-05-16 16:17:58

On Wednesday 16 May 2007 21:00:54 Peter Halacsy wrote:
> I was asking this because I'm working on data structure of key, value
> pairs (simple hash map) It has a function
>
>; let update table key intizializer updated =
> 'a t -> key -> (unit -> 'a) -> ('a -> 'a) -> 'a
>
>
> My add_or_replace funtcion is
>
> let add_or_replace table key value =
> update table key (fun () -> value) (fun old -> value)
>
> the find function uses id
>
> let find table key =
> update table key (fun () -> raise Not_found) (fun old -> old)
>;
> this is elegant but I don't know how effective (update is twice
> faster then find + add)

I suspect it will be fast enough. Running through an identity function will be
very fast compared to many of the other operations that your function
probably has to perform.

If you also like short code, try to order curried arguments such that currying
is as beneficial as possible. In this case, you could have written:

let update init update table key = ...

and then partially applied it:

let find x = update (fun () -> raise Not_found) (fun old -> old) x

and so on.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The F#.NET Journal
http://www.ffconsultancy.com/products/fsharp_journal/?e

__._,_.___
.

__,_._,___
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )