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

Fix performance problems caused by Identifier#toHexString #615

Merged
merged 3 commits into from
Nov 6, 2017

Conversation

davidgyoung
Copy link
Member

This addresses performance problems described in #612 where usage of beacon layouts with 3-15 byte identifiers results in slow calls to Identifier#toHexString, as this is what is used to convert identifiers of this length range to Strings.

Unfortunately, it is very difficult to remove the use of toHexString() in normal operation of the library, because it is used for both serialization of Beacon objects when moving them in the library, and for the implementation of hashCode() in Beacon, which is heavily used for internal beacon tracking.

Two changes:

  1. Wrap two debug level logging calls to beacon.toString() in checks to see if verbose logging is enabled, so in normal cases, there will be no call toString() and hence no call to toHexString() on Identifier.

  2. Optimize the implementation of toHexString() so it does not instantiate objects in a loop and instead uses primitive types for all conversion parts except the final construction of a single String object.

The first change reduces the number of calls to toHexString() by 51%, with all the remaining calls being used for serialization and hashCode calculations.

Before disabling verbose logging:
toHexString calls total: 366, serialize: 12, hash: 168

After disabling verbose logging:
toHexString calls total: 442, serialize: 34, hash: 408

The second change improves the performance of the method by 90% from 240 microseconds per call to 23 microseconds per call on a Nexus 5X.

Original implementation:
10-27 14:31:56.862 15936 15974 I Identifier: toHexString callCount: 558 Nanos per call: 241237

New implementation:
10-27 15:06:01.371 25842 26260 I Identifier: toHexString callCount: 722 Nanos per call: 22948 

Between these two changes, processing time in toHexString() is reduced by 95%, and the number of objects allocated is sharply reduced.

@davidgyoung
Copy link
Member Author

A test build with these changes, along with instructions on how to configure it with your project is here:
https://github.com/AltBeacon/android-beacon-library/releases/edit/optimize-identifier-hex-conversion

@davidgyoung davidgyoung merged commit 8cd351f into master Nov 6, 2017
@cupakromer cupakromer deleted the optimize-identifer-hex-conversion branch November 29, 2017 00:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant