Skip to content

Commit

Permalink
Merge pull request #103 from JakeWharton/patch-1
Browse files Browse the repository at this point in the history
Remove superfluous value reads after write
  • Loading branch information
samhill303 authored Mar 5, 2024
2 parents f930beb + 4197926 commit 6f97449
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ actual class AtomicInt actual constructor(initialValue: Int) {
private var internalValue: Int = initialValue

actual fun addAndGet(delta: Int): Int {
internalValue += delta
return internalValue
return (internalValue + delta).also {
internalValue = it
}
}

actual fun compareAndSet(expected: Int, new: Int): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ actual class AtomicLong actual constructor(initialValue: Long) {
private var internalValue: Long = initialValue

actual fun addAndGet(delta: Long): Long {
internalValue += delta
return internalValue
return (internalValue + delta).also {
internalValue = it
}
}

actual fun compareAndSet(expected: Long, new: Long): Boolean {
Expand Down

0 comments on commit 6f97449

Please sign in to comment.