Pushing the boundaries

…couldn’t be any simpler!

Archive for the ‘Programming’ Category

Photosynth – WTF!

Posted by vijayram on June 12, 2007

Be blown away – HERE

Download the plugin (5 Meg +) . http://labs.live.com/photosynth/ 

What blew my mind was the fact that I watched the movie “Deja Vu” the day before where the digital sureveillance used had data-streams of realtime feed which allowed them to present any setting at any angle. You are talking of billions of bytes of data for imaging at realtime. I thought it was rubbish.

But Photosynth took this a little closer. It can:

* Walk or fly through a scene to see photos from any angle.
* Seamlessly zoom in or out of a photo whether it’s megapixels or gigapixels in size.
* See where pictures were taken in relation to one another.
* Find similar photos to the one you’re currently viewing.
* Send a collection – or a particular view of one – to a friend.

Posted in Programming, Technology | 3 Comments »

integer overflow

Posted by vijayram on February 4, 2007

Santosh Kumar over on the iGeeks brought to my attention about this interesting topic. I never have dug in deep to learn about this subject, since I know it could cause more harm 😉

He mentioned: ” While type casting from Number (64-bit double-precision) to int (32-bit integer) if Number value range cannot fit into integer than integer value is converted to default value ie 0.”

Interesting, but why should it default to zero?! That begged the question. Let me retrospect from my earlier post. I mentioned, anything over the range of Integer values, shall be internally operated as a “Number” data-type in Flash.

var i:int = 2147483648;
<type name=”Number” base=”Object” isDynamic=”false” isFinal=”true” isStatic=”false”>
<extendsClass type=”Object”/>
<parameter index=”1″ type=”*” optional=”true”/>

Wait a minute, we just came across an example of “integer overflow“! When an attempt is made to store a value greater than this maximum value it is known as an integer overflow. Why didn’t the compiler warn me? Apparently where is no compile-time warnings or run-time exceptions to let you this.
Integer overflows cannot be detected after they have happened, so there is not way for an application to tell if a result it has calculated previously is in fact correct. This can get dangerous if the calculation has to do with the size of a buffer or how far into an array to index.

Ok, time to understand what is happening here. Lets take the case of both the unsigned integer and signed integer overflows (there are overflows and underflows).
When an unsigned integer reaches its maximum value, it wraps around and starts over, much as a car odometer. Unsigned short integers can’t hold a number larger than 4294967295, so the value is wrapped around to 0.

var i:uint = 4294967296; //overflow
// output – 0

A signed integer is different from an unsigned integer, in that half of the values you can represent are negative. Instead of picturing a traditional car odometer, you might picture one that rotates up for positive numbers and down for negative numbers. One mile from 0 is either 1 or -1. When you run out of positive numbers, you run right into the largest negative numbers and then count back down to 0.

var i:int = 2147483648; // overflow
//output – -2147483648 (this is largest negative int value)

Why is this important to know??Supposing you are working with unsigned data-type, the memory would be allocated for it accordingly. If that value has wrapped around then there would be far little memory for it now available. Disaster. Or if you are comparing values with a signed integer and it has overflown to be a negative, it would it pass througn. Disaster again!! If an attempt is made to store a value in an integer which is greater than the maximum value the integer can hold, the value will be truncated. If the stored value is the result of an arithmetic operation, any part of the program which later uses the result will run incorrectly as the result of the arithmetic being incorrect.

Now, think of what impact this could have. One could exploit this if it has not been taken care for in the Flash Plugin. One cannot directly overwrite the memory but the exploitation can be subtle. This topic is out of the subject for this current post but you can read about it here on Wikipedia. Flash plugin was prone to heap buffer read overflow in the recent past.
Microsoft have there version of SafeInt class to help safeguard from this issue. All arithmetic is performed on unsigned values, which is very interesting.

Now back to Flash. Any number too big is just returned back as “infinity”. Clever!!! But why?
Lets just remember that Flash uses another data-type for arithmetic operations called “Number”. The Number data-type interoperates as “double” data-type which means it has a larger range of numbers it can store and display.

Through some debugging process, I came to know that Flash uses “int” data-type to represent any values between the range 2^29 (-268435456 to +268435455). I am not sure what happens to those extra 3 bits, though one bit would be used for the sign (+ or-). Anything over this range is held internally as a Number data-type to represent. What this implies is that, we do not have our flash player crashing if we have an overflow or underflow as this implicit casting happens internally. Anything over the range of Number will be called as “infinity”.

Posted in Adobe, AS 3.0, Flash, Programming | 3 Comments »

int vs Number

Posted by vijayram on February 1, 2007

This issue was highlighted by Sho Kuwamoto sometime back who recommends to use Number where ever possible. Grant Skinner made some additional test and has posted his results here.

While looking through the code on tamarin on the class construct for Number, it is defined as a 64-bit double-precision floating-point number while, int and uint are 32-bit each. So technically both int and uint use 4bytes in the memory whilst Number uses 8 bytes of memory.

From, the Flex doc it says: “When you store integer values with the Number data type, only the 52 bits of the significand are used. The Number data type uses these 52 bits and a special hidden bit to represent integers from -9,007,199,254,740,992 (-253) to 9,007,199,254,740,992 (253)”. Explains the 64-bit range.

By looking through tamarin, it is evident what the maximum and minimum value you can store for each of these data types.
For Number:
public static const MIN_VALUE :Number = 4.9e-324
public static const MAX_VALUE :Number = 1.7976931348623158e+308
For int:
public static const MIN_VALUE:int = -0x80000000;
public static const MAX_VALUE:int = 0x7fffffff;
For uint:
public static const MIN_VALUE:uint = 0;
public static const MAX_VALUE:uint = 0xffffffff;

