Don't know why the patch didn't get through, sending again...

===

diff -ur a\System.Data.SQLite\SQLiteDataReader.cs 
b\System.Data.SQLite\SQLiteDataReader.cs
--- a\System.Data.SQLite\SQLiteDataReader.cs    Sat Aug 11 16:40:46 2012
+++ b\System.Data.SQLite\SQLiteDataReader.cs    Fri Oct 05 13:47:16 2012
@@ -48,6 +48,10 @@
     /// </summary>
     private int _fieldCount;
     /// <summary>
+    /// Names of fields (columns) in the row-returning statement currently 
being processed
+    /// </summary>
+    private string[] _fieldNames;
+    /// <summary>
     /// Datatypes of active fields (columns) in the current statement, used 
for type-restricting data
     /// </summary>
     private SQLiteType[] _fieldTypeArray;
@@ -208,6 +212,7 @@
         _command = null;
         _activeStatement = null;
         _fieldTypeArray = null;
+        _fieldNames = null;
       }
       finally
       {
@@ -643,7 +648,13 @@
       CheckClosed();
       SQLiteCommand.Check(_command);
- int r = _activeStatement._sql.ColumnIndex(_activeStatement, name);
+      int r = -1;
+#if !PLATFORM_COMPACTFRAMEWORK
+      if (_fieldNames != null)
+        r = Array.FindIndex(_fieldNames, delegate(string x) { return 
x.Equals(name, StringComparison.OrdinalIgnoreCase); });
+      if (r == -1)
+#endif
+        r = _activeStatement._sql.ColumnIndex(_activeStatement, name);
       if (r == -1 && _keyInfo != null)
       {
         r = _keyInfo.GetOrdinal(name);
@@ -1191,6 +1202,7 @@
         _activeStatementIndex++;
fieldCount = stmt._sql.ColumnCount(stmt);
+        _fieldNames = null;
// If the statement is not a select statement or we're not retrieving schema only, then perform the initial step
         if ((_commandBehavior & CommandBehavior.SchemaOnly) == 0 || fieldCount 
== 0)
@@ -1216,6 +1228,9 @@
         _activeStatement = stmt;
         _fieldCount = fieldCount;
         _fieldTypeArray = null;
+        _fieldNames = new string[fieldCount];
+        for (int i=0; i<fieldCount; i++)
+            _fieldNames[i] = GetName(i);
if ((_commandBehavior & CommandBehavior.KeyInfo) != 0)
           LoadKeyInfo();


===
Ashish Kulkarni wrote:
Hello,

I've attached a patch which caches the column names in SQLiteDataReader.
During profiling a scenario where more than 100-200 records are read and
columns are accessed by name and not by index -- well over 50% of the time
was spent in System.Data.SQLite.SQLite3.ColumnIndex(). The attached patch
caches the field names in the data reader (using the same semantics) which
speeds up retrieval of records by a large margin.

Thanks,
Ashish

diff -ur a\System.Data.SQLite\SQLiteDataReader.cs 
b\System.Data.SQLite\SQLiteDataReader.cs
--- a\System.Data.SQLite\SQLiteDataReader.cs    Sat Aug 11 16:40:46 2012
+++ b\System.Data.SQLite\SQLiteDataReader.cs    Fri Oct 05 13:47:16 2012
@@ -48,6 +48,10 @@
     /// </summary>
     private int _fieldCount;
     /// <summary>
+    /// Names of fields (columns) in the row-returning statement currently 
being processed
+    /// </summary>
+    private string[] _fieldNames;
+    /// <summary>
     /// Datatypes of active fields (columns) in the current statement, used 
for type-restricting data
     /// </summary>
     private SQLiteType[] _fieldTypeArray;
@@ -208,6 +212,7 @@
         _command = null;
         _activeStatement = null;
         _fieldTypeArray = null;
+        _fieldNames = null;
       }
       finally
       {
@@ -643,7 +648,13 @@
       CheckClosed();
       SQLiteCommand.Check(_command);
 
-      int r = _activeStatement._sql.ColumnIndex(_activeStatement, name);
+      int r = -1;
+#if !PLATFORM_COMPACTFRAMEWORK
+      if (_fieldNames != null)
+        r = Array.FindIndex(_fieldNames, delegate(string x) { return 
x.Equals(name, StringComparison.OrdinalIgnoreCase); });
+      if (r == -1)
+#endif
+        r = _activeStatement._sql.ColumnIndex(_activeStatement, name);
       if (r == -1 && _keyInfo != null)
       {
         r = _keyInfo.GetOrdinal(name);
@@ -1191,6 +1202,7 @@
         _activeStatementIndex++;
 
         fieldCount = stmt._sql.ColumnCount(stmt);
+        _fieldNames = null;
 
         // If the statement is not a select statement or we're not retrieving 
schema only, then perform the initial step
         if ((_commandBehavior & CommandBehavior.SchemaOnly) == 0 || fieldCount 
== 0)
@@ -1216,6 +1228,9 @@
         _activeStatement = stmt;
         _fieldCount = fieldCount;
         _fieldTypeArray = null;
+        _fieldNames = new string[fieldCount];
+        for (int i=0; i<fieldCount; i++)
+            _fieldNames[i] = GetName(i);
 
         if ((_commandBehavior & CommandBehavior.KeyInfo) != 0)
           LoadKeyInfo();
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to