Improve Font#equals and Font#hashCode for HashMap.
Font#equals and Font#hashCode reads bytes in the buffer and end up with bad performance if we use it as key of HashMap. To improve them, does following. - Font#hashCode Use buffer size as hash. Since we only accept OpenType compliant font files (invalid font data will be IllegalArgumentException in ctor), the hash collision due to same file size is unlikely happens. The ByteBuffer#hashCode includes head position as hashCode but intentionally exclude them from new hashCode since the head position is not used by platform include native code. - Font#equals The ByteBuffer is frequently duplicated for many reasons, e.g. having different reading head, etc, but the underlying mmaped memory address has not changed. Fortunately we have this address in underlying font object. Compare them as a shortcut of content equality. With this change, the local benchmark for the Font key-ed HashMap look up gets faster from 30ms to 4us. (on Pixel 3a) Bug: 170494256 Test: atest FontTest Change-Id: Ibc7f7aae0b3427e71209444eebb93753c340b711
Loading
Please register or sign in to comment