RFR: 8343418: Unnecessary Hashtable usage in CSS.htmlAttrToCssAttrMap [v3]
Andrey Turbanov
aturbanov at openjdk.org
Wed Nov 6 06:40:52 UTC 2024
> If a thread-safe implementation is not needed, it is recommended to use HashMap instead of legacy synchronized Hashtable.
> Map `CSS.htmlAttrToCssAttrMap` is modified only within static initialization block and then only `get` method is called.
>
> The only subtle difference is that Hashtable doesn't allow `null` keys and throws NPE from `get` method.
> I've checked all possible keys which are passed to `htmlAttrToCssAttrMap.get`.
> Currently 3 different execution paths are possible:
>
> **1,2**
> When `HTML.Attribute.BORDER` is passed as a key. It's definitely non-null.
>
> javax.swing.text.html.CSS#translateHTMLToCSS
> translateAttribute(HTML.Attribute.BORDER, "1", cssAttrSet);
> CSS.Attribute[] cssAttrList = getCssAttribute(key);
>
>
> javax.swing.text.html.CSS#translateAttributes
> translateAttribute(HTML.Attribute.BORDER, Integer.toString(borderWidth), cssAttrSet);
> CSS.Attribute[] cssAttrList = getCssAttribute(key);`
>
>
> **3**
> When non-null `key` is passed as a key.
>
> javax.swing.text.html.CSS#translateAttributes
>
> if (name instanceof HTML.Attribute) { // from this `instanceof` we know that it's non-null
> HTML.Attribute key = (HTML.Attribute)name;
>
> translateAttribute(key, (String) htmlAttrSet.getAttribute(key), cssAttrSet);
> CSS.Attribute[] cssAttrList = getCssAttribute(key);
>
>
> 
>
>
> I've used HashMap.newHashMap method instead of constructor to avoid resizing of internal HashMap array.
Andrey Turbanov has updated the pull request incrementally with one additional commit since the last revision:
Use immutable map instead of HashMap.
It fits even better: has the same null handling as Hashtable
-------------
Changes:
- all: https://git.openjdk.org/jdk/pull/21785/files
- new: https://git.openjdk.org/jdk/pull/21785/files/b653278f..a0860021
Webrevs:
- full: https://webrevs.openjdk.org/?repo=jdk&pr=21785&range=02
- incr: https://webrevs.openjdk.org/?repo=jdk&pr=21785&range=01-02
Stats: 54 lines in 1 file changed: 5 ins; 1 del; 48 mod
Patch: https://git.openjdk.org/jdk/pull/21785.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/21785/head:pull/21785
PR: https://git.openjdk.org/jdk/pull/21785
More information about the client-libs-dev
mailing list