RFR: 8311216: DataURI can lose information in some charset environments [v2]
Michael Strauß
mstrauss at openjdk.org
Sat Oct 28 18:11:08 UTC 2023
On Sat, 28 Oct 2023 06:13:18 GMT, John Hendrikx <jhendrikx at openjdk.org> wrote:
>> Michael Strauß has updated the pull request incrementally with one additional commit since the last revision:
>>
>> added more tests
>
> modules/javafx.graphics/src/main/java/com/sun/javafx/util/DataURI.java line 210:
>
>> 208:
>> 209: private static byte[] decodePercentEncoding(String input) {
>> 210: try (var output = new ByteArrayOutputStream(input.length())) {
>
> If deemed important to optimize this a bit:
>
> This could allocate an array that's a factor 3 too large, which depending on how these URI's are used can become quite big (although for large amounts of data, base64 is recommended).
>
> I noticed that in the URLDecoder they limit the size of the buffer `new StringBuilder(numChars > 500 ? numChars / 2 : numChars);` -- perhaps you could do something similar (`new ByteArrayOutputStream(input.length() > 500 ? input.length() / 2 : input.length())`)
>
> Another alternative would be to calculate the exact size first (by counting `%`s) avoiding the need to reallocate the `byte[]` -- performance is (IMHO) likely to be better:
>
> - ByteArrayOutputStream method
> - allocate output array: zeroes n*3 bytes
> - decode: reads whole string once (n to n*3 reads)
> - decode: writes n bytes
> - reallocate: reads whole output once (n)
> - reallocate: writes n bytes
> - garbage created: n*3 bytes
> - max memory use: n*3 + n bytes
>
> Total read/writes: 4n + 5n
>
> - Count method
> - count: reads whole string once (n to n*3 reads)
> - allocate output array: zeroes n bytes
> - decode: reads whole string again (n to n*3 reads)
> - decode: writes n bytes
> - garbage created: none
> - max memory use: n bytes
>
> Total read/writes: 6n + 2n
Solved by calculating the exact size first.
-------------
PR Review Comment: https://git.openjdk.org/jfx/pull/1165#discussion_r1375301666
More information about the openjfx-dev
mailing list