[DGD] Error trace optimized

Blain blain20 at gmail.com
Sun Dec 14 13:45:50 CET 2014


I wanted to optimize an error trace function for my errord that was also
self-reliant (so as to avoid errors within the runtime_error() function if
my secondary auto barfs)  I ended up saving a crap ton of ticks in the
process using macros to pad/center strings.

My usual trace print function uses my version sprintf, which is isn't
optimized yet, but is apparently very expensive.  I'll spare the reader the
sprintf code and the trace print code for the expensive routine is not much
unlike the optimized version far below.  The macros are what's saving ticks
versus the cost of my sprintf.

=== UNOPTIMIZED RESULT ===

Line --------------------- Program -------------------- ------- Function
------
 549 /kernel/sys/driver
initialize*
 532 /kernel/sys/driver
 _initialize
 629 /kernel/sys/rsrcd
initd*
 400 /kernel/lib/auto (/kernel/sys/rsrcd)
compile_object
 131 /kernel/lib/auto (~System/initd)
 _F_create*
  37 ~System/initd
construct
  80 ~System/initd
install_service
 178 ~System/lib/system (~System/initd)
preload_path
 [ticks: 58867]


=== OPTIMIZED CODE ===
#define PAD(wid) ("                                        "+\
                  "                                       ")[..(wid) - 1]
#define PADL(str,wid) (PAD((wid) - strlen(str)) + (str))
#define PADR(str,wid) ((str) + PAD((wid) - strlen(str)))
#define CENTER(str,wid) (PAD((wid)/2 - strlen(str)/2) + (str) + \
  PAD(((wid) - ((wid)/2) - (strlen(str) - strlen(str)/2))))

#define PADX(x,wid)
(x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+\
x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+\
x+x+x+x+x+x+x+x+x+x+x)[..(wid) - 1] //79 width
#define PADLX(str,wid,pad) (PADX(pad, (wid) - strlen(str)) + (str))
#define PADRX(str,wid,pad) ((str) + PADX(pad, (wid) - strlen(str)))
#define CENTERX(str,wid,pad) (PADX(pad, (wid)/2 - strlen(str)/2) + (str) + \
  PADX(pad, ((wid) - ((wid)/2) - (strlen(str) - strlen(str)/2))))

string get_trace(varargs mixed **trace)
{
  int idx, max;
  string ret;

  if(!trace)
    trace = call_trace();

  ret = "Line "
   + CENTERX(" Program ", 50, "-")
   + " "
   + CENTERX(" Function ", 23, "-")
   + "\n";

  for(idx = 0, max = sizeof(trace); idx < max; idx++)
  {
    mixed *frame;
    string prog, obj, usr, file;

    frame = trace[idx];

    if(frame[TRACE_PROGNAME] == DRIVER
     && frame[TRACE_FUNCTION] == "runtime_error")
      break;

    prog = frame[TRACE_PROGNAME];
    obj = frame[TRACE_OBJNAME];
    if(sscanf(prog, USR_DIR+"/%s/%s", usr, file) > 1)
      prog = "~"+usr+"/"+file;
    if(sscanf(obj, USR_DIR+"/%s/%s", usr, file) >= 1)
      obj = "~"+usr+"/"+file;
    if(obj != prog)
    {
      obj = explode(obj, prog)[0];
      prog += " ("+obj+")";
    }
    ret += PADL(frame[TRACE_LINE], 4)
     + " "
     + PADR(prog, 50)
     + " "
     + PADL(frame[TRACE_FUNCTION], 22)
     + (frame[TRACE_EXTERNAL] ? "*\n" : "\n");
  }
  return ret;
}

=== OPTIMIZED RESULT ===
Line --------------------- Program -------------------- ------ Function
-------
 549 /kernel/sys/driver
initialize*
 532 /kernel/sys/driver
 _initialize
 629 /kernel/sys/rsrcd
initd*
 400 /kernel/lib/auto (/kernel/sys/rsrcd)
compile_object
 131 /kernel/lib/auto (~System/initd)
 _F_create*
  37 ~System/initd
construct
  80 ~System/initd
install_service
 178 ~System/lib/system (~System/initd)
preload_path
 [ticks: 6486]



More information about the DGD mailing list