diff options
author | Thomas Wolf <thomas.wolf@paranor.ch> | 2018-03-18 23:29:59 +0100 |
---|---|---|
committer | Thomas Wolf <thomas.wolf@paranor.ch> | 2018-03-25 13:43:37 +0200 |
commit | 4bfc6c2ae9ec582575b05f4e63ee62212bb284a4 (patch) | |
tree | 62f5b9ce7b4f3095a5338cf08afb2f60c91a4ac5 /org.eclipse.jgit.test/exttst/org/eclipse/jgit | |
parent | 0bc2020412f36b2abf75e5aba1dd318443dbbb10 (diff) | |
download | jgit-4bfc6c2ae9ec582575b05f4e63ee62212bb284a4.tar.gz jgit-4bfc6c2ae9ec582575b05f4e63ee62212bb284a4.zip |
Significantly speed up FileTreeIterator on Windows
Getting attributes of files on Windows is an expensive operation.
Windows stores file attributes in the directory, so they are
basically available "for free" when a directory is listed. The
implementation of Java's Files.walkFileTree() takes advantage of
that (at least in the OpenJDK implementation for Windows) and
provides the attributes from the directory to a FileVisitor.
Using Files.walkFileTree() with a maximum depth of 1 is thus a
good approach on Windows to get both the file names and the
attributes in one go.
In my tests, this gives a significant speed-up of FileTreeIterator
over the "normal" way: using File.listFiles() and then reading the
attributes of each file individually. The speed-up is hard to
quantify exactly, but in my tests I've observed consistently 30-40%
for staging 500 files one after another, each individually, and up
to 50% for individual TreeWalks with a FileTreeIterator.
On Unix, this technique is detrimental. Unix stores file attributes
differently, and getting attributes of individual files is not costly.
On Unix, the old way of doing a listFiles() and getting individual
attributes (both native operations) is about three times faster than
using walkFileTree, which is implemented in Java.
Therefore, move the operation to FS/FS_Win32 and call it from
FileTreeIterator, so that we can have different implementations
depending on the file system.
A little performance test program is included as a JUnit test (to be
run manually).
While this does speed up things on Windows, it doesn't solve the basic
problem of bug 532300: the iterator always gets the full directory
listing and the attributes of all files, and the more files there are
the longer that takes.
Bug: 532300
Change-Id: Ic5facb871c725256c2324b0d97b95e6efc33282a
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
Diffstat (limited to 'org.eclipse.jgit.test/exttst/org/eclipse/jgit')
-rw-r--r-- | org.eclipse.jgit.test/exttst/org/eclipse/jgit/treewalk/FileTreeIteratorPerformanceTest.java | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/exttst/org/eclipse/jgit/treewalk/FileTreeIteratorPerformanceTest.java b/org.eclipse.jgit.test/exttst/org/eclipse/jgit/treewalk/FileTreeIteratorPerformanceTest.java new file mode 100644 index 0000000000..b238389847 --- /dev/null +++ b/org.eclipse.jgit.test/exttst/org/eclipse/jgit/treewalk/FileTreeIteratorPerformanceTest.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch> + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.jgit.treewalk; + +import static org.junit.Assert.fail; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.junit.RepositoryTestCase; +import org.eclipse.jgit.treewalk.filter.PathFilter; +import org.junit.Test; + +/** + * Simple performance test for git add / FileTreeIterator. + */ +public class FileTreeIteratorPerformanceTest extends RepositoryTestCase { + + private static int N_OF_FILES = 501; + + @Test + public void testPerformance() throws Exception { + try (Git git = new Git(db)) { + long times[] = new long[N_OF_FILES]; + long sum = 0; + String lastName = null; + for (int i = 0; i < N_OF_FILES; i++) { + lastName = "a" + (i + 100000) + ".txt"; + writeTrashFile(lastName, ""); + long start = System.nanoTime(); + git.add().addFilepattern(lastName).call(); + long elapsed = System.nanoTime() - start; + times[i] = elapsed; + sum += elapsed; + } + System.out.println("Total (µs) " + sum / 1000.0); + for (int i = 0; i < times.length; i++) { + System.out + .println("Time " + i + " (µs) = " + times[i] / 1000.0); + } + FileTreeIterator iter = new FileTreeIterator(db); + try (TreeWalk walk = new TreeWalk(db)) { + walk.setFilter(PathFilter.create(lastName)); + walk.addTree(iter); + long start = System.nanoTime(); + if (walk.next()) { + System.out.println("Walk time (µs) = " + + (System.nanoTime() - start) / 1000.0); + } else { + fail("File not found"); + } + } + } + } +} |