2005/5/9

     
 

Stack Variables

artefaktur

| References | Casting | Arrays | Import | instanceof | Package | Synchronization | Throwable | finally | Dangerous | Stack | String |

In Java, objects are always allocated in a heap. In ACDK, you can be much more flexible.

Using Reference in ACDK is a very easy way to manage objects.
Yet, there is a significant overhead using references.

In order to avoid this overhead, you can use Stack Objects. First, here is an example with Reference Objects:

// using Reference Object
RStringBuffer sb = new StringBuffer(10); // cost for allocating with new
for (int i = 0; i < 10; i++) {
  sb->add(i); /* cost for checking sb == Nil +
                 cost for dispatch virtual function +
                 cost for synchronizing in add
               */
}
Now, the same example with Stack Object:

// using Stack Object
StringBuffer sb(10); // NO cost for allocating with new
for (int i = 0; i < 10; i++) {
  sb.add(i); /* NO cost for checking sb == Nil +
                NO cost for dispatch virtual function +
                NO cost for synchronizing in add
             */
}
In Reference Objects, you have an object with an 'all in one insurance' just like the one known in Java. In Stack Objects, you have an object with a clear lifetime and boosted performance.

You can also convert a Stack Reference to Reference Objects.

void
addSomething(RStringBuffer sb) // Reference Object
{
  sb->add("bla"); /* cost for checking sb == Nil +
                     cost for dispatch virtual function +
                  NO cost for synchronizing in add */
}

StringBuffer sb(10);
addSomething(&sb); // convert StringBuffer to RStringBuffer
If you want to use this pattern, you must be sure, that the addSomething(RStringBuffer sb) does not store the Reference anywhere else, and that the life cycle of the Reference is not longer than the call.


void
addSomething(StringBuffer& sb) // Stack Object
{
  sb.add("asdf"); /*
                  NO cost for checking sb == Nil +
                  NO   cost for dispatch virtual function +
                  NO cost for synchronizing in add */
}

RStringBuffer sb = getStringBufferFromElseWhere();
addSomething(*sb); // checks for Nil
For more information about operations on RObject's please refer to  References;


 < prevConstructs(11 / 12) next >