Java Card Optimization

The very first thing we need to understand is Javacard is not Java, it can be thought as a subset of Java with less features. Javacard is primarily used for developing smart card applets. As we know we have limited number of resources in smart cards (like NVM, RAM etc) so we should be able to write applets which will require minimum number of computing resources.

There are many areas in Javacard applet development in which general optimizations can be found. These can be in design, creation of objects, method invocation, accessing array elements, if-else vs switch statement etc. But everything in this world comes with a trade-off ,therefore sometimes optimizations may lead to overhead when compared with the non-optimized code of the same functionality.

General guidelines for Applet optimization are-

  • The very first optimization comes at architectural level. We must try to use object-oriented features of java language as it provides an organized way of defining our methods and variables which are common to certain kind of objects. This increases re-usability of code.
  • We must try to create all objects in the constructor of applet itself. This way, applet can calculate the amount of memory it will need during execution and will prevent out_of_memory problem and moreover, the install() method of an applet is invoked within transaction hence, if there is an failure in between the installation then the allocated memory can be reclaimed.
  • Nested calling of methods should be avoided as in most of the smart cards, stack size on RAM is 200 bytes.Therefore, to reduce the stack usage method parameters, nested method invocations should be avoided. Applets should not use recursive calls.
  • Avoid using more than 3 parameters for virtual methods and 4 for static methods. This way, the compiler will use a number or bytecode shortcuts, which reduces code size.
  • To work on temporary data, one can use APDU buffer or transient arrays as writing on EEPROM is about 1,000 times slower than writing on RAM.
  • Inheritance is also an overhead in Javacard particularly when the hierarchy is complex.
  • Accessing array elements is also an overhead to card. So, in situations where there is repeated accessing of an array element try to store the element in a local variable and use it.

    Instead of doing this:

    if (arr[index1] == 1) do this;
    OR
    if (arr[index1] == 2) do this;
    OR
    if (arr[index1] == 3) do this;

    Do this:

    temp = arr[index1];
    if (temp == 1) do this;
    OR
    if (temp == 2) do this;
    OR
    if (temp == 3) do this;

There are many more areas in which optimization is possible but while optimizing we should always be thinking whether we really need to optimize our code or not because optimizing the code might produce some good results but in most of the cases it will come with a trade-off which should also be considered.

And as I always say:

Think Twice, Code Once!

Keep Coding,

Chao.

 

Leave a comment

Create a free website or blog at WordPress.com.

Up ↑