Permalink
Browse files

KeyValueStorage optimize

  • Loading branch information...
1 parent 92ae3ab commit 63e9a94c0647c67019e70e9996e396abfdc0b6c5 @jindw committed Apr 13, 2014
View
@@ -131,4 +131,6 @@ Description
* [中文版](doc/README_zh.md)
* [English Version](doc/README_en.md)
+#### Contact
+ * QQ群: 293775669
View
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
-<project name="AndroidUIO" default="release">
+<project name="AndroidUIO" default="build-jar">
<!-- The local.properties file is created and updated by the 'android' tool.
It contains the path to the SDK. It should *NOT* be checked into
@@ -78,6 +78,17 @@
<include name="android/support/v4/**" />
</fileset>
</jar>
+ <jar destfile="bin/UIO.jar">
+ <zipfileset src="libs/JSEL.jar" includes="**"/>
+ <fileset dir="bin/classes">
+ <include name="org/xidea/android/*.java"/>
+ </fileset>
+ <fileset dir="bin/classes">
+ <include name="org/xidea/android/**" />
+ <exclude name="**/test/**" />
+ <exclude name="org/robolectric/**"/>
+ </fileset>
+ </jar>
</target>
@@ -2,5 +2,5 @@
package org.xidea.androidbase;
public final class BuildConfig {
- public final static boolean DEBUG = true;
+ public final static boolean DEBUG = false;
}
@@ -5,7 +5,6 @@
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.HashMap;
-import java.util.IdentityHashMap;
import org.xidea.android.KeyValueStorage;
import org.xidea.android.KeyValueStorage.DefaultValue;
@@ -23,8 +22,7 @@
@DefaultValue
@KeyValueStorage.StorageKey
public class KeyValueStorageImpl {
- private static final CommonLog log = CommonLog
- .getLog();
+ private static final CommonLog log = CommonLog.getLog();
private static final JSONDecoder JSON_DECODER = new JSONDecoder(false);
private static final DefaultValue DEFAULT_VALUE = KeyValueStorageImpl.class
.getAnnotation(DefaultValue.class);
@@ -44,7 +42,7 @@
@Override
public Object invoke(Object thiz, Method method,
Object[] args) throws Throwable {
- //log.timeStart();
+ // log.timeStart();
if (args == null) {// alliyun bug
args = EMPTY_OBJECTS;
}
@@ -53,15 +51,15 @@ public Object invoke(Object thiz, Method method,
if (inv == null) {
inv = KeyValueStorageImpl.buildInvocable(type,
method, preferences, editorHolder);
- //System.err.println("create:"+method+"@"+System.identityHashCode(method)+(null==inv));
+ // System.err.println("create:"+method+"@"+System.identityHashCode(method)+(null==inv));
if (inv == null) {
return null;
} else {
impls.put(name, inv);
}
}
Object value = inv.invoke(thiz, args);
- //log.timeEnd("keyValue."+method+value);
+ // log.timeEnd("keyValue."+method+value);
return value;
}
});
@@ -73,9 +71,34 @@ public static Invocable buildInvocable(final Class<?> type, Method method,
Type returnType = method.getGenericReturnType();
String name = method.getName();
- Class<?> declaringClass = method.getDeclaringClass();
Class<?>[] paramsTypes = method.getParameterTypes();
- if (name.equals("toString") && paramsTypes.length == 0) {
+
+ // long t1 = System.nanoTime();
+ String key = name.replaceAll("^(?:get|set|is)([A-Z])", "$1");
+ if (!name.equals(key)) {
+ key = Character.toLowerCase(key.charAt(0)) + key.substring(1);
+ switch (name.charAt(0)) {
+ case 'g':// (name.startsWith("get")) {
+ if (paramsTypes.length == 0) {
+ return buildGetter(sharedPreferences, method, returnType,
+ paramsTypes, key);
+ }
+ break;
+ case 'i':// }else if (name.startsWith("is")) {
+ if (returnType == Boolean.class || returnType == Boolean.TYPE) {
+ name = Character.toLowerCase(name.charAt(2))
+ + name.substring(3);
+ return buildGetter(sharedPreferences, method, returnType,
+ paramsTypes, key);
+ }
+ break;
+ default:// case 's':// } else if (name.startsWith("set")
+ if (paramsTypes.length == 1) {
+ return buildSetter(sharedPreferences, editorHolder,
+ returnType != Void.TYPE, paramsTypes[0], key);
+ }
+ }
+ } else if (name.equals("toString") && paramsTypes.length == 0) {
return new Invocable() {
String label = type.getName() + "&"
+ sharedPreferences.toString();
@@ -85,36 +108,15 @@ public Object invoke(Object thiz, Object... args)
throws Exception {
return label;
}
- };
- } else if (declaringClass == KeyValueStorage.class) {
- Invocable inv = buildKeyValue(sharedPreferences, editorHolder,
- returnType, paramsTypes);
+ };//
+ } else if (method.getDeclaringClass() == KeyValueStorage.class) {
+ Invocable inv = buildStorage3Invocable(sharedPreferences,
+ editorHolder, returnType, paramsTypes);
if (inv != null) {
return inv;
}
- } else {
- // long t1 = System.nanoTime();
- String key = name.replaceAll("^(?:get|set|is)([A-Z])", "$1");
- if (!name.equals(key)) {
- key = Character.toLowerCase(key.charAt(0)) + key.substring(1);
- if (name.startsWith("is")) {
- if (returnType == Boolean.class
- || returnType == Boolean.TYPE) {
- name = Character.toLowerCase(name.charAt(2))
- + name.substring(3);
- return buildGetter(sharedPreferences, method,
- returnType, paramsTypes, key);
- }
- } else if (name.startsWith("set") && paramsTypes.length == 1) {
- return buildSetter(sharedPreferences, editorHolder,
- returnType!= Void.TYPE,paramsTypes[0], key);
- } else if (name.startsWith("get")) {
- return buildGetter(sharedPreferences, method, returnType,
- paramsTypes, key);
- }
- }
-
}
+
return new Invocable() {
@Override
public Object invoke(Object thiz, Object... args) throws Exception {
@@ -125,7 +127,8 @@ public Object invoke(Object thiz, Object... args) throws Exception {
}
private static Invocable buildSetter(final SharedPreferences preferences,
- final Editor[] editorHolder,final boolean returnThis, Class<?> type, final String name) {
+ final Editor[] editorHolder, final boolean returnThis,
+ Class<?> type, final String name) {
final Type valueType = toWrapperType(type);
return new Invocable() {
@Override
@@ -161,9 +164,9 @@ public Object invoke(Object thiz, Object... args) throws Exception {
if (selfTransaction) {
editor.commit();
}
- if(returnThis){
+ if (returnThis) {
return thiz;
- }else{
+ } else {
return null;
}
}
@@ -221,14 +224,18 @@ public Object invoke(Object thiz, Object... args) throws Exception {
}
}
- private synchronized Object defaultValue(Object[] args) {
- if (defaultValue == null) {
- return args[0];
- } else if (!hasSharedValue) {
+ private Object defaultValue(Object[] args) {
+ if (!hasSharedValue) {
+ if (defaultValue == null) {
+ return args[0];
+ }
double value = defaultValue.value();
- hasSharedValue = true;
- if (valueType instanceof Class && Enum.class.isAssignableFrom((Class<?>) valueType)) {
- sharedDefaultValue = defaultValue == null ? null : ReflectUtil.getEnum((int)value,(Class<?>) valueType);
+ if (valueType instanceof Class
+ && Enum.class
+ .isAssignableFrom((Class<?>) valueType)) {
+ sharedDefaultValue = defaultValue == null ? null
+ : ReflectUtil.getEnum((int) value,
+ (Class<?>) valueType);
} else {
String jsonValue = defaultValue.jsonValue();
if ("null".equals(jsonValue)) {
@@ -246,16 +253,18 @@ private synchronized Object defaultValue(Object[] args) {
} else {
Object obj = JSON_DECODER.decode(jsonValue,
valueType);
- if (valueType == String.class || obj == null || obj instanceof Number || obj instanceof Boolean) {
+ if (valueType == String.class || obj == null
+ || obj instanceof Number
+ || obj instanceof Boolean) {
sharedDefaultValue = obj;
} else {
- hasSharedValue = false;
+ return obj;
}
- return obj;
+
}
}
+ this.hasSharedValue = true;
}
-
return sharedDefaultValue;
}
};
@@ -268,44 +277,26 @@ private static Type toWrapperType(Type rt) {
return rt;
}
- private static Invocable buildKeyValue(final SharedPreferences sp,
- final Editor[] editorHolder, Type rt, Class<?>[] ps) {
- synchronized (sp) {
- if (rt == SharedPreferences.class) {// getSharedPrefrences
- return new Invocable() {
- @Override
- public Object invoke(Object thiz, Object... args)
- throws Exception {
- return sp;
- }
- };
- } else if (rt == Void.TYPE && ps.length == 0) {// ("commit".equals(name))
- // {
- return new Invocable() {
- @Override
- public Object invoke(Object thiz, Object... args)
- throws Exception {
- if (editorHolder[0] != null) {
- editorHolder[0].commit();
- editorHolder[0] = null;
- }
- return null;
+ private static Invocable buildStorage3Invocable(final SharedPreferences sp,
+ final Editor[] editorHolder, final Type rt, final Class<?>[] ps) {
+ return new Invocable() {
+ @Override
+ public Object invoke(Object thiz, Object... args) throws Exception {
+ if (rt == SharedPreferences.class) {// getSharedPrefrences
+ return sp;
+ } else if (rt == Void.TYPE) {// ("commit".equals(name))
+ if (editorHolder[0] != null) {
+ editorHolder[0].commit();
+ editorHolder[0] = null;
}
- };
- } else {// if
- // ("beginTransaction".equals(name))
- // {
- return new Invocable() {
- @Override
- public Object invoke(Object thiz, Object... args)
- throws Exception {
- if (editorHolder[0] == null) {
- editorHolder[0] = sp.edit();
- }
- return null;
+ return null;
+ } else {// if( T "beginTransaction".equals(name))
+ if (editorHolder[0] == null) {
+ editorHolder[0] = sp.edit();
}
- };
+ return null;
+ }
}
- }
+ };
}
}
@@ -11,6 +11,7 @@
import android.app.Activity;
import android.app.Application;
+import android.content.SharedPreferences;
import org.robolectric.RobolectricTestRunner;
@@ -57,4 +58,89 @@ public void test() {
}
+ public void test(Application app) {
+ // UIO.init(app);
+ SharedPreferences sp = app.getSharedPreferences("test", 0);
+ sp.edit().putInt("a", 1).commit();
+ //sp = app.getSharedPreferences("test", 0);
+ TestConfig config = UIO.getKeyValueStorage(TestConfig.class);
+ //int v = config.getInt();
+
+ HashMap<Object, Object> map = new HashMap<Object, Object>();
+ for (int k = 128; k > 0; k--) {
+ map.put(k, k);
+ }
+
+ int G = 10000;
+ for (int i = 10; i > 0; i--) {
+
+ long timeUsed_get = 0, timeUsed_get_set = 0, timeUsed_set = 0,timeUsed_native_get=0,timeUsed_native_get2=0,timeUsed_native_put=0;
+
+ long t1 = System.nanoTime();
+
+for(int x= G;x>0;x--){
+ config.getInt();
+ config.getInt();
+ config.getInt();
+ config.getInt();
+ config.getInt();
+ config.getInt();
+ config.getInt();
+ config.getInt();
+ config.getInt();
+ config.getInt();
+ }
+
+
+ timeUsed_get -= (t1 - (t1 = System.nanoTime()));
+
+ config.setInt(config.getInt());
+ timeUsed_get_set -= (int) (t1 - (t1 = System.nanoTime()));
+ config.setInt(12);
+ timeUsed_set -= (int) (t1 - (t1 = System.nanoTime()));
+
+for(int x= G;x>0;x--){
+ sp.getInt("a", -1);
+ sp.getInt("a", -1);
+ sp.getInt("a", -1);
+ sp.getInt("a", -1);
+ sp.getInt("a", -1);
+ sp.getInt("a", -1);
+ sp.getInt("a", -1);
+ sp.getInt("a", -1);
+ sp.getInt("a", -1);
+ sp.getInt("a", -1);
+}
+
+ timeUsed_native_get -= (t1 - (t1=System.nanoTime()));
+ sp.getInt("a", -1);
+ timeUsed_native_get2 -= (int) (t1 - (t1=System.nanoTime()));
+ sp.edit().putInt("a", -1).commit();
+ timeUsed_native_put -= (int) (t1 - (t1=System.nanoTime()));
+
+
+ System.err.println("\r\n----");
+ System.err.println("get_set:\t"+timeUsed_get_set);// / 1000d / 1000);
+ System.err.println("get_only:\t"+timeUsed_get);// / 1000d / 1000);
+ System.err.println("set_only:\t"+timeUsed_set);// / 1000d / 1000);
+ System.err.println("raw get:\t"+timeUsed_native_get);// / 1000d / 1000);
+ System.err.println("raw get2:\t"+timeUsed_native_get2);// / 1000d / 1000);
+ System.err.println("raw put:\t"+timeUsed_native_put);// / 1000d / 1000);
+
+ System.err.println("get rate:"+(timeUsed_get*1.0/timeUsed_native_get));
+ System.err.println("put rate:"+(timeUsed_set*1.0/timeUsed_native_put));
+ }
+
+ /*
+ * ( 3973): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
+ * W/System.err( 3973): 202.484135 W/System.err( 3973):
+ * 1.9226079999999999 W/System.err( 3973): 113.159178 W/System.err(
+ * 3973): 27.313233
+ */
+ // assertTrue("第一次不能跑的太慢啊,最多给你5毫秒!!",timeUsed0<1000*1000*1000*5);
+ // assertTrue("后续就更不能慢了,不能跑的太慢啊!!",timeUsed1<timeUsed &&
+ // timeUsed2<timeUsed);
+
+ }
+
}

0 comments on commit 63e9a94

Please sign in to comment.