Sorry, that got a bit munged.  I've attached a file with the patch.

Thanks,
Eric


On 02/03/2016 03:47 PM, Eric Faulhaber wrote:
Hello,

I use H2 as an embedded, in-memory database, exclusively for temp tables.  I would like to contribute the following small patch, which has improved the throughput of certain use cases in our system by 18-30%.

diff --git a/h2/src/main/org/h2/engine/Session.java b/h2/src/main/org/h2/engine/Session.java
index 215d814..4161c72 100644
--- a/h2/src/main/org/h2/engine/Session.java
+++ b/h2/src/main/org/h2/engine/Session.java
@@ -928,11 +928,13 @@ public class Session extends SessionWithState {
     private void cleanTempTables(boolean closeSession) {
         if (localTempTables != null && localTempTables.size() > 0) {
             synchronized (database) {
-                for (Table table : New.arrayList(localTempTables.values())) {
+                Iterator<Table> itr = localTempTables.values().iterator();
+                while (itr.hasNext()) {
+                    Table table = itr.next();
                     if (closeSession || table.getOnCommitDrop()) {
                         modificationId++;
                         table.setModified();
- localTempTables.remove(table.getName());
+                        itr.remove();
table.removeChildrenAndResources(this);
                         if (closeSession) {
                             // need to commit, otherwise recovery might

Specifically, the call to New.arrayList(localTempTables.values()) at the top of the for loop was the bottleneck that stood out while profiling our system.  I imagine it originally was written this way to avoid a ConcurrentModificationException, but using the Iterator.remove() call does the same thing much more efficiently.

This is based on the code in GitHub as of last night.  It is just copied from git's diff output.  If you need me to submit this in another form, please let me know.

Thanks for producing and sharing a great project!

Best regards,
Eric


--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.
diff --git a/h2/src/main/org/h2/engine/Session.java b/h2/src/main/org/h2/engine/Session.java
index 215d814..4161c72 100644
--- a/h2/src/main/org/h2/engine/Session.java
+++ b/h2/src/main/org/h2/engine/Session.java
@@ -928,11 +928,13 @@ public class Session extends SessionWithState {
     private void cleanTempTables(boolean closeSession) {
         if (localTempTables != null && localTempTables.size() > 0) {
             synchronized (database) {
-                for (Table table : New.arrayList(localTempTables.values())) {
+                Iterator<Table> itr = localTempTables.values().iterator();
+                while (itr.hasNext()) {
+                    Table table = itr.next();
                     if (closeSession || table.getOnCommitDrop()) {
                         modificationId++;
                         table.setModified();
-                        localTempTables.remove(table.getName());
+                        itr.remove();
                         table.removeChildrenAndResources(this);
                         if (closeSession) {
                             // need to commit, otherwise recovery might

Reply via email to