From 13f6f26bcfea3625cf789e528ba7abcd1dde6788 Mon Sep 17 00:00:00 2001 From: yshubin Date: Sun, 16 Aug 2015 23:04:12 +0400 Subject: [PATCH 1/3] optimization #1 --- src/com/activeandroid/Model.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/com/activeandroid/Model.java b/src/com/activeandroid/Model.java index 421426ea3..4ba360644 100644 --- a/src/com/activeandroid/Model.java +++ b/src/com/activeandroid/Model.java @@ -79,7 +79,9 @@ public final Long save() { final String fieldName = mTableInfo.getColumnName(field); Class fieldType = field.getType(); - field.setAccessible(true); + if (!field.isAccessible()) { + field.setAccessible(true); + } try { Object value = field.get(this); @@ -192,7 +194,9 @@ public final void loadFromCursor(Cursor cursor) { continue; } - field.setAccessible(true); + if (!field.isAccessible()) { + field.setAccessible(true); + } try { boolean columnIsNull = cursor.isNull(columnIndex); From bbd742956905ad76c3598f8d4e3fa12691469e7b Mon Sep 17 00:00:00 2001 From: yshubin Date: Sun, 16 Aug 2015 23:06:04 +0400 Subject: [PATCH 2/3] optimization #2 --- src/com/activeandroid/Model.java | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/com/activeandroid/Model.java b/src/com/activeandroid/Model.java index 4ba360644..d64ecb9f1 100644 --- a/src/com/activeandroid/Model.java +++ b/src/com/activeandroid/Model.java @@ -30,7 +30,9 @@ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; @SuppressWarnings("unchecked") public abstract class Model { @@ -46,6 +48,8 @@ public abstract class Model { private final TableInfo mTableInfo; private final String idName; + private static Map> columnIndexesCache = new HashMap>(); + ////////////////////////////////////////////////////////////////////////////////////// // CONSTRUCTORS ////////////////////////////////////////////////////////////////////////////////////// @@ -185,10 +189,23 @@ public final void loadFromCursor(Cursor cursor) { * when the cursor have multiple columns with same name obtained from join tables. */ List columnsOrdered = new ArrayList(Arrays.asList(cursor.getColumnNames())); + List columnIndexes = columnIndexesCache.get(mTableInfo.getTableName()); + if (columnIndexes == null) { + columnIndexes = new ArrayList(); + columnIndexesCache.put(mTableInfo.getTableName(), columnIndexes); + } + int counter = 0; for (Field field : mTableInfo.getFields()) { - final String fieldName = mTableInfo.getColumnName(field); Class fieldType = field.getType(); - final int columnIndex = columnsOrdered.indexOf(fieldName); + + final int columnIndex; + if (columnIndexes.size() <= counter) { + String fieldName = mTableInfo.getColumnName(field); + columnIndex = columnsOrdered.indexOf(fieldName); + columnIndexes.add(columnIndex); + } else { + columnIndex = columnIndexes.get(counter); + } if (columnIndex < 0) { continue; @@ -268,6 +285,8 @@ else if (ReflectionUtils.isSubclassOf(fieldType, Enum.class)) { if (value != null) { field.set(this, value); } + + counter++; } catch (IllegalArgumentException e) { Log.e(e.getClass().getName(), e); From 80f7397cf600b14f580c2db86feab3e8853ec39d Mon Sep 17 00:00:00 2001 From: yshubin Date: Sun, 16 Aug 2015 23:06:37 +0400 Subject: [PATCH 3/3] optimization #3 --- src/com/activeandroid/Model.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/com/activeandroid/Model.java b/src/com/activeandroid/Model.java index d64ecb9f1..c34706976 100644 --- a/src/com/activeandroid/Model.java +++ b/src/com/activeandroid/Model.java @@ -49,6 +49,7 @@ public abstract class Model { private final TableInfo mTableInfo; private final String idName; private static Map> columnIndexesCache = new HashMap>(); + private static Map> fieldTypesCache = new HashMap>(); ////////////////////////////////////////////////////////////////////////////////////// // CONSTRUCTORS @@ -194,17 +195,25 @@ public final void loadFromCursor(Cursor cursor) { columnIndexes = new ArrayList(); columnIndexesCache.put(mTableInfo.getTableName(), columnIndexes); } + List fieldTypes = fieldTypesCache.get(mTableInfo.getTableName()); + if (fieldTypes == null) { + fieldTypes = new ArrayList(); + fieldTypesCache.put(mTableInfo.getTableName(), fieldTypes); + } int counter = 0; for (Field field : mTableInfo.getFields()) { - Class fieldType = field.getType(); - final int columnIndex; + Class fieldType; if (columnIndexes.size() <= counter) { String fieldName = mTableInfo.getColumnName(field); columnIndex = columnsOrdered.indexOf(fieldName); columnIndexes.add(columnIndex); + + fieldType = field.getType(); + fieldTypes.add(fieldType); } else { columnIndex = columnIndexes.get(counter); + fieldType = fieldTypes.get(counter); } if (columnIndex < 0) {