[armedbear-cvs] r11998 - trunk/abcl/src/org/armedbear/lisp

Erik Huelsmann ehuelsmann at common-lisp.net
Sat Jun 6 17:47:06 UTC 2009


Author: ehuelsmann
Date: Sat Jun  6 13:46:59 2009
New Revision: 11998

Log:
Add a cache for opened '.zip' files.

Before this change, consecutive calls to
loadCompiledFunction() would open the same
zip file over and over.

Added:
   trunk/abcl/src/org/armedbear/lisp/ZipCache.java   (contents, props changed)
Modified:
   trunk/abcl/src/org/armedbear/lisp/Lisp.java
   trunk/abcl/src/org/armedbear/lisp/Load.java

Modified: trunk/abcl/src/org/armedbear/lisp/Lisp.java
==============================================================================
--- trunk/abcl/src/org/armedbear/lisp/Lisp.java	(original)
+++ trunk/abcl/src/org/armedbear/lisp/Lisp.java	Sat Jun  6 13:46:59 2009
@@ -1075,7 +1075,7 @@
                               zipFileName = zipFileName.substring(1);
                           }
                         zipFileName = URLDecoder.decode(zipFileName, "UTF-8");
-                        ZipFile zipFile = new ZipFile(zipFileName);
+                        ZipFile zipFile = ZipCache.getZip(zipFileName);
                         try
                           {
                             ZipEntry entry = zipFile.getEntry(entryName);
@@ -1089,7 +1089,7 @@
                           }
                         finally
                           {
-                            zipFile.close();
+                            ZipCache.removeZip(zipFile.getName());
                           }
                       }
                   }
@@ -1139,7 +1139,7 @@
       {
         LispObject loadTruename = Symbol.LOAD_TRUENAME.symbolValue(thread);
         String zipFileName = ((Pathname)loadTruename).getNamestring();
-        ZipFile zipFile = new ZipFile(zipFileName);
+        ZipFile zipFile = ZipCache.getZip(zipFileName);
         try
           {
             ZipEntry entry = zipFile.getEntry(namestring);
@@ -1155,7 +1155,7 @@
           }
         finally
           {
-            zipFile.close();
+            ZipCache.removeZip(zipFile.getName());
           }
       }
     catch (Throwable t)
@@ -1197,7 +1197,6 @@
     public static final LispObject loadCompiledFunction(byte[] bytes) throws Throwable {
         Class c = (new JavaClassLoader()).loadClassFromByteArray(null, bytes, 0, bytes.length);
         if (c != null) {
-            Class sc = c.getSuperclass();
             Constructor constructor = c.getConstructor((Class[])null);
             LispObject obj = (LispObject) constructor.newInstance((Object[])null);
             if (obj instanceof Function) {

Modified: trunk/abcl/src/org/armedbear/lisp/Load.java
==============================================================================
--- trunk/abcl/src/org/armedbear/lisp/Load.java	(original)
+++ trunk/abcl/src/org/armedbear/lisp/Load.java	Sat Jun  6 13:46:59 2009
@@ -123,7 +123,7 @@
         if (checkZipFile(file))
         {
             try {
-                zipfile = new ZipFile(file);
+                zipfile = ZipCache.getZip(file.getPath());
             }
             catch (Throwable t) {
                 // Fall through.
@@ -175,7 +175,7 @@
         finally {
             if (in != null) {
                 try {
-                    in.close();
+                   in.close();
                 }
                 catch (IOException e) {
                     return error(new LispError(e.getMessage()));
@@ -183,7 +183,7 @@
             }
             if (zipfile != null) {
                 try {
-                    zipfile.close();
+                    ZipCache.removeZip(zipfile.getName());
                 }
                 catch (IOException e) {
                     return error(new LispError(e.getMessage()));
@@ -265,7 +265,7 @@
                         String ext = getExtension(s);
                         if (ext.equalsIgnoreCase(".abcl")) {
                             try {
-                                zipfile = new ZipFile(file);
+                                zipfile = ZipCache.getZip(file.getPath());
                                 String name = file.getName();
                                 int index = name.lastIndexOf('.');
                                 Debug.assertTrue(index >= 0);
@@ -338,7 +338,7 @@
             finally {
                 if (zipfile != null) {
                     try {
-                        zipfile.close();
+                        ZipCache.removeZip(zipfile.getName());
                     }
                     catch (IOException e) {
                         return error(new LispError(e.getMessage()));

Added: trunk/abcl/src/org/armedbear/lisp/ZipCache.java
==============================================================================
--- (empty file)
+++ trunk/abcl/src/org/armedbear/lisp/ZipCache.java	Sat Jun  6 13:46:59 2009
@@ -0,0 +1,82 @@
+/*
+ * ZipCache.java
+ *
+ * Copyright (C) 2009 Erik Huelsmann
+ * 
+ * $Id$
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * As a special exception, the copyright holders of this library give you
+ * permission to link this library with independent modules to produce an
+ * executable, regardless of the license terms of these independent
+ * modules, and to copy and distribute the resulting executable under
+ * terms of your choice, provided that you also meet, for each linked
+ * independent module, the terms and conditions of the license of that
+ * module.  An independent module is a module which is not derived from
+ * or based on this library.  If you modify this library, you may extend
+ * this exception to your version of the library, but you are not
+ * obligated to do so.  If you do not wish to do so, delete this
+ * exception statement from your version.
+ */
+
+package org.armedbear.lisp;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.zip.ZipFile;
+
+/**
+ *
+ * @author Erik
+ */
+class ZipCache {
+
+    static Map<String, Entry> zips = new HashMap<String, Entry>();
+    
+    synchronized static ZipFile getZip(String name) throws IOException {
+        Entry zip = zips.get(name);
+        
+        if (zip == null)
+            zips.put(name, zip = new Entry(new ZipFile(name)));
+
+        zip.refcount++;
+        return zip.value;
+    }
+    
+    synchronized static void removeZip(String name) throws IOException {
+        Entry zip = zips.get(name);
+
+        if (zip == null)
+            return;
+        
+        zip.refcount--;
+        if (zip.refcount == 0) {
+            zip.value.close();
+            zips.remove(name);
+        }
+    }
+
+    static class Entry {
+        ZipFile value;
+        int refcount = 0;
+
+        Entry(ZipFile v) {
+            value = v;
+        }
+    }
+
+}




More information about the armedbear-cvs mailing list