Optimizing Common Lisp

(fosskers.ca)

48 points | by todsacerdoti 2 days ago ago

6 comments

  • metroholografix 2 days ago ago

    I see the following in code written by newcomers to CL frequently:

        (optimize (speed 3) (safety 0)) 
    
    but it is a very bad habit to get used to, in general, and may create the impression that it's needed for optimized code. Instead, use this where needed:

        (optimize (speed 3) (safety 1))
    
    which will result in roughly same performance for the vast majority of cases, but keep array bounds checks and (weaker) type checks.

    For SBCL, there's also the excellent sb-simd [1] plus compiler macros and even hooking into some of the VM internals for custom code generation [2].

    [1] https://www.sbcl.org/manual/#sb_002dsimd

    [2] https://pvk.ca/Blog/2014/03/15/sbcl-the-ultimate-assembly-co...

    • colingw a day ago ago

      Thanks for the tip. I double-checked, and while there is no difference between 0 and 1 for SBCL and ABCL, there is a slowdown for ECL.

      I've elaborated here:

      https://www.fosskers.ca/en/blog/optimizing-common-lisp#orgb5...

    • tmtvl 2 days ago ago

      If I remember correctly there are 5 optimization settings:

      1. Speed

      2. Debug

      3. Safety

      4. Size

      5. Compilation speed.

      I don't think those last two have much of an impact, but the first three are the ones you want to adjust to your use case.

  • dom2 2 days ago ago

    Also, after profiling with sb-sprof, calling

      (disassemble '<profiled function>)
    
    Will show the assembly augmented with the fraction of sampled hits on each individual instruction.
  • mark_l_watson 2 days ago ago

    Thanks for writing this up! I have done some profiling in LispWorks Pro, but not SBCL. I will try it out.

    Off topic: I also noticed your nice cooking page for mixing and matching herbs.

    • colingw 2 days ago ago

      A gem from my grandmother's kitchen.