Use Float instead of Double for all the level metering logic. (#1645)

This is in preparation of further changes to the way the audio level is computed and to allow recording and sending of the waveform. The main reasoning behind the change is twofold:
1) We don't need the precision of Double in our context (we just need a rough indication of the changes in audio level to successfully draw a level meter or a waveform in our UI).
2) Performance: It is true that on 64 bit CPUs single operations involving Floats or Doubles take the same amount of time (i.e one clock cycle). But there are other aspects here that vouch in favor of Floats:
	- A float takes half the space in memory compared to a double, so when storing long lists of them this can add up.
	- On Android O and greater the ART runtime can "vectorize" certain operations on lists and make use of the CPU's SIMD registers which are generally 128 bits. So by using floats 4 of them can fit and be computed at the same time whilst with doubles only 2 will fit halving the throughput.

References:
- https://source.android.com/docs/core/runtime/improvements
- https://www.slideshare.net/linaroorg/automatic-vectorization-in-art-android-runtime-sfo17216
This commit is contained in:
Marco Romano 2023-10-26 14:55:23 +02:00 committed by GitHub
parent b9b3bce2a2
commit 3ec62ad58a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 22 additions and 22 deletions

View file

@ -778,7 +778,7 @@ internal fun TextComposerVoicePreview() = ElementPreview {
enableVoiceMessages = true,
)
PreviewColumn(items = persistentListOf({
VoicePreview(voiceMessageState = VoiceMessageState.Recording(61.seconds, 0.5))
VoicePreview(voiceMessageState = VoiceMessageState.Recording(61.seconds, 0.5f))
}, {
VoicePreview(voiceMessageState = VoiceMessageState.Preview)
}, {

View file

@ -42,7 +42,7 @@ import kotlin.time.Duration.Companion.seconds
@Composable
internal fun VoiceMessageRecording(
level: Double,
level: Float,
duration: Duration,
modifier: Modifier = Modifier,
) {
@ -79,7 +79,7 @@ internal fun VoiceMessageRecording(
@Composable
private fun DebugAudioLevel(
level: Double,
level: Float,
modifier: Modifier = Modifier,
) {
Box(
@ -89,7 +89,7 @@ private fun DebugAudioLevel(
Box(
modifier = Modifier
.align(Alignment.CenterEnd)
.fillMaxWidth(level.toFloat())
.fillMaxWidth(level)
.background(ElementTheme.colors.iconQuaternary, shape = MaterialTheme.shapes.small)
.fillMaxHeight()
)
@ -108,5 +108,5 @@ private fun RedRecordingDot(
@PreviewsDayNight
@Composable
internal fun VoiceMessageRecordingPreview() = ElementPreview {
VoiceMessageRecording(0.5, 0.seconds)
VoiceMessageRecording(0.5f, 0.seconds)
}

View file

@ -25,6 +25,6 @@ sealed class VoiceMessageState {
data object Sending: VoiceMessageState()
data class Recording(
val duration: Duration,
val level: Double,
val level: Float,
): VoiceMessageState()
}