Skip to content
Commit 31bf860b authored by Seigo Nonaka's avatar Seigo Nonaka
Browse files

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
parent fad96cd6
Loading
Loading
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment