Implementation of JarVisitorFactory.getBytesFromInputStream is not efficient. Data loaded from the stream is copied over and over again in memory as the data is fetched.
Because of this problem creation of local EntityManagerFactory is slow for us in hibernate 3.4.0 (~40 seconds). We profiled start-up and found this method being bottleneck (>90% of time spent). Although we have not tested creation of EntityManagerFactory with the latest version of hibernate, this method is there in hibernate 4.1.8 and should be updated.
Attached is alternative implementation of method and performance test.
In our test we loaded 2491475 bytes:
Current implementation: 219 ms
Suggested implementation: 5 ms
Windows x64, RedHat Linux x64, HIbernate 3.4.0 GA
ByteArrayOutputStream class internally still does redundant copying by calling Arrays.copyOf() in write(byte b, int off, int len).
Default internal buffer size in ByteArrayOutputStream is just 32 bytes. Unless you make initial buffer size large everything will be similar to existing implementation. However, if you set buffer size to some larger number, say to 128k, it might be comparable to suggested implementation. This has to be tested.
Ah, good point. For now, I've pushed your version of the method.
what about zero size streams? was it fixed?
I tested it with a 0 size file w/o issue.
cool, thank you