RFR: 8343418: Use HashMap instead of Hashtable for CSS.htmlAttrToCssAttrMap

Phil Race prr at openjdk.org
Fri Nov 1 22:24:29 UTC 2024


On Wed, 30 Oct 2024 12:02:14 GMT, Andrey Turbanov <aturbanov at openjdk.org> wrote:

> 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);
> 
> 
> ![idea_analyze_dataflow_to_here](https://github.com/user-attachments/assets/48ace4de-6d0a-468e-bb14-b579a193254b)
> 
> 
> I've used HashMap.newHashMap method instead of constructor to avoid resizing of internal HashMap array.

I am not seeing a lot of value in this churn in completely OK code.
Instead please find something that is functionally broken that needs fixing.



> @honkar-jdk yes. It was my intention to convert them all. I want to go one-by-one to make review easier.

No. Let's leave them alone

src/java.desktop/share/classes/javax/swing/text/html/CSS.java line 1093:

> 1091:      * key ends up being an array of CSS.Attribute.* objects.
> 1092:      */
> 1093:     private static final HashMap<HTML.Attribute, Attribute[]> htmlAttrToCssAttrMap = HashMap.newHashMap(20);

I don't know why you decided to make this less clear by removing "CSS."

-------------

PR Comment: https://git.openjdk.org/jdk/pull/21785#issuecomment-2452666362
PR Review Comment: https://git.openjdk.org/jdk/pull/21785#discussion_r1826343389


More information about the client-libs-dev mailing list