Opened 5 years ago

Last modified 5 years ago

#9944 new bug

Performance issue re: simple loop

Reported by: clinton Owned by:
Priority: low Milestone:
Component: Compiler Version: 7.8.3
Keywords: Cc:
Operating System: Linux Architecture: x86_64 (amd64)
Type of failure: Runtime performance bug Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description (last modified by clinton)

The runtime of the following code actually decreases if the number 2147483647 (231-1) is increased to 2147483648 (231). In addition, adding "module Main where" at the top of the file improves performance to the fast case regardless of what number is picked.

f n = go 1 0 where
  go i c = if i == n then c + i else go (i+1) (c+i)

main = print $ f (2147483647 :: Int) 

I've attached two dumps from ghc-core, core7 is the 2147483647 case and core8 is the 2147483648 case, however the main differences are below:

2147483647 case:

_c3Qg:
	cmpq $2147483647,%r14
	jne _c3Q9
_c3Qa:
	leaq 2147483647(%rsi),%rbx
	jmp *(%rbp)
_c3Q9:
	addq %r14,%rsi
	incq %r14
	jmp _c3Qg

2147483648 case:

_c3Qg:
	movl $2147483648,%eax
	cmpq %rax,%r14
	jne _c3Q9
_c3Qa:
	movl $2147483648,%eax
	movq %rsi,%rbx
	addq %rax,%rbx
	jmp *(%rbp)
_c3Q9:
	addq %r14,%rsi
	incq %r14
	jmp _c3Qg

Despite the extra instructions, the latter approach seems faster for my PC.

Attachments (2)

core7 (5.6 KB) - added by clinton 5 years ago.
2147483647 case
core8 (5.6 KB) - added by clinton 5 years ago.
2147483648 case

Download all attachments as: .zip

Change History (6)

Changed 5 years ago by clinton

Attachment: core7 added

2147483647 case

Changed 5 years ago by clinton

Attachment: core8 added

2147483648 case

comment:1 Changed 5 years ago by clinton

Description: modified (diff)

comment:2 Changed 5 years ago by simonpj

How much faster? How did you measure it? How did you trip over this? (I would never have guessed it would make a measurable difference.)

comment:3 Changed 5 years ago by clinton

Summary: Performance issuePerformance issue re: simple loop

comment:4 Changed 5 years ago by clinton

Description: modified (diff)

Just "time". Difference for me is between 1.8 secs and 1.6 secs. Not a huge difference.

Added some additional information. Also, "module Main where" causes the fast case to be picked.

I tripped over it because I was consistently getting different runtimes from what I thought were two similar programs, I isolated the issue and found it was "module Main where" making the difference. I then found using difference constants made a difference also.

Last edited 5 years ago by clinton (previous) (diff)
Note: See TracTickets for help on using tickets.