Re: gethostname example: trying to avoid superfluous alloc+free
Kevin Rosenberg <kevin-HJRc7zDS/[email protected]> Thu, 21 Mar 2002 06:47:50 -0700 (MST)
| Newsgroups | gmane.lisp.uffi.general |
|---|---|
| Message-ID | <[email protected]> |
> Actually, I'd like to point out that using alloc+free foreign strings is
> far away from the most efficient implementation on CMUCL (and CLISP
> which is not supported).
I expect that you are right. However, for Allegro which can open-code
pointer access with static allocation, static allocation is much faster
than stack allocation. Further, stack allocation has a limitation with
interpreted code where the object position may change with garbage
collection. (see docs for f::with-stack-fobject)
CL-USER> (stk-vs-stat)
Stack allocation
; cpu time (non-gc) 6,910 msec user, 0 msec system
; cpu time (gc) 310 msec user, 0 msec system
; cpu time (total) 7,220 msec user, 0 msec system
; real time 7,217 msec
; space allocation:
; 0 cons cells, 24,000,000 other bytes, 0 static bytes
Static allocation, open-coded slot access
; cpu time (non-gc) 1,420 msec user, 10 msec system
; cpu time (gc) 180 msec user, 0 msec system
; cpu time (total) 1,600 msec user, 10 msec system
; real time 1,609 msec
; space allocation:
; 0 cons cells, 16,000,000 other bytes, 4,000,000 static bytes
(defun stk ()
(ff:with-stack-fobject (a :int)
(setf (ff:fslot-value a) 0)))
(defun stat ()
(let ((ptr (ff:allocate-fobject :int :c)))
(setf (ff:fslot-value-typed :int :c ptr) 0)
(ff:free-fobject ptr)))
(defun stk-vs-stat ()
(format t "~&Stack allocation")
(time (dotimes (i 1000)
(dotimes (j 1000)
(stk))))
(format t "~&Static allocation, open-coded slot access")
(time (dotimes (i 1000)
(dotimes (j 1000)
(stat)))))
> I believe there should be easy ways in UFFI to call gethostname with
> stack allocated buffers were possible (CMUCL, maybe LispWorks too,
> CLISP). That's how I got the idea of my (untested) somehow odd code
> below.
I'm not against easy ways. The first priority of design is that
correct UFFI code that works on one platform must work on all platforms.
Allegro seems to be the weakest in terms of stack allocation support.
> Esp. I think examples should show code that is amenable to efficient
> translation on the platforms where this is possible, whereas using
> alloc/free is hard (to much compiler type program or data flow analysis
> involved instead of simple macro transformations). The purpose is that
> there would be a chance that a portable style of writing emerges among
> users which supports (or at least doesn't stand in the way of) the
> optimization goal. Examples educate people. Copy&paste rules even in
> Lisp.
Yes, that why I'm adding to the examples directory nearly every day. It
also serves as an efficient tester to make sure the examples don't break
as UFFI is modified.
Hmm. I could make a macro for binding a variable with an allocation and
a free for Allegro, but transform into a stack allocation for CMUCL.
Kevin