some more strategies

18 views
Skip to first unread message

Kasper Sandberg

unread,
Apr 25, 2012, 6:43:42 PM4/25/12
to objene...@googlegroups.com
Hello.

I just noticed that google's gson library has something i dont see in objenesis. it appears that there is a sun.misc.Unsafe that can be used to allocate a new instance aswell. Did you already know about this, and simply didnt use it since its not as good, or?

it also contains strategies for android dalvik, it appears one is what objenesis already has, the other appears to be new aswell..

http://google-gson.googlecode.com/svn/trunk/gson/src/main/java/com/google/gson/internal/UnsafeAllocator.java

I find these different methods interesting to study, so if any of you have information on other strategies, and comments on whether they should be used, and benefits/drawbacks, i'd like to hear it.

furthermore, i think i can offer to implement strategies if you provide link to documentations of them, or examples etc, and provide a patch.


-- 
Kasper Sandberg
Sandberg Enterprises
+45 51944242
http://www.sandbergenterprises.dk

Nate

unread,
Apr 26, 2012, 12:55:03 PM4/26/12
to objene...@googlegroups.com
I tried out Unsafe, first via reflection like the linked code, but when that was slow I tried this:
http://pastebin.com/pZLTKEG1

Here is my benchmark code:
      int count = 1024 * 1024 * 10;
      while(true) {
         ArrayList moo = new ArrayList();
         long s = System.nanoTime();
         for(int i = 0; i < count; i++)
            moo.add(o.newInstance());
         long e = System.nanoTime();
         System.out.println((e - s) / 1e6f);
         moo.clear();
         System.gc();
         System.gc();
      }

Running on Java 7u3 with the server VM, for Unsafe I see ~186ms and for ReflectionFactory I see ~79ms. It seems Unsafe isn't worthwhile.

Attached is a patch to add support for Android, however it is UNTESTED. The part that needs testing is in StdInstantiatorStrategy. I know the JVM_NAME check is fine, but I'm not sure about the VM_VERSION check. We need to check for Android 2.3+ (which is Gingerbread and up). If someone could test it, that would be great. Otherwise I imagine it is fine to check in because even if it doesn't work, Objenesis didn't work on Android before these changes anyway. :)

-Nate
objenesis-android.patch

Henri Tremblay

unread,
Apr 26, 2012, 2:10:20 PM4/26/12
to objene...@googlegroups.com
Thanks for the patch in the analysis.

Henri Tremblay

unread,
Oct 6, 2013, 7:53:49 AM10/6/13
to objene...@googlegroups.com
Hi,

The support for Android was added and also the UnsafeInstantiator which is the fallback when everything else fails. I had the same results as you, the ReflectionFactory is way faster to I kept it for hotspot.

Thanks a lot for your feedback,
Henri
Reply all
Reply to author
Forward
0 new messages