xzel23 commented on PR #270: URL: https://github.com/apache/pdfbox/pull/270#issuecomment-3341973061
What I mean is: the effect of your change at runtime is negligible: the old version initializes the internal array reference with a pre-allocated shared Object[] instance of length 0 (since Java 9, I think before Java 9, a new empty array was allocated every time) and the first add operation instantly allocates a new array of size 10 (which is by chance the default capacity of an ArrayList). Since the empty array is a shared static instance, even the number of array allocations remains the same (this would not be true for size > 10). But the new version is slightly less readable than the original version, and should the list ever have to be expanded or elements removed, the size has to be changed manually too. Compared to that, using List.of(...) reduces the number of method calls by 10, returns a list that is allocated with the correct size right from the start no matter how many elements you pass (no reallocations are necessary), and the code is both more readable and easier to maintain (less code and no manual keeping track of the array size). It should also perform slightly better. But if this really was a performance bottleneck, the tables list should be converted to a private static final constant, this only minimally increases the heap size because all strings contained in the table are contained in the string pool anyway as long as the program runs, so only the list overhead would be added, while at the same time the code size and number of memory allocations during runtime would be reduced. All in all, I see lots of small PRs you made with the title "improve performance of ...". But in most cases the performance impact is near zero. That being said, your PRs generally really improve the code quality - so you could maybe name them "improve code quality" - which is something that is at least as important as performance, so keep it up - extracting common expressions into temporary variables often improves readability and maintainability (like a change needs to be made only once instead of several times when the same expression is used multiple times). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
