Skip to content

Add "resource constant" #362

@dmlloyd

Description

@dmlloyd

Issue description

A resource-based dynamic constant can be useful in cases where a large list or map is being constructed in bytecode. Such constructs fail after a few thousand entries due to oversized bytecode.

We could introduce methods like this:

  • TypeCreator#resourceConstant(String name, List<String> content)
  • TypeCreator#resourceConstant(String name, Map<String,String> content)

These would generate the resource file to the class output, and resolve to a Const which is a dynamic constant that loads the resource file lazily, whose name is derived from the class name plus the constant name. Like lambdas, we'd generate a per-class private bootstrap as needed. The constant would only be valid within the same class, hence scoping it to the type creator.
This will result in much smaller methods which should be more optimizable by HotSpot.

Impl. notes:

  • If the list/map is very small - say fewer than 10 items - we can possibly just create the constant directly without using the external file. This would be done using a dynamic constant with the standard invoke bootstrap method to List.of()/Map.of().
  • We have to enhance ClassOutput to allow resource output.
  • The strings should be interned as they are loaded.
  • We could pre-size the list/map arrays during load since we know the exact content.
  • We might consider adding helpers for Class (loading resources) and InputStream/OutputStream/Reader/Writer as long as we're here.

Metadata

Metadata

Assignees

Labels

2.xIssue applies to Gizmo 2.x

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions