Re: Tips on coding for performance
John Pallister <john@synchromesh.com> writes:
> I'm new to Lisp/LispWorks optimisation, and I'm looking for
> a few pointers to get me started. Which of the following
> should I be looking at? (I haven't tried the profiler yet,
> but I will do so; I just wanted to get this email off first.)
I'll chime in too... I think you need to use the profiler asap. I've
almost always been surprised when using the profiler on a slow
application, e.g. I /knew/ I had a frequently used function with a
stupid, slow implementation, and then the profiler shows me that it
accounts for just 0.1% of the cpu usage.
> * Methods vs. functions: I have used a lot of classes &
> methods; should I use ordinary functions instead of generic
> functions when there's only one method? Or indeed structs
> instead of classes?
Don't do anything with this before you know you have to
(I don't think you'll end up changing it).
> * Type declarations: none yet. Are these best near FLI
> wrappers and/or in tight loops, or should I just go through
> the whole thing and add them everywhere? How much does the
> compiler infer itself?
Not everywhere! Only do this where it really makes sense (e.g. I have
a server application that has to compute 5 million correlations, each
based on 2x500-element vectors, within reasonable time, the
correlation and standard deviation functions are heavily optimised
with floating-point declarations, but the bulk of the application has
no declarations).
> * Pooling resources & memory: is foreign memory much more
> expensive to allocate & GC than the Lisp heap? Are the MP
> locks cheap to get & release, or should I have thread-safe &
> -unsafe pools?
I don't know how you're going to use the locks, but my experience
is that they are really cheap to get & release (on linux, both
with and without pthreads (lw 5 and 4)) (which version and OS
are you using?)
Other things to look out for:
- Look for excessive consing, and consider if you would be better off
using vectors instead of lists
- Avoid medium-lived long lists, especially FIFO queues implemented as
lists (LW has pushed me on this for a while, finally I'm replacing
them with vectors :-)) - they're expensive to handle for the GC.
- Watch out for slow iteration (maphash/loop) on big hash tables! They get
locked, and your application may stall completely!
--
(espen)