-
Notifications
You must be signed in to change notification settings - Fork 10
Small optimization in LoggerImpl.extractFields #529
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
Conversation
| put("_error_details", it.message.orEmpty()) | ||
| ): InternalFieldsMap { | ||
| // Maintainer note: keep initialCapacity in sync with the code adding fields to the map. | ||
| val initialCapacity = (fields?.size ?: 0) + (throwable?.let { 2 } ?: 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: the 2 here has to be kept in sync with the code in throwable?.let {}. That's slightly annoying, an alternative could be:
companion object {
val throwableExtractors = listOf<(Throwable) -> Pair<String, FieldValue>>({
"_error" to it.javaClass.name.orEmpty().toFieldValue()
}, {
"_error_details" to it.message.orEmpty().toFieldValue()
})
}then:
val initialCapacity = (fields?.size ?: 0) + (throwable?.let { throwableExtractors.size } ?: 0)
throwable?.let {
for (extractor in throwableExtractors) {
val (key, value) = extractor(throwable)
extractedFields[key] = value
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this alternative better over the hardcoded 2. The one relying on 2 feels fragile over time and easy to forget if there is a need to add another _error_... fields in the future, though no sure it's worth the cost for creating throwableExtractors
97be702 to
47ab3b9
Compare
| @Suppress("SENSELESS_COMPARISON") | ||
| internal fun Map<String, String>?.toFields(): Map<String, FieldValue> { | ||
| if (this == null) return emptyMap() | ||
| if (isNullOrEmpty()) return emptyMap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch
|
I have read the CLA Document and I hereby sign the CLA |
47ab3b9 to
02cc7be
Compare
- In `LoggerImpl.extractFields`, skip creating a MapBuilder (capacity 8) and a HashMap(0S if throwable is null and fields is null or empty - In `LoggerImpl.extractFields`, skip creating a MapBuilder AND a HashMap for every log with fields, directly create the target HashMap with the right capacity. - In `Map.toFields()`, skip creating a HashMap(0) is map is empty.
02cc7be to
485dedb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the improvement! Btw, this should be ok to be merged as I did extensive manual verification with the gradle test app
LoggerImpl.extractFields, skip creating a MapBuilder (capacity 8) and a HashMap(0) if throwable is null and fields is null or emptyLoggerImpl.extractFields, skip creating a MapBuilder AND a HashMap for every log with fields, directly create the target HashMap with the right capacity.Map.toFields(), skip creating a HashMap(0) is map is empty.