【问题标题】:Why am I getting *worse* performance with a *larger* buffer in my BufferedReader?为什么在我的 BufferedReader 中使用*大* 缓冲区时性能会*差*?
【发布时间】:2014-12-18 12:52:31
【问题描述】:

当我改变缓冲区的大小时,我得到了无法从 BufferedReader 解释的奇怪结果。

我曾强烈预计,随着缓冲区大小的增加,性能会逐渐提高,收益递减会很快出现,此后性能会或多或少持平。但似乎,在只有非常适中的缓冲区大小之后,增加缓冲区的大小会使其变慢

这是一个最小的例子。它所做的只是遍历一个文本文件,并计算行长的总和。

public int traverseFile(int bufSize) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader("words16"), bufSize*1024);
    String line;
    int total=0;
    while ((line=reader.readLine())!=null)
        total+=line.length();
    reader.close();
    return total;
}

我尝试使用各种缓冲区大小对此进行基准测试,结果相当奇怪。最大约256KB,性能提升;在那之后,情况变得更糟。我想知道这是否只是分配缓冲区所花费的时间,所以我尝试添加一些东西以使其始终分配相同的内存总量(见下面的第二行):

public int traverseFile(int bufSize) throws IOException {
    byte[] pad = new byte[(65536-bufSize)*1024];
    BufferedReader reader = new BufferedReader(new FileReader("words16"), bufSize*1024);
    String line;
    int total=0;
    while ((line=reader.readLine())!=null)
        total+=line.length();
    reader.close();
    return total;
}

这并不难。在两台不同的机器上,我仍然得到相同的结果。以下是完整结果:

Benchmark                                        Mode  Samples    Score   Error  Units
j.t.BufferSizeBenchmark.traverse_test1_4K        avgt      100  363.987 ± 1.901  ms/op
j.t.BufferSizeBenchmark.traverse_test2_16K       avgt      100  356.551 ± 0.330  ms/op
j.t.BufferSizeBenchmark.traverse_test3_64K       avgt      100  353.462 ± 0.557  ms/op
j.t.BufferSizeBenchmark.traverse_test4_256K      avgt      100  350.822 ± 0.562  ms/op
j.t.BufferSizeBenchmark.traverse_test5_1024K     avgt      100  356.949 ± 0.338  ms/op
j.t.BufferSizeBenchmark.traverse_test6_4096K     avgt      100  358.377 ± 0.388  ms/op
j.t.BufferSizeBenchmark.traverse_test7_16384K    avgt      100  367.890 ± 0.393  ms/op
j.t.BufferSizeBenchmark.traverse_test8_65536K    avgt      100  363.271 ± 0.228  ms/op

如您所见,最佳位置约为 256KB。差别不大,但肯定是可以衡量的。

我能想到的只是这可能与内存缓存有关。是因为正在写入的 RAM 离正在读取的 RAM 更远吗?但如果它是一个循环缓冲区,我什至不确定这是不是真的:正在写入的内容将位于正在读取的内容之后。

words16 文件是 80MB,所以我不能在这里发布,但它是 Fedora 的标准 /usr/share/dict/words 文件,超过了 16 倍。如有必要,我可以找到发布链接的方法。

这是基准测试代码:

@OutputTimeUnit(TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@OperationsPerInvocation(1)
@Warmup(iterations = 30, time = 100, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 100, time = 10000, timeUnit = TimeUnit.MILLISECONDS)
@State(Scope.Thread)
@Threads(1)
@Fork(1)
public class BufferSizeBenchmark {

    public int traverseFile(int bufSize) throws IOException {
        byte[] pad = new byte[(65536-bufSize)*1024];
        BufferedReader reader = new BufferedReader(new FileReader("words16"), bufSize*1024);
        String line;
        int total=0;
        while ((line=reader.readLine())!=null)
            total+=line.length();
        reader.close();
        return total;
    }

    @Benchmark
    public int traverse_test1_4K() throws IOException {
        return traverseFile(4);
    }

    @Benchmark
    public int traverse_test2_16K() throws IOException {
        return traverseFile(16);
    }

    @Benchmark
    public int traverse_test3_64K() throws IOException {
        return traverseFile(64);
    }

    @Benchmark
    public int traverse_test4_256K() throws IOException {
        return traverseFile(256);
    }

    @Benchmark
    public int traverse_test5_1024K() throws IOException {
        return traverseFile(1024);
    }

    @Benchmark
    public int traverse_test6_4096K() throws IOException {
        return traverseFile(4096);
    }

    @Benchmark
    public int traverse_test7_16384K() throws IOException {
        return traverseFile(16384);
    }

    @Benchmark
    public int traverse_test8_65536K() throws IOException {
        return traverseFile(65536);
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(
                        ".*" + BufferSizeBenchmark.class.getSimpleName() + ".*")
                .forks(1).build();

        new Runner(opt).run();
    }

}

为什么当我增加缓冲区大小时性能会变差?

【问题讨论】:

    标签: java performance file-io buffer bufferedreader


    【解决方案1】:

    这很可能是缓存行大小的影响。由于缓存使用 LRU 驱逐策略,使用过大的缓冲区会导致您写入缓冲区“开始”的内容在您有机会读取之前被驱逐。

    【讨论】:

      【解决方案2】:

      256k 是典型的 CPU 缓存大小!您在什么类型的 CPU 上进行了测试?

      所以发生的情况是:如果您读取 256k 或更小的块,则写入缓冲区的内容在读取访问时仍在 CPU 缓存中。如果您有大于 256k 的块,则最后读取的 256k 位于 CPU 缓存中,因此当从头开始读取时,必须从主内存中检索内容。

      第二个问题是缓冲区分配。填充缓冲区的技巧很聪明,但并没有真正平均分配成本。这样做的原因是,分配的真正成本不是内存的保留,而是清除它。此外,操作系统可能会推迟在实际内存中映射到它第一次访问的时间。但是你永远不会访问填充缓冲区。

      【讨论】:

        猜你喜欢
        • 2016-09-21
        • 1970-01-01
        • 2013-06-03
        • 1970-01-01
        • 2023-03-07
        • 2018-05-24
        • 2014-07-23
        • 2011-06-06
        • 2011-07-22
        相关资源
        最近更新 更多