Could the speed difference depends on the extra time it takes to find the '[]() lfun for the sequence?
Yes, that seems reasonable. index_no_free is called through o_index in both cases. If it's a real array then simple_array_index_no_free is called directly and it's done.
Otherwise it calls object_index_no_free, which looks up the `[] lfun (at a fixed index in an array), pushes the index on the stack again, makes a high level call to it through mega_apply, and then it arrives in your lfun where the index is read (again) from the stack and simple_array_index_no_free is called.
The indirection through an object doesn't come for free.
/ Martin Stjernholm, Roxen IS
Previous text:
2003-06-12 11:59: Subject: Question about speed
As some of you probably know am I currently adding some ADTs to Pike, among those a sequence. I've been running some speed comparisons between the new sequence and the array regarding indexing and index assignment ( [] and []= ) and found that the array is about twice as fast as the sequence. I found this quite surprising since it use about the same code as the array, it is just a few rows extra, see the code below. Could the speed difference depends on the extra time it takes to find the '[]() lfun for the sequence?
The CMOD-Code
PIKEFUN mixed `[](mixed index) {
simple_array_index_no_free(Pike_sp, THIS->a, index); Pike_sp++; }
PIKEFUN mixed `[]=(mixed index, mixed value) { should_copy(); simple_set_index(THIS->a, index, value); }
The final C-Code
void f_Sequence_cq__backtick_5B_5D(INT32 args) { #line 67 "/export/spare/pike/home/peta/Pike/7.5/src/post_modules/_ADT/sequence.c mod" struct svalue * index; #line 67 "/export/spare/pike/home/peta/Pike/7.5/src/post_modules/_ADT/sequence.c mod" if(args != 1) wrong_number_of_args_error("`[]",args,1); #line 67 "/export/spare/pike/home/peta/Pike/7.5/src/post_modules/_ADT/sequence.c mod" index=Pike_sp+0-1; dmalloc_touch_svalue(Pike_sp+0-1); #line 67 "/export/spare/pike/home/peta/Pike/7.5/src/post_modules/_ADT/sequence.c mod" {
simple_array_index_no_free(Pike_sp, THIS->a, index); Pike_sp++; }
void f_Sequence_cq__backtick_5B_5D_eq(INT32 args) { #line 89 "/export/spare/pike/home/peta/Pike/7.5/src/post_modules/_ADT/sequence.c mod" struct svalue * index; #line 89 "/export/spare/pike/home/peta/Pike/7.5/src/post_modules/_ADT/sequence.c mod" struct svalue * value; #line 89 "/export/spare/pike/home/peta/Pike/7.5/src/post_modules/_ADT/sequence.c mod" if(args != 2) wrong_number_of_args_error("`[]=",args,2); #line 89 "/export/spare/pike/home/peta/Pike/7.5/src/post_modules/_ADT/sequence.c mod" index=Pike_sp+0-2; dmalloc_touch_svalue(Pike_sp+0-2); #line 89 "/export/spare/pike/home/peta/Pike/7.5/src/post_modules/_ADT/sequence.c mod" value=Pike_sp+1-2; dmalloc_touch_svalue(Pike_sp+1-2); #line 89 "/export/spare/pike/home/peta/Pike/7.5/src/post_modules/_ADT/sequence.c mod" { should_copy(); simple_set_index(THIS->a, index, value); }
/ Peta, jo det är jag