java


Kotlin to Javascript

In my opinion, one of the most interesting Kotlin features is the ability to transpile into Javascript. I’m a huge fan of GWT (Java -> Javascript), a lot of my projects are working in the web with GWT (like http://chess.mobialia.com or http://12chess.com), and I need to know if Kotlin is going to be able to replace Java+GWT in the near future.

So I tried to compile the Carballo Kotlin code to JS (as a NodeJS app) in the “js” branch: https://github.com/albertoruibal/karballo/tree/js, and I couldn’t make it work yet. I found some problems that I will detail in this post.

GWT includes a reduced version of the JRE, without some classes and methods, but Kotlin is much more radical: To compile a Kotlin code to JS, it cannot reference any java.* class, so forget about java.lang.*, java.util.* or java.io.*.

No basic java types

Integer, Character, Boolean… are a Java classes and they cannot be used, so a lot of common operations must be done the “Kotlin way”, i.e.:

Integer.parseInt(myString) -> myString.toInt()
Character.toLowerCase(myChar) -> myChar.toLowerCase()
Integer.MAX_VALUE -> Int.MAX_VALUE

Emulating Java classes: System, Math, Random, Arrays, Date

These classes are not available when building to JS. To avoid changing the original code and migrate seamless from JVM to JS, I created auxiliar classes with the methods used in the code:

  • System: I implemented currentTimeMillis() (calling the JS Date object), exit(), arraycopy(), and an empty gc()
  • Math: When building for JS, the java.lang.Math class must be replaced by kotlin.js.Math, using this class avoids to change a lot of code
  • Random: Using kotlin.js.Math.random()
  • Arrays: Implementing some variants of the fill() method
  • Date: partial implementation to get the current date

Other problems

  • IntelliJ does not allow to mix the Java and the JS Kotlin stdlib in the same project, so I hat to create a different branch “js”
  • No Threads nor Coroutines support in JS jet
  • Kotlin’s readLine() (to read lines from stdin) is only available in the JVM stdlib
  • String.toBoolean() is only available in the JVM stdlib, I fixed it defining a extension function in the same package: 
    inline fun String.toBoolean(): Boolean = "true".equals(this.toLowerCase())
  • No support for asserts in the JS compiler
  • Some annotations aren’t available @JvmOverloads, @Throws…
  • No LinkedList
  • No StringBuffer, but in this case it’s better to use Kotlin’s StringBuilder
  • Missing StringBuilder.setLength() method (I sent a pull request to Kotlin implementing setLength() and substring())
  • It does not allow returns into synchronized blocks
  • No printStackTrace() on exceptions
  • No JUnit tests in JS, so I removed the tests in the “js” branch

Overall conclusion

After solving all these problems, the Kotlin2JS compiler is introducing bugs that don’t allow the karballo engine to work.

Kotlin2JS isn’t production ready, and years behind GWT.


Converting Carballo to Kotlin

Kotlin is a JVM language developed by JetBrains: http://kotlinlang.org gaining momentum among Android developers. Kotlin has interesting features like:

  • It can be compiled to bytecode compatible with Java >=6, allowing to use a lot of Java 7-8 features (lambdas…)  in Java 6 bytecode (=Android)
  • It can be transpiled to Javascript (like Java with GWT)

So I decided to migrate the Carballo Chess Engine code to Kotlin (and his name is Karballo) to make some experiments and having some “fun” :)… but it became a non-trivial task, the converted code is at: https://github.com/albertoruibal/karballo.

Converting the code

To start working with Kotlin I installed the Kotlin plugin for Android Studio (=IntelliJ) from:

File->Settings->Plugins->Install JetBrains Plugin

Once the Kotlin plugin is installed, it’s quite easy to convert java source files to Kotlin with: CTRL + SHIFT + ALT + K

Conversion problems

