-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RUM-6286: replacing
joinToString
when it possible, refactor a bit
- Loading branch information
Showing
11 changed files
with
184 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/StringBuilderExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.core | ||
|
||
/** | ||
* This utility function helps replace joinToString calls with more efficient StringBuilder's methods calls. | ||
* In case when content should be separated with some separator it's handy to add it in front of new string only | ||
* if buffer already contain some data | ||
* @param str - string that should be added into buffer only if it already contains some data | ||
*/ | ||
fun StringBuilder.appendIfNotEmpty(str: String) = apply { | ||
if (isNotEmpty()) append(str) | ||
} | ||
|
||
/** | ||
* This utility function helps replace joinToString calls with more efficient StringBuilder's methods calls. | ||
* In case when content should be separated with some separator it's handy to add it in front of new string only | ||
* if buffer already contain some data | ||
* @param char - char that should be added into buffer only if it already contains some data | ||
*/ | ||
fun StringBuilder.appendIfNotEmpty(char: Char) = apply { | ||
if (isNotEmpty()) append(char) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
dd-sdk-android-core/src/test/kotlin/com/datadog/android/core/StringBuilderExtKtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.datadog.android.core | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
class StringBuilderExtKtTest { | ||
|
||
@Test | ||
fun `M add char W addIfNotEmpty {buffer is not empty}`() { | ||
// Given | ||
val initialData = "something inside buffer already" | ||
val buffer = StringBuilder(initialData) | ||
|
||
// When | ||
buffer.appendIfNotEmpty(' ') | ||
|
||
// Then | ||
|
||
assertThat(buffer.toString()).isEqualTo("$initialData ") | ||
} | ||
|
||
@Test | ||
fun `M add str W addIfNotEmpty {buffer is not empty}`() { | ||
// Given | ||
val initialData = "something inside buffer already" | ||
val buffer = StringBuilder(initialData) | ||
|
||
// When | ||
buffer.appendIfNotEmpty(" ") | ||
|
||
// Then | ||
|
||
assertThat(buffer.toString()).isEqualTo("$initialData ") | ||
} | ||
|
||
@Test | ||
fun `M not add any char W addIfNotEmpty {buffer is empty}`() { | ||
// Given | ||
val buffer = StringBuilder() | ||
|
||
// When | ||
buffer.appendIfNotEmpty(' ') | ||
|
||
// Then | ||
|
||
assertThat(buffer.toString()).isEqualTo("") | ||
} | ||
|
||
@Test | ||
fun `M not add any str W addIfNotEmpty {buffer is empty}`() { | ||
// Given | ||
val buffer = StringBuilder() | ||
|
||
// When | ||
buffer.appendIfNotEmpty(" ") | ||
|
||
// Then | ||
|
||
assertThat(buffer.toString()).isEqualTo("") | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
dd-sdk-android-core/src/test/kotlin/com/datadog/android/utils/PerformanceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
package com.datadog.android.utils | ||
|
||
import com.datadog.android.utils.forge.Configurator | ||
import fr.xgouchet.elmyr.Forge | ||
import fr.xgouchet.elmyr.junit5.ForgeConfiguration | ||
import fr.xgouchet.elmyr.junit5.ForgeExtension | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.junit.jupiter.api.extension.Extensions | ||
import kotlin.system.measureNanoTime | ||
|
||
private const val ITEMS_TO_JOINT = 10_000 | ||
private const val REPETITION_COUNT = 10_000 | ||
|
||
@Extensions( | ||
ExtendWith(ForgeExtension::class) | ||
) | ||
@ForgeConfiguration(Configurator::class) | ||
internal class PerformanceTest { | ||
|
||
private fun List<Long>.mean() = sum().toDouble() / size | ||
|
||
@Test | ||
fun `M be faster than joinToString W buildString`(forge: Forge) { | ||
val itemsForJoin = forge.aList(ITEMS_TO_JOINT) { forge.aString() } | ||
val jointToStringResults = mutableListOf<Long>() | ||
val builderResults = mutableListOf<Long>() | ||
|
||
var jointToStringResult = "" | ||
var builderResult = "" | ||
|
||
for (i in 0..REPETITION_COUNT) { | ||
jointToStringResults.add( | ||
measureNanoTime { | ||
val jointToStringContainer = mutableListOf<String>() | ||
for (item in itemsForJoin) { | ||
jointToStringContainer.add(item) | ||
} | ||
jointToStringResult = jointToStringContainer.joinToString(separator = " ") { it } | ||
} | ||
) | ||
|
||
builderResults.add( | ||
measureNanoTime { | ||
builderResult = buildString { | ||
itemsForJoin.forEachIndexed { i, item -> | ||
if (i > 0) append(" ") | ||
append(item) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
|
||
assertThat(builderResult).isEqualTo(jointToStringResult) // same result | ||
assertThat(builderResults.mean()).isLessThan(jointToStringResults.mean()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters