swarm-modeling
[Top][All Lists]
Advanced

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

replacing objc method with c function; ivars inside function no good...


From: Paul E Johnson
Subject: replacing objc method with c function; ivars inside function no good...
Date: Mon, 02 Apr 2001 18:27:44 -0500

I want to talk in alegory here, like in the Bible, but hoping Gods will
actually answer with something beside a lightning bolt.

Some of us were talking about optimizing a very frequently used class,
which, for the sake of discussion, has a bunch of ivars and a method

-(unsigned) getMyUnsigned {   

  ---lots of IVARS used here---
}

>From profiling, we learned that the program was slowed down a lot
because elsewhere in the class there were lots usages like 

  x=[self getMyUnsigned];

and the objc lookup on it was expensive, so my smarter colleague said
"replace that with a C function, 
avoid the overhead".  So we just want a usage like
 
  x=getMyUnsignedFunction();

I said great, I've done that. 

But I ran into a little trouble. I know there was a way around it.

Suppose I declare

unsigned
getMyUnsignedFunction (){
   ---want to use IVARS in here ----
}

The compiler goes crazy because it can't find any of the IVARS. Know
what I mean? It says all the ivars are undeclared.  

It appears to me that, in this particular case, many of the IVARS are
just numerical constants, so inside the function I can just re-declare
them.  But two of the variables inside the method are important ivars
that condition the next response to getMyUnsignedFunction().  So is the
only correct response to pass-by-reference those IVARS into the
function, as in 

unsigned
getMyUnsignedFunction(int * ivar1, int * ivar2){

        //re-declare all the IVARs that are constants or can be reconstructed:
        int x, y,z;
        defererence: use *ivar1 and *ivar2 here.
   }
and use the function like
   x = getMyUnsignedFunction(&ivar1, &ivar2);

I don't remember it being this difficult to use a function in
Objective-C.  But, then again, I don't think I clearly understood what a
pointer was until about ten minutes ago :)

-- 
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ukans.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700


                  ==================================
   Swarm-Modelling is for discussion of Simulation and Modelling techniques
   esp. using Swarm.  For list administration needs (esp. [un]subscribing),
   please send a message to <address@hidden> with "help" in the
   body of the message.
                  ==================================


reply via email to

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