tikxml icon indicating copy to clipboard operation
tikxml copied to clipboard

Optimize generated Code for single attribute

Open sockeqwe opened this issue 9 years ago • 6 comments

If there is just one single attribute we don't have to allocate an extra HashMap. In this case we can just use a simple if statement

sockeqwe avatar Jan 22 '16 11:01 sockeqwe

It will be good idea to get rid of HashMap and use switch or if (in single attribute case) in fromXml section if attribute has no @Path annotation. It will speed up reading and save some memory.

WeaponMan avatar Aug 31 '18 13:08 WeaponMan

For single attribute cases yes! for others I think we have to measure if switch is really faster / slower than a hashmap. It also depends on Android runtime as it does some optimization on run time.

In theory switch on Strings is O(n) whereas HashMap is O(1). However, compiler might transform switch to HashMap on compile time anyway but this depends on compiler optimizations and jdk if I remember correctly. In general i think HashMap vs. switch is a micro optimizaion and we could tackle that later. Single Attributes is an optimization (not micro optimizaion) for sure and we gain much more from optimising this compared to HashMap vs. switch.

What do you think?

sockeqwe avatar Aug 31 '18 15:08 sockeqwe

I have done some benchmarks with jmh on openjdk 1.8.0_152 linux-x64 with patches from intellij. Turns out switch is actually cheaper than key lookup and calling method on interface, but I can upload jmh bechmark to github and you can see yourself.

WeaponMan avatar Aug 31 '18 15:08 WeaponMan

would be interesting to run those benchmarks on android device (not sure if its possible though). Also related to #37 and #78

sockeqwe avatar Aug 31 '18 16:08 sockeqwe

Not possible with jmh, but I can create new benchmark with Caliper.

WeaponMan avatar Aug 31 '18 16:08 WeaponMan

Benchmark code can be found here First run results on Nokia 6.1 with android 8.1 here and another one with 240 trials here

WeaponMan avatar Aug 31 '18 17:08 WeaponMan