Skip to content
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

Faster unsafe.Ptr.{apply, update} by skipping unnecessary GC allocations #3522

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 45 additions & 17 deletions nativelib/src/main/scala/scala/scalanative/unsafe/Ptr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import scala.language.implicitConversions
import scala.scalanative.annotation.alwaysinline
import scala.scalanative.runtime.Intrinsics._
import scala.scalanative.runtime._
import scala.scalanative.unsigned.USize
import scala.scalanative.unsigned._

final class Ptr[T] private[scalanative] (
private[scalanative] val rawptr: RawPtr
Expand All @@ -15,10 +15,8 @@ final class Ptr[T] private[scalanative] (

@alwaysinline override def equals(other: Any): Boolean =
(this eq other.asInstanceOf[AnyRef]) || (other match {
case other: Ptr[_] =>
other.rawptr == rawptr
case _ =>
false
case other: Ptr[_] => other.rawptr == rawptr
case _ => false
})

@alwaysinline override def toString: String =
Expand All @@ -36,37 +34,67 @@ final class Ptr[T] private[scalanative] (
@alwaysinline def `unary_!_=`(value: T)(implicit tag: Tag[T]): Unit =
tag.store(this, value)

@alwaysinline def +(offset: Int)(implicit tag: Tag[T]): Ptr[T] =
new Ptr[T](elemRawPtr(rawptr, castIntToRawSize(offset * tag.size.toInt)))

@alwaysinline def +(offset: Size)(implicit tag: Tag[T]): Ptr[T] =
new Ptr(elemRawPtr(rawptr, (offset * tag.size.toSize).rawSize))
new Ptr[T](elemRawPtr(rawptr, toRawSize(offset * tag.size.toSize)))

@alwaysinline def +(offset: USize)(implicit tag: Tag[T]): Ptr[T] =
new Ptr(elemRawPtr(rawptr, (offset * tag.size).rawSize))
new Ptr[T](elemRawPtr(rawptr, toRawSize(offset * tag.size)))

@alwaysinline def -(offset: Int)(implicit tag: Tag[T]): Ptr[T] =
new Ptr[T](
elemRawPtr(rawptr, castIntToRawSize(-offset * tag.size.toInt))
)

@alwaysinline def -(offset: Size)(implicit tag: Tag[T]): Ptr[T] =
new Ptr(elemRawPtr(rawptr, (-offset * tag.size.toSize).rawSize))
new Ptr[T](elemRawPtr(rawptr, toRawSize(-offset * tag.size.toSize)))

@alwaysinline def -(offset: USize)(implicit tag: Tag[T]): Ptr[T] =
new Ptr(elemRawPtr(rawptr, (-((offset * tag.size).toSize)).rawSize))
new Ptr[T](
elemRawPtr(rawptr, toRawSize(-offset.toSize * tag.size.toSize))
)

@alwaysinline def -(other: Ptr[T])(implicit tag: Tag[T]): CPtrDiff = {
val left = if (is32BitPlatform) this.toInt.toSize else this.toLong.toSize
val right = if (is32BitPlatform) other.toInt.toSize else other.toLong.toSize
(left - right) / tag.size.toSize
if (is32BitPlatform) (this.toInt - other.toInt).toSize / tag.size.toSize
else (this.toLong - other.toLong).toSize / tag.size.toSize
}

@alwaysinline def apply(offset: Int)(implicit tag: Tag[T]): T =
tag.load(
elemRawPtr(rawptr, castIntToRawSize(offset * tag.size.toInt))
)

@alwaysinline def apply(offset: USize)(implicit tag: Tag[T]): T =
(this + offset).`unary_!`
tag.load(elemRawPtr(rawptr, toRawSize(offset * tag.size)))

@alwaysinline def apply(offset: Size)(implicit tag: Tag[T]): T =
(this + offset).`unary_!`
tag.load(elemRawPtr(rawptr, toRawSize(offset * tag.size.toSize)))

@alwaysinline def update(offset: Int, value: T)(implicit
tag: Tag[T]
): Unit =
tag.store(
elemRawPtr(rawptr, castIntToRawSize(offset * tag.size.toInt)),
value
)

@alwaysinline def update(offset: USize, value: T)(implicit
tag: Tag[T]
): Unit =
(this + offset).`unary_!_=`(value)
tag.store(
elemRawPtr(rawptr, toRawSize(offset * tag.size)),
value
)

@alwaysinline def update(offset: Size, value: T)(implicit tag: Tag[T]): Unit =
(this + offset).`unary_!_=`(value)
@alwaysinline def update(offset: Size, value: T)(implicit
tag: Tag[T]
): Unit =
tag.store(
elemRawPtr(rawptr, toRawSize(offset * tag.size.toSize)),
value
)
}

object Ptr {
Expand Down