Bug 53904 - Generate xlsx File is slow causing by org.apache.poi.openxml4j.opc.PackageRelationshipCollection
Summary: Generate xlsx File is slow causing by org.apache.poi.openxml4j.opc.PackageRel...
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.8-FINAL
Hardware: All All
: P2 critical with 4 votes (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-09-19 14:42 UTC by WuQiming
Modified: 2014-08-31 20:22 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description WuQiming 2012-09-19 14:42:42 UTC
When I generate a xlsx file which size is 1.7MB, I wait more than 11 minutes to finish. When I fix the bug, the time is reduce to 5 seconds.
My code(JDK 1.7):
try (BufferedOutputStream out = new BufferedOutputStream(desFile.getContent().getOutputStream())) {
			this.workbook.write(out);
} catch (IOException ex) {
	log.error(ex.getMessage(), ex);
}

The bug(org.apache.poi.openxml4j.opc.PackageRelationshipCollection):
public PackageRelationship addRelationship(URI targetUri, TargetMode targetMode, String relationshipType, String id) {
	if (id == null) {
		// Generate a unique ID is id parameter is null.
		int i = 0;
		do {
			id = "rId" + ++i;
		} while (relationshipsByID.get(id) != null);
	}
	......
}
The variable i will raise form 0 to 1000000 when the method be called  1000000 and the loop will calculate more than 500000 billion times!

I modified the code:
private int currentRelationshipsId = 0;
public PackageRelationship addRelationship(URI targetUri, TargetMode targetMode,
 String relationshipType, String id) {
	if (id == null) {
		// Generate a unique ID is id parameter is null.
		id = "rId" + ++currentRelationshipsId;
	}
	......
}

The performance became more faster.
Comment 1 Nick Burch 2013-02-04 15:07:27 UTC
Your suggested fix would run into problems if relationships were added using the other overloaded add methods. For example, if you opened an existing file with two relationships, then went to add a third, your code would fail by trying to add a second rId1

In r1442148 I've added something that'll hopefully greatly reduce the number of steps, but will still check for clashes. Could you please test that and see if it resolves the issue for you?
Comment 2 Dmitry Smirnov 2013-02-21 13:05:07 UTC
*** Bug 53582 has been marked as a duplicate of this bug. ***
Comment 3 Dominik Stadler 2014-08-31 20:22:20 UTC
No response for quite some time on this one, a potential fix was made, so I don't think we can improve much more here unless you can provide updated information about your experience with the changes that were applied, therefore I am closing this to FIXED for now.