Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 1 May 2017 06:03:44 +0000 (UTC)
From:      Alexander Motin <mav@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r317634 - stable/10/lib/libc/gen
Message-ID:  <201705010603.v4163iiD089496@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: mav
Date: Mon May  1 06:03:44 2017
New Revision: 317634
URL: https://svnweb.freebsd.org/changeset/base/317634

Log:
  MFC r317064: Optimize pathologic case of telldir() for Samba.
  
  When application reads large directory, calling telldir() for each entry,
  like Samba does, it creates exponential performance drop as number of
  entries reach tenths to hundreds of thousands.  It is caused by full search
  through the internal list, that never finds matches in that scenario, but
  creates O(n^2) delays.  This patch optimizes that search, limiting it to
  entries of the same buffer, turning time closer to O(n) in case of linear
  directory scan.

Modified:
  stable/10/lib/libc/gen/telldir.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/lib/libc/gen/telldir.c
==============================================================================
--- stable/10/lib/libc/gen/telldir.c	Mon May  1 06:03:07 2017	(r317633)
+++ stable/10/lib/libc/gen/telldir.c	Mon May  1 06:03:44 2017	(r317634)
@@ -53,15 +53,22 @@ long
 telldir(dirp)
 	DIR *dirp;
 {
-	struct ddloc *lp;
+	struct ddloc *lp, *flp;
 	long idx;
 
 	if (__isthreaded)
 		_pthread_mutex_lock(&dirp->dd_lock);
+	flp = NULL;
 	LIST_FOREACH(lp, &dirp->dd_td->td_locq, loc_lqe) {
-		if (lp->loc_seek == dirp->dd_seek &&
-		    lp->loc_loc == dirp->dd_loc)
+		if (lp->loc_seek == dirp->dd_seek) {
+			if (flp == NULL)
+				flp = lp;
+			if (lp->loc_loc == dirp->dd_loc)
+				break;
+		} else if (flp != NULL) {
+			lp = NULL;
 			break;
+		}
 	}
 	if (lp == NULL) {
 		lp = malloc(sizeof(struct ddloc));
@@ -73,7 +80,10 @@ telldir(dirp)
 		lp->loc_index = dirp->dd_td->td_loccnt++;
 		lp->loc_seek = dirp->dd_seek;
 		lp->loc_loc = dirp->dd_loc;
-		LIST_INSERT_HEAD(&dirp->dd_td->td_locq, lp, loc_lqe);
+		if (flp != NULL)
+			LIST_INSERT_BEFORE(flp, lp, loc_lqe);
+		else
+			LIST_INSERT_HEAD(&dirp->dd_td->td_locq, lp, loc_lqe);
 	}
 	idx = lp->loc_index;
 	if (__isthreaded)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201705010603.v4163iiD089496>