I was actually digging through the notes on tamarin just to see how things work and why we have this variations in speed.
It so happens that, almost all the numbers ranges that we use to code falls between the max and min values of an integer (-2,147,483,648 to 2,147,483,647). Flash does an implicit conversion at runtime for any values we pass between this range to an integer. Even if you pass a floating point to a variable data-typed as int.How did I know this?

The flash.utils package contains a method called describeType. Equipped with this function, call this on your variable which contains the value and you shall the result as follows:
<type name=”int” base=”Object” isDynamic=”false” isFinal=”true” isStatic=”false”>
<extendsClass type=”Object”/>
<parameter index=”1″ type=”*” optional=”true”/>

Even if your variable is data-typed to be a Number it still is converted to “int”. Now what happens if we exceeds the rage for Integer? It is automatically converted into a Number as it has a larger range than integer. Clever! What about uint? It is again converted to int.

Now go back to Grant Skinner’s test results and you shall see why int outperforms in most cases. As you are aware, Number data-type can accepts decimal values and hence floating-point operator(multiplication and division) is quicker when data-typed to Number.
So to avoid this performance overhead of implicit conversion, its better to data-type your variable to “int” during addition and subraction and whilst division and multiplication is involved use “Number” data-type.

Technorati Tags: , , ,

Posted in Adobe, AS 3.0, Flash, Programming | Leave a Comment »

native function

Posted by vijayram on January 31, 2007

Ok, this is a cool modifier. While I was looking through the notes on ActionScript 3.0, I did notice a reserved keyword called “native”. I had worked with native implementation in JAVA(JNI), so this really got me excited.Why?!

The reason being, the use of native functions indicates that the method is implemented externally. In a DLL maybe? ;). So, there maybe possiblities to call native windowsAPI from the swf…hmmm. Using this abstract modifier means that the method implementation is not provided in the class.Because an external method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no braces ({ }) following the signature.

native function functionName();
class className {
native function methodName();

So I started rummaging through the Flash Player code, and voila they use native functions as a part of implementation. What this means is that the native functions available to the Virtual Machine is called for through this function call using ActionScript.

//pseudo-final – no user class can extend Class
public dynamic class Class
// {DontEnum,DontDelete,ReadOnly}
public native final function get prototype()

// Class.length = 1 per ES3
// E262 {ReadOnly, DontDelete, DontEnum }
public static const length:int = 1;

The above is the actual code for a top level Class construct in the Virtual Machine which is available on tamarin.
Ok, the next obvious step, can I call for it from my code and override it if possible?!

Sadly not!!!! It turns out that Flash Player uses this internally and I cannot use it in my code as explained here.

Posted in Adobe, AS 3.0, Flash, Programming | Leave a Comment »

Garbage Collector

Posted by vijayram on January 31, 2007

Adding to the post on Stacks and Heaps, I had to link to this blog about the internal workings of Garbage Collector in ActionScript 3.0.

Grant Skinner on Resource Management . If that’s not enough, he has published his talks on Garbage Collector using visual simulators here.

*Some side notes:
– You cannot delete class members in ActionScript 3, if you want to cause a variable to no longer reference an object or value in memory, you should set your variable’s value to null instead of deleting it. [ myVar = null; ]
If all variable references to an object are null, the Garbage Collector will mark it for deletion and it will eventually be cleared from memory.
There is a post hereby Gordon Smith explaning the same.
– Ted Patrick has an interesting post on “flash.system.System.totalMemory” saying it just represents the memory allocated to the FlashPlayer. So it might not be the best util to check for memory leaks when you remove objects by setting it to null. You can learn about System.totalMemory here.
– FlashLite has this profiling for sometime now showing the heap memory usage. Pretty cool using the emulator tool. Any questions on that can be directed to IndiMad which is managed by Mariam.

Technorati Tags: , , ,

Posted in Adobe, AS 3.0, Flash, Programming | Leave a Comment »

Stacks and Heaps

Posted by vijayram on January 30, 2007

Someone asked me this question and I wasn’t in the best position to reply back then. This time I will take the effort of writing it down for future reference and for the benefit of all.

Simple explanation would be, stack is allocated before the program executes, where the heap contains memory allocated during the program run.

Using ActionScript for Flash, you can keep track of how much memory Flash Player uses at run-time using the System.totalMemory method.

To begin with data can be of two types, Value or Reference. Although there are many data types, they are always treated: as primitive (also called Value) and by Reference. It is important to note where this data is stored. Value types are stored in “stack” and Reference types are stored in the “heap”. Lets look at this way, objects created on the fly(ie: using the “new” keyword) is stored in the heap while variables will be stored in the stack.

Both Stack and Heap work together. Stack works on basis of last-in, first-out (LIFO). This would be the top-most in the pile would be removed first. Once an object has been instantiated from a class, it is moved into the heap.

During the course of the program running, the heap is polluted with unused data reference which doesn’t exist anymore. This is when the Garbage Collector kicks in. It is the role of the Garbage Collector to clear the heap of unused references or data, else you will have a memory leak, causing the program to come to a grinding halt!

Garbage Collector which is part of the ActionScript Virtual Machine uses several techniques to clear up the data of the heap. There is a “reference counting” technique and there is also a “mark and sweep” technique. From my understanding Garbage Collector in Flash Player 9 uses a deferred method whereby, based on certain factors, the Garbage Collector will kick-in and not by merrily removing the reference to the object in code clears up the heap.

Technorati Tags: , , ,

Posted in Adobe, AS 3.0, Flash, Programming | Leave a Comment »