{"record":{"id":"dd4eb65dbee9133a","repo":"oracle/graal","slug":"size-is-not-supported-for-performance-reasons","errorCode":null,"errorMessage":"size() is not supported for performance reasons","messagePattern":"size\\(\\) is not supported for performance reasons","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/graph/NodeMap.java","lineNumber":125,"sourceCode":"        values[getNodeId(node)] = value;\n    }\n\n    public void setAndGrow(Node node, T value) {\n        checkAndGrow(node);\n        set(node, value);\n    }\n\n    /**\n     * @param i\n     * @return Return the key for the entry at index {@code i}\n     */\n    protected Node getKey(int i) {\n        return graph.getNode(i);\n    }\n\n    @Override\n    public int size() {\n        throw new UnsupportedOperationException(\"size() is not supported for performance reasons\");\n    }\n\n    public int capacity() {\n        return values.length;\n    }\n\n    public boolean isNew(Node node) {\n        return getNodeId(node) >= capacity();\n    }\n\n    private boolean check(Node node) {\n        assert node.graph() == graph : String.format(\"%s is not part of the graph\", node);\n        assert !isNew(node) : \"this node was added to the graph after creating the node map : \" + node;\n        assert node.isAlive() : \"this node is not alive: \" + node;\n        return true;\n    }\n\n    @Override","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/graph/NodeMap.java#L107-L143","documentation":"NodeMap is a fixed array indexed by node id, so it does not maintain an element count; size() would either require expensive bookkeeping or return a misleading number (array capacity). It therefore throws UnsupportedOperationException('size() is not supported for performance reasons'). Use capacity() for the backing array length, or track counts yourself if you need cardinality.","triggerScenarios":"Calling nodeMap.size() directly, or passing a NodeMap where Map-shaped APIs call size(): logging helpers, toString implementations, assertion libraries, or generic utility methods typed against java.util.Map.","commonSituations":"Refactoring code from HashMap<Node,T> to NodeMap for speed and forgetting to remove size()-based logic. Debug output that prints map sizes. Test assertions comparing entry counts.","solutions":["Track the number of meaningful entries in a separate counter updated wherever you call set().","Use map.capacity() when you actually need the array dimension, not the entry count.","Snapshot into a HashMap when interoperating with Map-based APIs: copy entries for nodes you care about, then call size() on that."],"exampleFix":"// before\nint entries = map.size(); // throws\n\n// after\nint entries = 0;\nfor (int i = 0; i < map.capacity(); i++) {\n    if (map.get(map.getKey(i)) != null) entries++; // or maintain a counter at set() sites","handlingStrategy":"validation","validationCode":"// never call map.size(); use capacity() for array length and your own counter for entries\nint backingLength = map.capacity();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Track entry counts at set() call sites if cardinality matters.","Snapshot into a HashMap when handing data to Map-based APIs.","Grep for .size() when migrating from HashMap to NodeMap."],"tags":["java","graal","graal-compiler","unsupported-operation","nodemap","api-misuse"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}