On Mon, 28 Jun 2010 17:25:18 -0500
Gabriel Dos Reis <[email protected]> wrote:
> Is the ECL compilation scheme of functions defined by
> labels likely to yield better codes than those functions
> defined at toplevel?
When I read your mail I added on my TODO list to perform some
locality/global tests out of curiosity and see how ECL HEAD does now
with them. I could do some testing today.
The following test code tests the difference between a dynamic global
variable versus a local closure one, as well as between a global
function and a local one. The resulting C code is also attached.
It follows that for variables, *GLOBAL* and SHARED accesses are about
equivalent as they both require the environment (to obtain the closure
environment in the latter case). However, ECL was able to optimize
away ecl_plus() calls in the SHARED case, unlike on *GLOBAL* despite an
additional redundant DECLARE to tell the compiler it holds a FIXNUM.
As for global vs local functions, they currently seem to be treated the
same in this particular case.
However, depending on the use case, it would be possible for global
functions to be called via a dispatcher routine, which is important
when a function is expected to be redefined dynamically (which is more
expensive than calling a C function directly, of course).
--
Matt
;;; XXX Note:
;;; For this to really be taken into consideration,
;;; enter it at the REPL before issuing COMPILE-FILE.
(declaim (optimize (speed 3) (safety 0) (debug 0)))
(defparameter *global* 10)
(declaim (type fixnum *global*))
(defun count-1 ()
(loop
for i of-type fixnum from 1 to *global*
collect i))
;;; ecl_plus() is still invoked in this case
(defun count-2 ()
(declare (fixnum *global*))
(loop
for i of-type fixnum from 1 to 10
collect (the fixnum (+ *global* i))))
(let ((shared 10))
(declare (type fixnum shared))
(defun count-3 ()
(loop
for i of-type fixnum from 1 to shared
collect i))
;;; Inline C + !
(defun count-4 ()
(loop
for i of-type fixnum from 1 to 10
collect (the fixnum (+ shared i)))))
(defun function-1 ()
nil)
(defun test-1 ()
(loop
for i of-type fixnum from 0 to 100000
do (function-1)))
(defun test-2 ()
(flet ((function-2 ()
nil))
(loop
for i of-type fixnum from 0 to 100000
do (function-2))))
/* Compiler: ECL 10.7.1 */
/* Date: 2010/7/4 16:00 (yyyy/mm/dd) */
/* Machine: NetBSD 5.0_STABLE i386 */
/* Source: /tmp/tmp.lisp */
#include <ecl/ecl-cmp.h>
#include "/tmp/tmp.h"
/* function definition for COUNT-1 */
/* optimize speed 3, debug 0, space 0, safety 0 */
static cl_object
L1count_1()
{
VT2 VLEX2 CLSR2 STCK2
const cl_env_ptr cl_env_copy = ecl_process_env();
cl_object value0;
TTL:
{
cl_fixnum V1; /* I */
cl_fixnum V2;
V1 = 1;
V2 = fix(ECL_SYM_VAL(cl_env_copy, VV[0]));
{
cl_object V3;
cl_object V4;
V3 = ecl_list1(Cnil);
V4 = V3;
L6: ;
if (!((V1) > (V2))) {
goto L10;
}
goto L7;
L10: ;
goto L8;
L8: ;
T0 = V4;
V4 = ecl_list1(MAKE_FIXNUM(V1));
ECL_CONS_CDR(T0) = V4;
V1 = (V1) + 1;
goto L6;
L7: ;
value0 = CDR(V3);
cl_env_copy->nvalues = 1;
return value0;
}
}
}
/* function definition for COUNT-2 */
/* optimize speed 3, debug 0, space 0, safety 0 */
static cl_object
L2count_2()
{
VT3 VLEX3 CLSR3 STCK3
const cl_env_ptr cl_env_copy = ecl_process_env();
cl_object value0;
TTL:
{
cl_fixnum V1; /* I */
V1 = 1;
{
cl_object V2;
cl_object V3;
V2 = ecl_list1(Cnil);
V3 = V2;
L5: ;
T0 = V3;
T1 = ecl_plus(ECL_SYM_VAL(cl_env_copy, VV[0]),
MAKE_FIXNUM(V1));
V3 = ecl_list1(T1);
ECL_CONS_CDR(T0) = V3;
V1 = (V1) + 1;
if (!((V1) > (10))) {
goto L13;
}
goto L6;
L13: ;
goto L11;
L11: ;
goto L5;
L6: ;
value0 = CDR(V2);
cl_env_copy->nvalues = 1;
return value0;
}
}
}
/* function definition for COUNT-3 */
/* optimize speed 3, debug 0, space 0, safety 0 */
static cl_object
L3count_3(cl_narg narg,...)
{
VT4 VLEX4 CLSR4 STCK4
const cl_env_ptr cl_env_copy = ecl_process_env();
cl_object env0 = cl_env_copy->function->cclosure.env;
cl_object value0;
/* Scanning closure data ... */
CLV0 = env0; /* SHARED */
{ /* ... closure scanning finished */
TTL:
{
cl_fixnum V1; /* I */
cl_fixnum V2;
V1 = 1;
V2 = fix(ECL_CONS_CAR(CLV0));
{
cl_object V3;
cl_object V4;
V3 = ecl_list1(Cnil);
V4 = V3;
L6: ;
if (!((V1) > (V2))) {
goto L10;
}
goto L7;
L10: ;
goto L8;
L8: ;
T0 = V4;
V4 = ecl_list1(MAKE_FIXNUM(V1));
ECL_CONS_CDR(T0) = V4;
V1 = (V1) + 1;
goto L6;
L7: ;
value0 = CDR(V3);
cl_env_copy->nvalues = 1;
return value0;
}
}
}
}
/* function definition for COUNT-4 */
/* optimize speed 3, debug 0, space 0, safety 0 */
static cl_object
L4count_4(cl_narg narg,...)
{
VT5 VLEX5 CLSR5 STCK5
const cl_env_ptr cl_env_copy = ecl_process_env();
cl_object env0 = cl_env_copy->function->cclosure.env;
cl_object value0;
/* Scanning closure data ... */
CLV0 = env0; /* SHARED */
{ /* ... closure scanning finished */
TTL:
{
cl_fixnum V1; /* I */
V1 = 1;
{
cl_object V2;
cl_object V3;
V2 = ecl_list1(Cnil);
V3 = V2;
L5: ;
T0 = V3;
{
cl_fixnum V4;
V4 = (fix(ECL_CONS_CAR(CLV0))) + (V1);
V3 = ecl_list1(MAKE_FIXNUM(V4));
}
ECL_CONS_CDR(T0) = V3;
V1 = (V1) + 1;
if (!((V1) > (10))) {
goto L13;
}
goto L6;
L13: ;
goto L11;
L11: ;
goto L5;
L6: ;
value0 = CDR(V2);
cl_env_copy->nvalues = 1;
return value0;
}
}
}
}
/* function definition for FUNCTION-1 */
/* optimize speed 3, debug 0, space 0, safety 0 */
static cl_object
L5function_1()
{
VT6 VLEX6 CLSR6 STCK6
const cl_env_ptr cl_env_copy = ecl_process_env();
cl_object value0;
TTL:
value0 = Cnil;
cl_env_copy->nvalues = 1;
return value0;
}
/* function definition for TEST-1 */
/* optimize speed 3, debug 0, space 0, safety 0 */
static cl_object
L6test_1()
{
VT7 VLEX7 CLSR7 STCK7
const cl_env_ptr cl_env_copy = ecl_process_env();
cl_object value0;
TTL:
{
cl_fixnum V1; /* I */
V1 = 0;
L3: ;
(void) L5function_1() /* FUNCTION-1 */ ;
V1 = (V1) + 1;
if (!((V1) > (100000))) {
goto L10;
}
goto L4;
L10: ;
goto L8;
L8: ;
goto L3;
L4: ;
value0 = Cnil;
cl_env_copy->nvalues = 1;
return value0;
}
}
/* function definition for TEST-2 */
/* optimize speed 3, debug 0, space 0, safety 0 */
static cl_object
L8test_2()
{
VT8 VLEX8 CLSR8 STCK8
const cl_env_ptr cl_env_copy = ecl_process_env();
cl_object value0;
TTL:
{
cl_fixnum V1; /* I */
V1 = 0;
L3: ;
(void) LC7function_2() /* FUNCTION-2 */ ;
V1 = (V1) + 1;
if (!((V1) > (100000))) {
goto L10;
}
goto L4;
L10: ;
goto L8;
L8: ;
goto L3;
L4: ;
value0 = Cnil;
cl_env_copy->nvalues = 1;
return value0;
}
}
/* local function FUNCTION-2 */
/* optimize speed 3, debug 0, space 0, safety 0 */
static cl_object
LC7function_2()
{
VT9 VLEX9 CLSR9 STCK9
const cl_env_ptr cl_env_copy = ecl_process_env();
cl_object value0;
TTL:
value0 = Cnil;
cl_env_copy->nvalues = 1;
return value0;
}
#include "/tmp/tmp.data"
#ifdef __cplusplus
extern "C"
#endif
ECL_DLLEXPORT void
init_fas_CODE(cl_object flag)
{
VT1 VLEX1 CLSR1 STCK1
const cl_env_ptr cl_env_copy = ecl_process_env();
cl_object value0;
cl_object *VVtemp;
if (!FIXNUMP(flag)) {
Cblock = flag;
#ifndef ECL_DYNAMIC_VV
flag->cblock.data = VV;
#endif
flag->cblock.data_size = VM;
flag->cblock.temp_data_size = VMtemp;
flag->cblock.data_text = compiler_data_text;
flag->cblock.data_text_size = compiler_data_text_size;
flag->cblock.cfuns_size = compiler_cfuns_size;
flag->cblock.cfuns = compiler_cfuns;
flag->cblock.source =
make_constant_base_string("/tmp/tmp.lisp");
return;
}
#ifdef ECL_DYNAMIC_VV
VV = Cblock->cblock.data;
#endif
Cblock->cblock.data_text = "@EcLtAg:init_fas_CODE@";
VVtemp = Cblock->cblock.temp_data;
{
cl_object V1;
cl_object V2;
V1 = Cnil;
V2 = VVtemp[0];
L6: ;
if (!(V2 == Cnil)) {
goto L10;
}
goto L7;
L10: ;
goto L8;
L8: ;
V1 = ECL_CONS_CAR(V2);
V2 = ECL_CONS_CDR(V2);
(void) ecl_function_dispatch(cl_env_copy, ECL_SYM("PROCLAIM",
664)) (1, V1);
goto L6;
L7: ;
goto L2;
}
L2: ;
(void) si_Xmake_special(VV[0]) /* *MAKE-SPECIAL */ ;
ECL_SETQ(cl_env_copy, VV[0], MAKE_FIXNUM(10));
{
cl_object V3;
cl_object V4;
V3 = Cnil;
V4 = VVtemp[1];
L30: ;
if (!(V4 == Cnil)) {
goto L34;
}
goto L31;
L34: ;
goto L32;
L32: ;
V3 = ECL_CONS_CAR(V4);
V4 = ECL_CONS_CDR(V4);
(void) ecl_function_dispatch(cl_env_copy, ECL_SYM("PROCLAIM",
664)) (1, V3);
goto L30;
L31: ;
goto L26;
}
L26: ;
ecl_cmp_defun(VV[8]);
ecl_cmp_defun(VV[9]);
{
cl_object env1 = env0;
env1 = Cnil;
CLV0 = env1 = CONS(MAKE_FIXNUM(10), env1); /* SHARED
*/
T0 = ecl_make_cclosure_va((cl_objectfn) L3count_3, env1,
Cblock);
(void) si_fset(4, VV[3], T0, Cnil, Cnil) /* FSET */ ;
T0 = ecl_make_cclosure_va((cl_objectfn) L4count_4, env1,
Cblock);
(void) si_fset(4, VV[4], T0, Cnil, Cnil) /* FSET */ ;
}
ecl_cmp_defun(VV[10]);
ecl_cmp_defun(VV[11]);
ecl_cmp_defun(VV[12]);
}
------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Ecls-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ecls-list