Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BuiltList won't compute hash as part of ==. #269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Commits on Jan 3, 2023

  1. BuiltList won't compute hash as part of ==.

    The implementation of `BuiltList.operator==` used `this.hashCode != other.hashCode` as a short-cut in operator `==`.
    However, computing `hashCode` can be as expensive as doing equality, since it has to visit all the same objects (otherwise the hash code won't be consistent with equality) and do a computation on each.
    
    This change makes the `hashCode` only be used if it's already computed by both `this` and `other`, in which case it should be free.
    
    It's a change in performance-behavior. A single `==` won't do double work, but repeated `==`s on the same objects might have been more efficient if the hash code was computed eagerly. For elements in a set or map, the hash code will be computed anyway, so it should not affect those. Having a list of `BuiltList`s and doing repeated `indexOf` with the same values on the list might suffer.
    I believe the trade-off is worth it, because most lists are not compared for equality, and those that are, are either one-off comparisons, or used as map keys or set elements.
    lrhn authored Jan 3, 2023
    Configuration menu
    Copy the full SHA
    0f5d54b View commit details
    Browse the repository at this point in the history