chicken-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-hackers] code suggestion


From: Jörg F . Wittenberger
Subject: [Chicken-hackers] code suggestion
Date: Mon, 1 Feb 2016 19:27:11 +0100
User-agent: Mozilla/5.0 (X11; Linux armv7l; rv:38.0) Gecko/20100101 Icedove/38.4.0

Hi all,

given argvector's in place I wonder if we should change some things.

If this code would be correct.  If so, it could simplify library.scm
(list->vector and vector->list would become one-liners, vector a
##core#primitive) and save runtime.

--- runtime.c ---

void C_ccall C_make_vector(C_word c, C_word *av)
{
  C_word v, *v0, needed = sizeof(C_header) + (c-2) * sizeof(C_word);
  if(!C_demand(needed))
    C_save_and_reclaim((void *)C_make_vector, c, av);
  v0 = C_alloc(needed);
  v = (C_word) v0;
  *(v0++) = C_VECTOR_TYPE | (c-2);
switch(c) {
case 2: break;
case 3: v0[0]=av[2]; break;
case 4: v0[0]=av[2];  v0[1]=av[3]; break;
default:
  C_memcpy(v0, av+2, (c-2) * sizeof(C_word));
}
  av[0] = av[1];
  av[1] = v;
  C_do_apply(2, av);
}

--- library.scm ---

(define vector (##core#primitive "C_make_vector"))

-------------------


So far the gain would be only so much.  However if we also had:


--- runtime.c ---
void C_ccall C_apply_vector(C_word c, C_word *av)
{
  C_word *av2, k, f, *v, sz;

  k=av[1]; f=av[2], v=(C_word *)(av[3]); sz=C_header_size(v);
  if(!C_demand(2+sz))
    C_save_and_reclaim((void *)C_apply_vector, c, av);
  av2=C_alloc((2+sz) *sizeof(C_word));
  av2[0]=f;
  av2[1]=k;
  C_memcpy(av2+2, v+1, sz * sizeof(C_word));
  C_do_apply(2+sz, av2);
}
-------

we could rewrite dynamic-wind like this:

--- library.scm ---
(define (dynamic-wind before thunk after)
  (before)
  (set! ##sys#dynamic-winds (cons (cons before after) ##sys#dynamic-winds))
  (let ((results (##sys#call-with-values thunk vector)))
    (set! ##sys#dynamic-winds (##sys#slot ##sys#dynamic-winds 1))
    (after)
    (apply-vector/unsafe ##sys#values results)))
-------

The attached test does some simple benchmarking on this.  Depending on
optimization (-O2 or more) results on my desktop machine indicate
anything between 10-30% gain.

Having written this, I wonder: maybe we could simply put the tag into
av[1] in the C_make_vector above and return av+1.  Should have the same
effect and save all the memcpy.

Cheers

/Jörg



reply via email to

[Prev in Thread] Current Thread [Next in Thread]