caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Prevost <j.prevost@gmail.com>
To: "effbiae@ivorykite.com" <effbiae@ivorykite.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] c is 4 times faster than ocaml?
Date: Wed, 4 Aug 2004 00:59:20 -0400	[thread overview]
Message-ID: <d849ad2a040803215967acd9ed@mail.gmail.com> (raw)
In-Reply-To: <36002.60.246.254.84.1091587147.squirrel@www.ivorykite.com>

Just from a first look, I'd say that there are two likely reasons that
this artificial (and incredibly hard to read) benchmark program
performs poorly:

First, even with -unsafe, bounds checking is performed on BigArray types.

Second, working with single byte values is likely painful here--O'Caml
always works with word-aligned values, so it's going to lose bigtime. 
gcc, on the other hand, knows that the crazy intel instruction set can
handle non-word-aligned values.  Here's the main setting loop from
gcc:

.L84:
	movb	%al, (%eax,%ecx)
	incl	%eax
	cmpl	%edx, %eax
	jl	.L84

And here it is from O'Caml:

.L109:
	movl	%esi, %ecx            ;; grab our index into %ecx
	sarl	$1, %ecx              ;; shift off the tag bit
	movl	20(%eax), %edx        ;; get the array's length into %edx
	cmpl	%ecx, %edx            ;; compare the two
	jbe	.L111                 ;; if the index is too high, punt
	movl	4(%eax), %edi         ;; ? probably figure which byte in word
	movl	%esi, %edx            ;; load the loop value into %edx
	sarl	$1, %edx              ;; shift off the low bit
	movb	%dl, (%edi, %ecx)     ;; shove %edx's byte into the word
	movl	%esi, %ecx            ;; store back into array
	addl	$2, %esi              ;; add 1 to index
	cmpl	%ebx, %ecx            ;; compare to target
	jne	.L109                 ;; not equal?  loop

That jbe .L111 is what happens if a bounds check fails, by the way! 
Anyway, you can see that the bounds check takes a bunch of
instructions.  THe main loop is also a bit more expensive.  One thing
going on is those "sarl" instructions, which are shifting out the tag
bit on the right end of O'Caml integers.  If you were working on
integers instead, I think it might be less painful.  Especially if you
could use int32s held in registers to index into things.

Anyway, the main two things slowing stuff down here are the bounds
check and the fact that O'Caml needs to do so much work turning caml
integers into c integers.

(Just as a note, I accidentally tweaked your file to make the loops
not know the type of their arguments at one point while looking for
this loop--you *always* want exact types known at a deep level for
this kind of thing, as that made ocamlopt use C calls to access the
array.)

Oh--and ignore my old ramblings on mmap stuff.  That code was bad
then, and is worse now.  :)

As for your project, I suspect we could provide better suggestions on
how to optimize if we were looking at real code.  My suspicion is that
you might want to write one or two low-level routines in C, rather
than using Bigarrays for this task.  (Just assuming, though--from the
sound of it you're going to have larger structured data in the mmap'd
areas.)

Good luck!

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2004-08-04  4:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-04  2:39 effbiae
2004-08-04  4:59 ` John Prevost [this message]
2004-08-04  5:05   ` John Prevost
2004-08-04  5:24   ` effbiae
2004-08-04  7:28     ` John Prevost
2004-08-04  8:18       ` [Caml-list] " Jack Andrews
2004-08-04 10:06         ` Mikhail Fedotov
2004-08-04 10:25           ` [Caml-list] " Jack Andrews
2004-08-04 15:38             ` [Caml-list] custom mmap modeled on bigarray Jack Andrews
2004-08-10  5:06               ` Jack Andrews
2004-08-11 14:52                 ` Eric Stokes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d849ad2a040803215967acd9ed@mail.gmail.com \
    --to=j.prevost@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=effbiae@ivorykite.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).