Skip to content

Conversation

@pyricau
Copy link
Contributor

@pyricau pyricau commented Jul 28, 2025

  • In LoggerImpl.extractFields, skip creating a MapBuilder (capacity 8) and a HashMap(0) 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.

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)
Copy link
Contributor Author

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
  }

Copy link
Contributor

@FranAguilera FranAguilera Jul 28, 2025

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

@FranAguilera FranAguilera force-pushed the py/optimize_extract_fields branch from 97be702 to 47ab3b9 Compare July 28, 2025 08:26
@FranAguilera FranAguilera self-requested a review July 28, 2025 08:33
@Suppress("SENSELESS_COMPARISON")
internal fun Map<String, String>?.toFields(): Map<String, FieldValue> {
if (this == null) return emptyMap()
if (isNullOrEmpty()) return emptyMap()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

@pyricau
Copy link
Contributor Author

pyricau commented Jul 28, 2025

I have read the CLA Document and I hereby sign the CLA

@FranAguilera FranAguilera reopened this Jul 28, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Jul 28, 2025
@bitdriftlabs bitdriftlabs unlocked this conversation Jul 28, 2025
@FranAguilera FranAguilera force-pushed the py/optimize_extract_fields branch from 47ab3b9 to 02cc7be Compare July 28, 2025 15:04
@FranAguilera FranAguilera reopened this Jul 28, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Jul 28, 2025
pyricau and others added 2 commits July 28, 2025 19:42
- 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.
@FranAguilera FranAguilera force-pushed the py/optimize_extract_fields branch from 02cc7be to 485dedb Compare July 28, 2025 17:42
@FranAguilera FranAguilera reopened this Jul 28, 2025
@FranAguilera FranAguilera requested a review from murki July 28, 2025 17:53
Copy link
Contributor

@FranAguilera FranAguilera left a 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

@murki murki merged commit 1c78758 into bitdriftlabs:main Jul 28, 2025
14 of 15 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants