Titanium SDK : support for Int32Array on Android

Int32Array are fast implementation of int arrays with Javascript and for now Titanium does not support it.

With the current version the following code will crash :

Java :

	@Kroll.method
	public int myProxyMethod(int[] tabs)
	{
		return 0;
	}

JavaScript :

	var myJavaModule= require('fr.dzzd.myJavaModule');
	var tabs = new Int32Array();
	myJavaModule.myProxyMethod(tabs);

Return : Invalid value, expected type Array

The upcoming patch will allow Int32Array aswell as some other optimisations like autodetection of Array and Int32Array when they are embeded in another object.

Titanium already implement a specific method for int[] arrays transfert from JS to Java but this is a very unefficient one and if the type of parameter is not specifically set to int[] or if it is embeded in another object like function(Object[]) it wont use it.

Firsts results with Int32Array are very promising :

The benchmark is sending 50 times an array of 100000 integers to two java methods with two different signatures.

Java module test code (the test will call those methods multiple times from Javascript):

	@Kroll.method
	public int benchmarkInt(int[] tabs)
	{
		return 0;
	}

	@Kroll.method
	public int benchmarkObject(Object args)
	{
		return 0;
	}

Current SDK average results (5MBytes transfered) :

public int benchmarkInt(int[] tabs) 
=> 227ms

public int benchmarkObject(Object args)
=>1776ms

After patch average results (5MBytes transfered) :

public int benchmarkInt(int[] tabs) 
=> 9ms

public int benchmarkObject(Object args)
=>15ms

The patched version is about 20x faster for int[] and more then hundred faster for Object[].

This is an important improvment as it will enable to fastly send big set of data to Java modules from Javascript, allowing new kind of applications and it will also improve existing modules by autodetecting int arrays embeded in JavaScript objects.

Also this is just a first step, I will take care of Float & Long as well for a future release.

1 Like

wow, those numbers are impressive!

Done !

The fasted way to change your JavaScript code to use it without breaking anything else is to make this replacement for method with int[] parameters :

yourJavaModule.javaMethod(intArray);
replaced by:
yourJavaModule.javaMethod(Int32Array.from(intArray));

This is not the more efficient but still 10 times faster.