The Java to Kotlin code conversion does not work perfectly, the Carballo conversion arose these errors:

  • Kotlin is strong typed, you cannot compare a long against the literal ‘0’, you must use ‘0L’… I had hundreds of this comparisons
  • A Long cannot be initialized with an unsigned hex literal if the value does not fit in the signed type, it gives a “Value out of range” compilation error ,  so you cannot do:
    var variable = 0xffffffffffffffffL

    The solution is to convert the literals to a signed decimal:

    var variable = -1
  • Error “Property must be initialized or be abstract” with attributes not initialized in the constructor, solved adding the “lateinit” modifier to the declaration of the attributes (yes, Kotlin knows if you are initializing the attribute in the constructor)
  • Strange toInt() insertions:
    pieceNames.indexOf(pieceChar.toInt())

    should be:

    pieceNames.indexOf(pieceChar)
  • Variables of type Byte cannot be used as array indices, I had to manually change many vars from Byte to Int
  • Kotlin does not allow assignments in expressions, so it’s impossible to do:
    while ((node.move = node.moveIterator.next()) != Move.NONE) { 

    I manually had to change some cases to the more verbose:

    while (true) {
        node.move = node.moveIterator.next()
        if (node.move == Move.NONE) {
           break
        }
  • The binary operators do not work in multi line if they are placed at the beginning of the second line, only if they are at the end of the first, so:
    var myLong : Long = long1
        or long2

    does not compile, it must be:

    var myLong : Long = long1 or
        long2
  • It didn’t recognize some custom getters and I had to merge them manually, I like a lot how they look in Kotlin (notice the use of the special word “field” to avoid calling the getter recursively):
    var lastMoveSee: Int = SEE_NOT_CALCULATED
        get() {
            if (field == SEE_NOT_CALCULATED) {
                field = board.see(move, ai)
            }
            return field
        }
  • The conversion process got hung with two complex classes: CompleteEvaluator and ExperimentalEvaluator… I had to kill IntelliJ. I converted the CompleteEvaluator class copying to a new class small chunks of code.
  • Kotlin’s when() statement do not work like the Java’s switch->case, as it hasn’t breaks, you cannot jump from one option to the next excluding the break: the conversion duplicated a lot of the MoveIterator code and I fixed it manually.
  • Some other strange errors like wrong expressions and missing parenthesis…

Some things of Kotlin that I don’t like (yet)

Some are part of the claimed Kotlin “features”:

  • Kotlin does not has primitive types, but it seems to not affect the performance…
  • There is no ternary operator in Kotlin, it’s replaced with “if (…) … else …” expressions: This increases a lot the verbosity, al least in my code
  • Kotlin’s crusade against NullPointerExcepcions: It a type allows null, it must be explicitly indicated appending a question mark to the type:
    var myString : String? = null

    To convert a nullable var/val to a non-nullable, you should use the !! operator, this forces a NullPointerException if the value is null (and it seems that you are shouting to the IDE…):

    var myString : String? = "hello"
    var myStringNotNull : String = myString!!
  • Static fields and methods are grouped in a “Companion Object”
  • Compilation is slower than pure Java
  • Many bugs running from Android Studio non-android projects (IntelliJ worked better for me)
  • Couldn’t get the JS compilation working yet

And other things that I like

  • The full interoperability with Java
  • Type inference, normally I continue to specify the types, but in some cases it saves a bit of code
  • Data classes, they will save you hundreds of lines of boilerplate code https://kotlinlang.org/docs/reference/data-classes.html
  • Array initialization with lambdas
    nodes = Array(MAX_DEPTH, {i->Node(this, i)})
  • The singleton pattern is embedded in the language: using “object” instead “class” assumes that the class is a singleton
  • Visibility is “public” by default, with the access modifier “internal” it can be accessed only from the same module
  • Implicit getters / setters
  • No need for “new” to call constructors
  • And much more…

Performance

I’m my first tests, I’m not noticing any performance downgrade (or upgrade) over the Carballo Java version.


Improving Carballo Chess Engine the hard way  

torneo_ajedrezOne year ago my Carballo Chess Engine (https://github.com/albertoruibal/carballo) was stuck: all the improvements that I was trying were not working, and I detected the main problem: I am a poor chess player so I will never be a good chess engine developer. I thought that the main chess programming skill was statistical analysis and not chess knowledge, but I was wrong.

So, I took the decision of starting to learn and play chess. I joined the local chess club Xadrez Ramiro Sabell (http://www.xadrezramirosabell.com) and I was so lucky that in this club teaches chess the International Master Yudania Hernández Estevez. It’s quite curious the amazing people that you can find in a small city like Ponteareas. I also try to help the club in the tournaments organization and with a small Mobialia sponsorship. Now I am playing the Galician Chess League (in third division) and all the tournaments that I can.

My chess level is improving fast (ok, I’m under 1600 ELO yet: http://ratings.fide.com/card.phtml?event=24597015), but the real deal is that the Carballo Chess Engine strength is improving much faster, climbing positions in the CCRL list (http://www.computerchess.org.uk/ccrl/4040/). Learning chess helps me to diagnose the flaws and to understand better what’s going on under the hood.

Finally,  playing chess also helps me to detect the chess player needs, so I realized the main missing feature from Mobialia Chess: a chess database to review historic games and to analyze your own games searching statistics for each position. This year I worked to implement this feature and starting today you can access a Beta version of the database in Mobialia Chess Web (http://chess.mobialia.com).


Benchmarking Java to native compilers

Java DukeJava to native compilers have been around for some years, and I was curious about if one of this solutions could improve the performance of my Carballo Chess Engine.

I ran a tournament between binaries of the Carballo development version (1.5) compiled with different solutions to compare the performance. I used cutechess-cli to run a 3000 game tournament with time control 5″ + 0.1″ per move by side and with the Noomen Test Suite as the starting positions.

The compared binaries

  • The Pure Java Version: This is the carballo-1.5.jar ran with the Oracle JDK 1.8.0_73 VM under my 64bit linux (Debian Sid).
  • GCJ: The GNU’s Java compiler, incomplete and unfinished, but it works for Carballo. This binary was compiled with this script.
  • Excelsior Jet: A classic proprietary Java to native converter at http://www.excelsiorjet.com. I used Excelsior Jet 11 32bit for Linux (evaluation) to generate this binary. The 64bit version had worse results.
  • RoboVM: (https://robovm.com) A solution to run Java apps on iOS. Recently it was bought by Xamarin, and after the Microsoft acquisition of Xamarin, RoboVM was discontinued. RoboVM also has the option to compile Java apps to desktop binaries. I built this binary with the last RoboVM free version (1.8). Now RoboVM is forked in BugVM, but I was not able to build the binary with BugVM.
  • C# compiled with Mono: There is a C# version of Carballo converted with the Sharpen tool. I compiled this binary with MonoDevelop 5.10. The converted code is sub-optimal but it is a good solution if you need a native version (or if you need to integrate Java code in a C# project).

Test results

Rank Name                          ELO   Games   Score   Draws
   1 carballo-1.5-gcj               89    1200     62%     28%
   2 carballo-1.5                   37    1200     55%     27%
   3 carballo-1.5-mono              -5    1200     49%     30%
   4 carballo-1.5-jet              -33    1200     45%     28%
   5 carballo-1.5-robovm           -88    1200     38%     27%

Conclusion

The JVM performance is very good, better than almost all the Java to native solutions.

The exception is GCJ, but it’s incomplete and it will not work for all the Java apps.

I expected better results from Excelsior Jet, as some time ago Carballo Jet binaries where available an used for testing.

The C# version is a bit worse but acceptable.


Running Android apps inside Chrome

I couldn’t believe it when somebody told me: Google is running Android apps in ChromeBooks and some selected apps can be installed from the Chrome Webstore. It works with a Chrome extension that implements an Android virtual machine running over the Native Client (NaCL is a sandbox for running compiled C and C++ code inside Chrome).

Based on this Google work, Vlad Filippov released an unofficial Chrome extension called ARChon (https://github.com/vladikoff/chromeos-apk/blob/master/archon.md) to allow this for all the desktop Chromes (Linux, Mac & Windows). This ARChon extension is a 100MB zip that must be unzipped and loaded into Chrome as an unpacked extension.

Then each Android APK must be converted to an unpacked extension with a Node-JS based tool, chromeos-apk (https://github.com/vladikoff/chromeos-apk) and loaded into Chrome. The generated extension contains the APK and some support files. Once installed, it appears as a regular Chrome app.

I tested all my Android apps and I was very surprised with the results, they load fast and run smooth. Some of my apps did not work because:

  • It does not support Google Play Services
  • It does not support apps using the Android NDK
  • It does not support GL_TEXTURE_CUBE_MAP in OpenGL ES (and my app crashes)

But I consider it SPECTACULAR. This opens a lot of new possibilities for Android, now running inside browsers like Flash, … and confirms my “theory” that Android is only a virtual machine (IMHO, the underlying SO is Linux :P).

You can comment this post in Google+.

chromeos-apk


My Gradle Tips and Tricks 1

gradle_logoThe new Android build system is based in Gradle, an advanced build tool that simplifies build automation.

I used Maven for many years and Gradle project structure is very similar, but Gradle configuration files are Groovy-based and much (much!) more simple. Here is the official documentation

This week I migrated a lot of my Android and GWT projects to Gradle (some of them in my github), and here are some tips that I discovered (and I will add more as I find them):

Embrace the new Source Structure

Initially can be difficult, but putting the code under /src/main/java/, Android resources in /src/main/res/, etc. is a very popular form to organize the code. You can configure the build.gradle file to keep the old Android project structure, but I suggest migrating to the new structure.

Reference Another Project in your Workspace

You have two options:

  1. Use a Multi-Project Build
  2. Install the artifact (JAR if is a standard Java library, AAR if an Android Library) in your local Maven repo

Both options are explained below:

Use Multi-Project Builds

You can create a multi-project setup with projects on subfolders adding a settings.gradle file at the root with a include statement:

include 'library-project', 'your-project'

Then inside a your-project’s build.gradle you can easily add a dependency to another project (called “other_project”):

dependencies {
    compile project(':library-project')
}

Sample here.

Install a JAR to your Local Maven Repo (with the source code)

With standard java libraries and a build.gradle file like:

apply plugin: 'java'
apply plugin: 'maven'
group = 'com.company'
version = '0.8'

You can do “gradle install” to copy it yo your Maven repo.

Manually Install a JAR to your Local Maven Repo (without source code)

At the moment a lot of Google artifacts (=JARs, AARs) are not available at the Maven Central repo, but you can install them manually to your local Maven repo

mvn install:install-file -Dfile=android-support-v4.jar -DgroupId=com.google -DartifactId=android-support-v4 -Dversion=0.1 -Dpackaging=jar

You can also do it with gradle, but it is a bit more complex sample here.

Reference an Artifact from your Local Maven Repository

Be careful to add mavenLocal() to your repositories in the build.gradle:

repositories {
    mavenLocal()
}

Then use a normal reference, for the android-support-v4.jar sample above, it will be:

dependencies {
    compile 'com.google:android-support-v4:0.1'
}

Install an AAR to the Local Maven Repository

AAR is the new package format for Android Libraries. The “gradle install” task (that should install the AAR in the local Maven repository) does not works as expected in those projects, so I use a little hack:

apply plugin: 'maven'
uploadArchives {
  repositories {
    mavenDeployer {
      repository url: 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath
    }
  }
}
task install(dependsOn: uploadArchives)

Usage sample here.

Android Library Projects need an Application Tag

This is an ADT bug, and the workaround is to add an empty application tag yo your library’s AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
    <application />

Include all the JARs in the libs/ Directory

This was the default behaviour of Android Projects, to maintain it you can add to your build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

I suggest referencing local JARs only in final Android projects, referencing them in libraries can cause problems, read below:

Avoid Referencing Local JARs in Android and Java Libraries

If you reference local JARs in Android libraries, those jars are going to be embedded in the AAR. If you reference again the JAR from a project that uses the AAR you will see this build error:

:project:dexDebug
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Landroid/support/v4/app/ActivityCompatHoneycomb;
//..
1 error; aborting
:project:dexDebug FAILED

If an Android Library projects references a JAR library, and the JAR library references local JAR files, those local JAR files are not going to be embedded in the Android projects using the library.

The best solution is to install all the JARs to your local Maven repo as explained above.

Or Embed all the Dependency JARs Into your Library

In some situations you may embed all the JARs in your library, but ONLY if those dependency JARs are not going to be referenced again in a project that uses the library.

jar {
    from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}

Android Projects Need Libraries Compiled with Java 1.6

If in an Android Project you use a library JAR including classes compiled with source compatibility to Java 1.7, build gives this WARNING:

trouble processing:
bad class file magic (cafebabe) or version (0033.0000)
...while parsing Class

…and the class is not included in the APK, so the app force closes at running.

This may be solved adding in the build.gradle of the library:

apply plugin: 'java'
sourceCompatibility = 1.6
targetCompatibility = 1.6

Create a Source JAR for a Library

Source JARs are needed for libraries used in GWT projects. To generate them add this to your build.gradle:

apply plugin: 'maven'
//...
task sourcesJar(type: Jar, dependsOn:classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}
artifacts {
    archives sourcesJar
}

You can install them to your local repo with “gradle install”

Include a Source JAR from Another Project

For multi-project_builds:

dependencies {
    compile project(':other_project')
    compile project(path: ':other_project', configuration: 'archives')
}

Sample here.

Include a Source JAR from a Maven Repo

Simply append “:sources”:

dependencies {
    compile 'com.company:artifact-name:0.8'
    compile 'com.company:artifact-name:0.8:sources'
}

JavaScript as a Runtime

The future is here, and JavaScript (JS) is everywhere, but JS development is so hard that many people prefer to develop in other languages and then compile their code to JS, using JS as a universal runtime. Here are the most interesting options:

GWT

GWT stand for Google Web Toolkit, but now it’s in hands of the community and extensively used in many corporations. GWT compiles Java into JS and it’s strongly optimized. I use it a lot, and I feel very productive using an advanced IDE like Eclipse with tools like code assist, refactor, etc.

https://developers.google.com/web-toolkit/

CoffeeScript

A very compact language, inspired by Ruby and Python and that has become extremely popular in the last years. I’m not very familiar with the “Syntactic sugar” and I’m more productive with traditional languages (yes, I love curly backets! {}).

http://coffeescript.org/

Haxe

If you are an ActionScript developer (Adobe Flash), this is your language. It not only compiles to JS and ActionScript, also to PHP, C++, C#, etc. It’s becoming popular for the development of multi-platform mobile games with NME.

http://www.haxe.org/

Dart

This is a new language for the web pushed by Google. It tries to be a “modern and structured” language for the web that can be run directly into the browser, but to retain compatibility (and to run in other browsers that publicly rejected Dart), it can also be compiled to JS.

http://www.dartlang.org/

List of languages that compile to Js: http://altjs.org/


Java vs C# vs Javascript 5

*** UPDATE: This article had a HUGE mistake in the Javascript part, read below ***

I’m a Java guy but I’m always opened to other programming languages. I heard great things about Mono and the .Net platform, among those things recently I saw a Xamarin article about Android being ported over Mono in a project called XobotOS (http://blog.xamarin.com/2012/05/01/android-in-c-sharp/) and they are getting a much better performance than with Dalvik.

They did it using Sharpen: an automated tool that can convert from Java source code to C#. I was hesitated and started to port my “Carballo” chess engine to C#. Sharpen runs as an eclipse plugin (but outside eclipse). The best tutorial that I found to install sharpen is here: http://www.pauldb.me/post/14916717048/a-guide-to-sharpen-a-great-tool-for-converting-java

The new Carballo Chess Engine with a C# version is now hosted at GitGub (https://github.com/albertoruibal/carballo). You can launch the conversion tool running “ant” on the “csharp/” folder. This C# version hasn’t a graphic interface yet, but is great to make some comparisons…

I use a lot the BT2630 chess test suite, with positions to find the best move, lets see how much time needs my chess engine in Java to find the solution for the first position:

r q . . r . k . 8
. . . . . p p . 7
p . . . . . . . 6
. . . . b N P . 5
. p . . P . . P 4
. . . . . Q . . 3
P P . . . . K . 2
. . . . . R . R 1
a b c d e f g h white move
DEBUG SearchEngine - depth 1 score cp 170 nodes 176 time 42 nps 4093 pv f3b3
DEBUG SearchEngine - depth 2 score cp 136 nodes 1044 time 123 nps 8419 pv f3b3 b8b7
DEBUG SearchEngine - depth 3 score cp 131 nodes 4335 time 358 nps 12075 pv f1f2 b8b6 f3b3
DEBUG SearchEngine - depth 4 score cp 111 nodes 14603 time 854 nps 17079 pv f1f2 b8b6 f3b3 e8e6
DEBUG SearchEngine - depth 5 score cp 115 nodes 27593 time 1311 nps 21031 pv f1f2 b8b7 h1d1 a8d8 f2d2
DEBUG SearchEngine - depth 6 score cp 119 nodes 50536 time 1450 nps 34828 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2
DEBUG SearchEngine - depth 7 score cp 117 nodes 110415 time 1693 nps 65180 pv f1f2 b8b7 h1d1 a8c8 f2d2 b7c6 b2b3
DEBUG SearchEngine - depth 8 score cp 110 nodes 314754 time 2515 nps 125100 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 b7c6 b2b3
DEBUG SearchEngine - depth 9 score cp 112 nodes 564320 time 3580 nps 157587 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 a6a5 b2b3 b7b6
DEBUG SearchEngine - depth 10 score cp 129 nodes 1219488 time 6174 nps 197487 pv f1f2 b8c7 f3b3 c7b7 b3d5 b7d5 e4d5 a8d8 h1d1 e8e6 a2a4
DEBUG SearchEngine - depth 11 score cp 241 nodes 2430413 time 11373 nps 213681 pv f5g7 e8f8 g7f5 a8a7 f5h6 g8g7 f1f2 b8b5 h6f5 g7g8 f3b3 a7d7 h1c1 d7d1

This is 11.373 seconds at 213.681 nodes per second, my java version is “Java(TM) SE Runtime Environment (build 1.6.0_26-b03)”.

And with Mono? let’s see the result on the same machine:

DEBUG SearchEngine - depth 1 score mate 0 nodes 176 time 25 nps 6769 pv f3b3
DEBUG SearchEngine - depth 2 score mate 0 nodes 1044 time 36 nps 28216 pv f3b3 b8b7
DEBUG SearchEngine - depth 3 score mate 0 nodes 4335 time 58 nps 73474 pv f1f2 b8b6 f3b3
DEBUG SearchEngine - depth 4 score mate 0 nodes 14603 time 136 nps 106591 pv f1f2 b8b6 f3b3 e8e6
DEBUG SearchEngine - depth 5 score mate 0 nodes 27593 time 247 nps 111262 pv f1f2 b8b7 h1d1 a8d8 f2d2
DEBUG SearchEngine - depth 6 score mate 0 nodes 50536 time 450 nps 112053 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2
DEBUG SearchEngine - depth 7 score mate 0 nodes 110415 time 813 nps 135644 pv f1f2 b8b7 h1d1 a8c8 f2d2 b7c6 b2b3
DEBUG SearchEngine - depth 8 score mate 0 nodes 314754 time 2058 nps 152867 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 b7c6 b2b3
DEBUG SearchEngine - depth 9 score mate 0 nodes 564320 time 3693 nps 152766 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 a6a5 b2b3 b7b6
DEBUG SearchEngine - depth 10 score mate 0 nodes 1219488 time 7673 nps 158911 pv f1f2 b8c7 f3b3 c7b7 b3d5 b7d5 e4d5 a8d8 h1d1 e8e6 a2a4
DEBUG SearchEngine - depth 11 score mate 0 nodes 2430413 time 15490 nps 156891 pv f5g7 e8f8 g7f5 a8a7 f5h6 g8g7 f1f2 b8b5 h6f5 g7g8 f3b3 a7d7 h1c1 d7d1

That is 15.490 seconds at 156.891 nodes per second with the “Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-3)”.

I didn’t found the Sharp and Mono magic, at least in my case Mono is about a 30% slower than Java. This may be as a result of the automated code conversion…

I tested the GWT part in the GWT Developer Mode. In this mode, the code is run in the server as Java (and not in JS). In recent tests I found that the Javascript version is much more slow, probably because of the GWT Javascript Long emulation (Js does not has 64bits integers).

Well and this chess engine also has a GWT version. GWT is another magical Google product that converts your java code to Javascript. Let’s see how fast runs this test on the browser:

[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 1 score cp 170 nodes 176 time 56 nps 3087 pv f3b3 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 2 score cp 136 nodes 1044 time 186 nps 5582 pv f3b3 b8b7 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 3 score cp 131 nodes 4335 time 483 nps 8956 pv f1f2 b8b6 f3b3 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 4 score cp 111 nodes 14603 time 997 nps 14632 pv f1f2 b8b6 f3b3 e8e6 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 5 score cp 115 nodes 27593 time 1123 nps 24548 pv f1f2 b8b7 h1d1 a8d8 f2d2 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 6 score cp 119 nodes 50536 time 1251 nps 40364 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 7 score cp 117 nodes 110415 time 1571 nps 70238 pv f1f2 b8b7 h1d1 a8c8 f2d2 b7c6 b2b3 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 8 score cp 110 nodes 314754 time 2648 nps 118819 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 b7c6 b2b3 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 9 score cp 112 nodes 564320 time 4063 nps 138858 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 a6a5 b2b3 b7b6 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 10 score cp 129 nodes 1219488 time 7474 nps 163142 pv f1f2 b8c7 f3b3 c7b7 b3d5 b7d5 e4d5 a8d8 h1d1 e8e6 a2a4 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 11 score cp 241 nodes 2430413 time 14216 nps 170951 pv f5g7 e8f8 g7f5 a8a7 f5h6 g8g7 f1f2 b8b5 h6f5 g7g8 f3b3 a7d7 h1c1 d7d1 )

I can’t believe my eyes : 14.216 seconds at 170.951 nodes per second with Google Chome Version 19.0.1084.52… and this is also an automated conversion process…

Yes, in my tests Javascript is faster than Sharpened Mono…


Mobile 2.0 Open Ideas

This year I was invited to organize a workshop at the Mobile 2.0 Open Ideas event at Barcelona, on 16-17 June. With my experience developing Android apps at Mobialia and the social media integration on Martin Varsavsky’s RadioMe, I proposed the workshop:

Bulding Social Media Enabled Android Apps

This will be a 1-hour workshop In which I’ll provide some examples on how to integrate social media on Android apps, from the simple “Share” button to more complex integrations like using Twitter, LinkedIn or Facebook APIs, including logging-in with social media, etc.

I will try to do it interesting for developers and also for app designers who want to add cool social features to their apps. Slides and code samples will be available from the day of the event.


A small problem with the facebook Android SDK

At Mobialia the lasts weeks I was involved on the development of a social app using, among other social networks APIs, the facebook API.

For me it was easier to implement Twitter API access using only the signpost library, for facebook they recommend to download their Android SDK, and I did so. First we need a class attribute with the Facebook object:

Facebook fb = new Facebook(appId);

The problem is during the autentication process: we must call the fb.authorize method…

fb.authorize(this, PERMISSIONS, ACTIVITY_CODE, this);

And wait for the result of this called activity on our onActivityResult method

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    fb.authorizeCallback(requestCode, resultCode, data);
}

But, here is the problem, under heavy memory requierements (like testing on a old HTC Magic) the caller activity may be killed, and the status of the Facebook object is not maintained so the call to authorizeCallback is going to fail.

Then I need a method to mantain the Facebook object status, and here comes the hack. I added this method to the Facebook object:

public void setStatus(int mAuthActivityCode, DialogListener mAuthDialogListener){
    this.mAuthActivityCode = mAuthActivityCode;
    this.mAuthDialogListener = mAuthDialogListener;
}

And I call this method on my onActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    fb.setStatus(ACTIVITY_CODE, this);
    fb.authorizeCallback(requestCode, resultCode, data);
}

Note that the DialogListener is the same Activity. Other solution may be to avoid killing of the caller activity, but I don’t figure how…