git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH 01/11] Change Daemon to use concurrent collections for exported repositories

From
Shawn O. Pearce <spearce@spearce.org>
Date
Jul 11, 2009, 20:19 UTC
Message-ID
<1247343566-19025-2-git-send-email-spearce@spearce.org>
In-Reply-To
<1247343566-19025-1-git-send-email-spearce@spearce.org>

A ConcurrentHashMap can be read without taking a lock, which makes it more efficient when serving the concurrent client connections. This is also true of a CopyOnWriteArrayList, where again the readers far outweigh the writers. Copying on write is much more efficient than copying on read.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../src/org/spearce/jgit/transport/Daemon.java     |   36 +++++++------------
 1 files changed, 13 insertions(+), 23 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/Daemon.java b/org.spearce.jgit/src/org/spearce/jgit/transport/Daemon.java
index 3101d6f..d3f7b2e 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/Daemon.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/Daemon.java
@@ -46,10 +46,10 @@
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.SocketAddress;
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
 
 import org.spearce.jgit.lib.PersonIdent;
 import org.spearce.jgit.lib.Repository;
@@ -93,8 +93,8 @@ public Daemon() {
 	 */
 	public Daemon(final InetSocketAddress addr) {
 		myAddress = addr;
-		exports = new HashMap<String, Repository>();
-		exportBase = new ArrayList<File>();
+		exports = new ConcurrentHashMap<String, Repository>();
+		exportBase = new CopyOnWriteArrayList<File>();
 		processors = new ThreadGroup("Git-Daemon");
 
 		services = new DaemonService[] {
@@ -196,9 +196,7 @@ public synchronized void setExportAll(final boolean export) {
 	 *            the repository instance.
 	 */
 	public void exportRepository(final String name, final Repository db) {
-		synchronized (exports) {
-			exports.put(name, db);
-		}
+		exports.put(name, db);
 	}
 
 	/**
@@ -210,9 +208,7 @@ public void exportRepository(final String name, final Repository db) {
 	 *            named <code>git-daemon-export-ok</code> will be published.
 	 */
 	public void exportDirectory(final File dir) {
-		synchronized (exportBase) {
-			exportBase.add(dir);
-		}
+		exportBase.add(dir);
 	}
 
 	/** @return timeout (in seconds) before aborting an IO operation. */
@@ -351,21 +347,15 @@ Repository openRepository(String name) {
 		name = name.substring(1);
 
 		Repository db;
-		synchronized (exports) {
-			db = exports.get(name);
-			if (db != null)
-				return db;
+		db = exports.get(name);
+		if (db != null)
+			return db;
 
-			db = exports.get(name + ".git");
-			if (db != null)
-				return db;
-		}
+		db = exports.get(name + ".git");
+		if (db != null)
+			return db;
 
-		final File[] search;
-		synchronized (exportBase) {
-			search = exportBase.toArray(new File[exportBase.size()]);
-		}
-		for (final File f : search) {
+		for (final File f : exportBase) {
 			db = openRepository(new File(f, name));
 			if (db != null)
 				return db;
-- 
1.6.4.rc0.117.g28cb
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 2 of 14 in “Repository instance caching”
  1. 00/11 Repository instance cachingShawn O. Pearce, Jul 11, 2009
  2. 01/11 Change Daemon to use concurrent collections for exported repositoriesShawn O. Pearce, Jul 11, 2009
  3. 02/11 Make Daemon's exportAll check not require synchronizationShawn O. Pearce, Jul 11, 2009
  4. 03/11 Don't retry ".git" suffix in daemon if already triedShawn O. Pearce, Jul 11, 2009
  5. 04/11 Refactor ref advertisement code from server implementationsShawn O. Pearce, Jul 11, 2009
  6. 05/11 Don't advertise HEAD from ReceivePackShawn O. Pearce, Jul 11, 2009
  7. 06/11 Add a use reference counter to RepositoryShawn O. Pearce, Jul 11, 2009
  8. 07/11 Introduce RepositoryCache to cache handles of Repository objectsShawn O. Pearce, Jul 11, 2009
  9. 08/11 Change Daemon to use RepositoryCacheShawn O. Pearce, Jul 11, 2009
  10. 09/11 Expose the Repository's ObjectDatabase objectShawn O. Pearce, Jul 11, 2009
  11. 10/11 Use cached Repository instances when resolving alternatesShawn O. Pearce, Jul 11, 2009
  12. 11/11 Send .have lines in ReceivePack for alternate repositoriesShawn O. Pearce, Jul 11, 2009
  13. Robin RosenbergJul 21, 2009
  14. Shawn O. PearceJul 21, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.