Skip to content

Possible efficiency improvements in annotation binary searches #606

@MikeEdgar

Description

@MikeEdgar

There may be a possible minor improvement in the binary searching done in AnnotationInstance by special-casing arrays of length 0 and 1, and using lambdas for the comparators. The use of the lambda relies on the fact that Arrays.binarySearch will pass the array entries as the first parameter of the comparator (AnnotationInstance#value(String) already relies on this). It should result in an allocation-free search.

For example:

static AnnotationInstance binarySearch(AnnotationInstance[] annotations, DotName name) {
    if (annotations.length == 0) {
        return null;
    }

    if (annotations.length == 1) {
        return annotations[0].name().equals(name) ? annotations[0] : null;
    }

    // Relies on binarySearch passing array entries as the first comparator parameter
    int i = Arrays.binarySearch(annotations, null, (entry, x) -> entry.name().compareTo(name));
    return i >= 0 ? annotations[i] : null;
}

Also, the create method AnnotationInstance instantiates an AnnotationValue name comparator which maybe could be a constant.

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