Skip to content

[FEATURE] Use java.lang.LazyConstant for lazy getters #3993

@xtay2

Description

@xtay2

Describe the feature
With LazyConstant (formerly StableValue), Java will receive a new default mechanism for lazy initialization which supports constant folding. This issue proposes to change the implementation of the @Getter(lazy = true) to use LazyConstants under the hood:

@Getter(lazy=true) private final double[] cached = expensive();
  
  private double[] expensive() {
    double[] result = new double[1000000];
    for (int i = 0; i < result.length; i++) {
      result[i] = Math.asin(i);
    }
    return result;
  }

will become:

@Getter(lazy=true) private final LazyConstant<double[]> cached = LazyConstant.of(() -> {
    double[] result = new double[1000000];
    for (int i = 0; i < result.length; i++) {
      result[i] = Math.asin(i);
    }
    return result;
  });
  
  private double[] expensive() {
    return expensive.get();
  }

Describe the target audience
Everyone could benefit from the performance boost unlocked through constant folding.

Additional context
This would introduce breaking (source) changes, because Lombok would have to change the datatype of the field. As the Lombok documentation already discourages direct field access, I'd argue, that this would be an excusable price to pay.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions