The following method reads specially formatted text (TOML) from a file and parses the input using a parser to get a few properties, which are then used to create an object.
The profiler tells me that this method is the bottleneck in my program. While parsing ~5000 files, this method eats up over 95% of the total running time. While in another method, I am able to write the data parsed from this method to a text file in under 4 seconds for more than 5000 files, this method takes around 15 seconds on an average to run.
/**
* Creates an appropriate instance of a Parsable implementation depending
* upon the header of the file.
*
* @param file the path of the file from which to create a Parsable.
* @return the created Parsable.
*/
private Parsable createParsable(Path file) {
Toml toml = new Toml();
try (BufferedReader br = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
StringBuilder header = new StringBuilder();
String line;
while ((line = br.readLine()) != null && !line.equals(HEADER_DELIMITER)) {
header.append(line).append("\n");
}
toml.parse(header.toString());
String title = toml.getString("title");
author = toml.getString("author") != null ? toml.getString("author") : author;
String date = toml.getString("date");
String slug = toml.getString("slug");
LocalDate publishDate = LocalDate.parse(date, DateTimeFormatter.ofPattern(config.getDateFormat()));
String layout = toml.getString("layout");
List<String> tag = toml.getList("tags");
StringBuilder content = new StringBuilder();
while ((line = br.readLine()) != null) {
content.append(line).append("\n");
}
if (layout.equals("post")) {
return new Post(title, author, publishDate, file, content.toString(), slug, layout, tag);
}
else {
return new Page(title, author, file, content.toString(), slug, layout, tag);
}
}
catch (IOException ex) {
Logger.getLogger(DirectoryCrawler.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
It is important to note that this method gets called over 5000 times in my test. I have tried analyzing separate parts of the method for the performance problem, but haven't been able to identify any. How can I write this better?
The TOML library is from here : https://github.com/mwanji/toml4j
And the implementation of the constructors are like so:
/**
* Creates a Post with the given paramenter.
*
* @param titl the post title
* @param auth the post author
* @param dat the post date
* @param loc the post's Path
* @param cont the post's content
* @param slu the post slug
* @param lay the layout
* @param tag the list of tags
*/
public Post(String titl, String auth, LocalDate dat,
Path loc, String cont, String slu, String lay, List<String> tag) {
title = titl;
author = auth;
//TODO add summary option
//this.summary = summ;
date = dat;
location = loc;
content = cont;
slug = slu;
layout = lay;
tags = tag;
}
new Toml()
? – h.j.k. Jul 2 at 8:35Post
andPage
constructors will be useful too, if they are non-trivial. – h.j.k. Jul 2 at 8:38