RFR: 8280010: Remove double buffering of InputStream for Properties.load
Andrey Turbanov
duke at openjdk.java.net
Fri Jan 14 12:21:56 UTC 2022
On Mon, 10 Jan 2022 20:46:36 GMT, Andrey Turbanov <duke at openjdk.java.net> wrote:
> `Properties.load` uses `java.util.Properties.LineReader`. LineReader already buffers input stream. Hence wrapping InputStream in BufferedInputStream is redundant.
Checked. `BufferedInputStream` add a bit of overhead.
Benchmark
@BenchmarkMode(value = Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 6, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@State(Scope.Benchmark)
public class PropertiesLoad {
Properties properties;
private InputStream arrayInputStream;
private InputStream fileInputStream;
private Path filePath;
@Setup
public void setupStrings() throws IOException {
properties = new Properties();
String content = """
currentVersion=IdealGraphVisualizer {0}
LBL_splash_window_title=Starting IdealGraphVisualizer
SPLASH_WIDTH=475
SplashProgressBarBounds=0,273,475,6
SplashProgressBarColor=0xFFFFFF
SplashRunningTextBounds=10,283,460,12
SplashRunningTextColor=0xFFFFFF
""";
filePath = Files.createTempFile("benchmark", ".properties");
Files.writeString(filePath, content);
arrayInputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
fileInputStream = Files.newInputStream(filePath);
}
@TearDown
public void tearDown() throws IOException {
fileInputStream.close();
Files.delete(filePath);
}
@Benchmark
public Properties plain_ByteStream() throws IOException {
properties.load(arrayInputStream);
return properties;
}
@Benchmark
public Properties plain_fileStream() throws IOException {
properties.load(fileInputStream);
return properties;
}
@Benchmark
public Properties buffered_ByteStream() throws IOException {
properties.load(new BufferedInputStream(arrayInputStream));
return properties;
}
@Benchmark
public Properties buffered_fileStream() throws IOException {
properties.load(new BufferedInputStream(fileInputStream));
return properties;
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(PropertiesLoad.class.getSimpleName())
.build();
new Runner(opt).run();
}
}
Results:
Benchmark Mode Cnt Score Error Units
PropertiesLoad.buffered_ByteStream avgt 5 2416,364 ± 46,209 ns/op
PropertiesLoad.buffered_fileStream avgt 5 4276,015 ± 139,094 ns/op
PropertiesLoad.plain_ByteStream avgt 5 1445,864 ± 649,779 ns/op
PropertiesLoad.plain_fileStream avgt 5 3183,012 ± 198,974 ns/op
-------------
PR: https://git.openjdk.java.net/jdk/pull/7021
More information about the serviceability-dev
mailing list