Skip to content

Commit c277a4a

Browse files
authored
[offheap] reintroduce inlining (#1089)
### Problem The `release` workflow is failing in main since #1072 got merged. The compiler for some reason has trouble to generate scaladocs after the `inline` keywords were removed. ### Solution I've tried to understand why the compiler was having trouble with scaladocs but the error messages don't show any meaningful information. I've decided to reintroduce inlining to check if the scaladoc generation would work but it actually continues failing but I think the change still makes sense.
1 parent e865dbd commit c277a4a

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

kyo-offheap/native/src/main/java/lang/foreign/MemorySegment.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,23 @@ object ValueLayout:
177177
trait OfFloat extends ValueLayout
178178
trait OfDouble extends ValueLayout
179179

180-
case object JAVA_BOOLEAN extends OfBoolean:
180+
val JAVA_BOOLEAN = new OfBoolean:
181181
val byteSize = sizeOf[CBool]
182-
case object JAVA_BYTE extends OfByte:
182+
val JAVA_BYTE = new OfByte:
183183
val byteSize = sizeOf[Byte]
184-
case object JAVA_CHAR extends OfChar:
184+
val JAVA_CHAR = new OfChar:
185185
val byteSize = sizeOf[CChar]
186-
case object JAVA_SHORT extends OfShort:
186+
val JAVA_SHORT = new OfShort:
187187
val byteSize = sizeOf[CShort]
188-
case object JAVA_INT extends OfInt:
188+
val JAVA_INT = new OfInt:
189189
val byteSize = sizeOf[CInt]
190-
case object JAVA_LONG extends OfLong:
190+
val JAVA_LONG = new OfLong:
191191
val byteSize = sizeOf[CLong]
192-
case object JAVA_FLOAT extends OfFloat:
192+
val JAVA_FLOAT = new OfFloat:
193193
val byteSize = sizeOf[CFloat]
194-
case object JAVA_DOUBLE extends OfDouble:
194+
val JAVA_DOUBLE = new OfDouble:
195195
val byteSize = sizeOf[CDouble]
196-
case object ADDRESS extends AddressLayout:
196+
val ADDRESS = new AddressLayout:
197197
val byteSize = sizeOf[Ptr[Byte]]
198198
end ValueLayout
199199

kyo-offheap/shared/src/main/scala/kyo/Memory.scala

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ object Memory:
5959
* @return
6060
* The result of applying f to the new memory segment
6161
*/
62-
def initWith[A: Layout as l](size: Int)[B, S](f: Memory[A] => B < S)(using frame: Frame): B < (Arena & S) =
62+
def initWith[A: Layout as l](size: Int)[B, S](f: Memory[A] => B < S)(using Frame): B < (Arena & S) =
6363
Arena.use { arena =>
6464
IO.Unsafe(f(arena.allocate(l.size * size)))
6565
}
@@ -73,7 +73,7 @@ object Memory:
7373
* @return
7474
* The element at the specified index
7575
*/
76-
def get(index: Int)(using Frame): A < Arena =
76+
inline def get(index: Int)(using inline frame: Frame): A < Arena =
7777
IO.Unsafe {
7878
// TODO inference issue without the val
7979
val x = Unsafe.get(self)(index)
@@ -88,15 +88,15 @@ object Memory:
8888
* @param value
8989
* The value to write
9090
*/
91-
def set(index: Int, value: A)(using Frame): Unit < Arena =
91+
inline def set(index: Int, value: A)(using inline frame: Frame): Unit < Arena =
9292
IO.Unsafe(Unsafe.set(self)(index, value))
9393

9494
/** Fills the entire memory segment with the specified value.
9595
*
9696
* @param value
9797
* The value to fill with
9898
*/
99-
def fill(value: A)(using frame: Frame): Unit < Arena =
99+
inline def fill(value: A)(using inline frame: Frame): Unit < Arena =
100100
IO.Unsafe(Unsafe.fill(self)(value))
101101

102102
/** Folds over all elements in the memory segment.
@@ -108,7 +108,7 @@ object Memory:
108108
* @return
109109
* The final accumulated value
110110
*/
111-
def fold[B](zero: B)(f: (B, A) => B)(using Frame): B < Arena =
111+
inline def fold[B](zero: B)(f: (B, A) => B)(using inline frame: Frame): B < Arena =
112112
IO.Unsafe(Unsafe.fold(self)(zero)(f))
113113

114114
/** Finds the index of the first element satisfying the predicate.
@@ -118,7 +118,7 @@ object Memory:
118118
* @return
119119
* The index wrapped in Maybe, or Absent if not found
120120
*/
121-
def findIndex(p: A => Boolean)(using Frame): Maybe[Int] < Arena =
121+
inline def findIndex(p: A => Boolean)(using inline frame: Frame): Maybe[Int] < Arena =
122122
IO.Unsafe(Unsafe.findIndex(self)(p))
123123

124124
/** Checks if any element satisfies the predicate.
@@ -128,7 +128,7 @@ object Memory:
128128
* @return
129129
* true if any element satisfies the predicate
130130
*/
131-
def exists(p: A => Boolean)(using Frame): Boolean < Arena =
131+
inline def exists(p: A => Boolean)(using inline frame: Frame): Boolean < Arena =
132132
IO.Unsafe(Unsafe.exists(self)(p))
133133

134134
/** Creates a view of a portion of this memory segment.
@@ -208,13 +208,13 @@ object Memory:
208208
/** WARNING: Low-level API meant for integrations, libraries, and performance-sensitive code. See AllowUnsafe for more details. */
209209
object Unsafe:
210210
extension [A: Layout as l](self: Unsafe[A])
211-
def get(index: Int)(using AllowUnsafe): A =
211+
inline def get(index: Int)(using AllowUnsafe): A =
212212
l.get(self, index * l.size)
213213

214-
def set(index: Int, value: A)(using AllowUnsafe): Unit =
214+
inline def set(index: Int, value: A)(using AllowUnsafe): Unit =
215215
l.set(self, index * l.size, value)
216216

217-
def fill(value: A)(using AllowUnsafe): Unit =
217+
inline def fill(value: A)(using AllowUnsafe): Unit =
218218
val len = size
219219
@tailrec def loop(i: Int): Unit =
220220
if i < len then
@@ -223,7 +223,7 @@ object Memory:
223223
loop(0)
224224
end fill
225225

226-
def fold[B](z: B)(f: (B, A) => B)(using AllowUnsafe): B =
226+
inline def fold[B](z: B)(inline f: (B, A) => B)(using AllowUnsafe): B =
227227
val len = size
228228
@tailrec def loop(i: Int, acc: B): B =
229229
if i < len then
@@ -232,7 +232,7 @@ object Memory:
232232
loop(0, z)
233233
end fold
234234

235-
def findIndex(f: A => Boolean)(using AllowUnsafe): Maybe[Int] =
235+
inline def findIndex(inline f: A => Boolean)(using AllowUnsafe): Maybe[Int] =
236236
val len = size
237237
@tailrec def loop(i: Int): Maybe[Int] =
238238
if i < len then
@@ -242,7 +242,7 @@ object Memory:
242242
loop(0)
243243
end findIndex
244244

245-
def exists(f: A => Boolean)(using AllowUnsafe): Boolean =
245+
inline def exists(inline f: A => Boolean)(using AllowUnsafe): Boolean =
246246
findIndex(f) match
247247
case Present(_) => true
248248
case Absent => false
@@ -268,8 +268,8 @@ object Memory:
268268

269269
/** Defines how values of type A are laid out in memory. */
270270
abstract class Layout[A]:
271-
def get(memory: Unsafe[A], offset: Long)(using AllowUnsafe): A
272-
def set(memory: Unsafe[A], offset: Long, value: A)(using AllowUnsafe): Unit
271+
inline def get(memory: Unsafe[A], offset: Long)(using AllowUnsafe): A
272+
inline def set(memory: Unsafe[A], offset: Long, value: A)(using AllowUnsafe): Unit
273273
def size: Long
274274
end Layout
275275

@@ -278,49 +278,49 @@ object Memory:
278278
import ValueLayout.*
279279

280280
given Layout[Byte] with
281-
def get(memory: Unsafe[Byte], offset: Long)(using AllowUnsafe): Byte =
281+
inline def get(memory: Unsafe[Byte], offset: Long)(using AllowUnsafe): Byte =
282282
memory.get(JAVA_BYTE, offset)
283-
def set(memory: Unsafe[Byte], offset: Long, value: Byte)(using AllowUnsafe): Unit =
283+
inline def set(memory: Unsafe[Byte], offset: Long, value: Byte)(using AllowUnsafe): Unit =
284284
memory.set(JAVA_BYTE, offset, value)
285285
def size = JAVA_BYTE.byteSize
286286
end given
287287

288288
given Layout[Short] with
289-
def get(memory: Unsafe[Short], offset: Long)(using AllowUnsafe): Short =
289+
inline def get(memory: Unsafe[Short], offset: Long)(using AllowUnsafe): Short =
290290
memory.get(JAVA_SHORT, offset)
291-
def set(memory: Unsafe[Short], offset: Long, value: Short)(using AllowUnsafe): Unit =
291+
inline def set(memory: Unsafe[Short], offset: Long, value: Short)(using AllowUnsafe): Unit =
292292
memory.set(JAVA_SHORT, offset, value)
293293
def size = JAVA_SHORT.byteSize
294294
end given
295295

296296
given Layout[Int] with
297-
def get(memory: Unsafe[Int], offset: Long)(using AllowUnsafe): Int =
297+
inline def get(memory: Unsafe[Int], offset: Long)(using AllowUnsafe): Int =
298298
memory.get(JAVA_INT, offset)
299-
def set(memory: Unsafe[Int], offset: Long, value: Int)(using AllowUnsafe): Unit =
299+
inline def set(memory: Unsafe[Int], offset: Long, value: Int)(using AllowUnsafe): Unit =
300300
memory.set(JAVA_INT, offset, value)
301301
def size = JAVA_INT.byteSize
302302
end given
303303

304304
given Layout[Long] with
305-
def get(memory: Unsafe[Long], offset: Long)(using AllowUnsafe): Long =
305+
inline def get(memory: Unsafe[Long], offset: Long)(using AllowUnsafe): Long =
306306
memory.get(JAVA_LONG, offset)
307-
def set(memory: Unsafe[Long], offset: Long, value: Long)(using AllowUnsafe): Unit =
307+
inline def set(memory: Unsafe[Long], offset: Long, value: Long)(using AllowUnsafe): Unit =
308308
memory.set(JAVA_LONG, offset, value)
309309
def size = JAVA_LONG.byteSize
310310
end given
311311

312312
given Layout[Float] with
313-
def get(memory: Unsafe[Float], offset: Long)(using AllowUnsafe): Float =
313+
inline def get(memory: Unsafe[Float], offset: Long)(using AllowUnsafe): Float =
314314
memory.get(JAVA_FLOAT, offset)
315-
def set(memory: Unsafe[Float], offset: Long, value: Float)(using AllowUnsafe): Unit =
315+
inline def set(memory: Unsafe[Float], offset: Long, value: Float)(using AllowUnsafe): Unit =
316316
memory.set(JAVA_FLOAT, offset, value)
317317
def size = JAVA_FLOAT.byteSize
318318
end given
319319

320320
given Layout[Double] with
321-
def get(memory: Unsafe[Double], offset: Long)(using AllowUnsafe): Double =
321+
inline def get(memory: Unsafe[Double], offset: Long)(using AllowUnsafe): Double =
322322
memory.get(JAVA_DOUBLE, offset)
323-
def set(memory: Unsafe[Double], offset: Long, value: Double)(using AllowUnsafe): Unit =
323+
inline def set(memory: Unsafe[Double], offset: Long, value: Double)(using AllowUnsafe): Unit =
324324
memory.set(JAVA_DOUBLE, offset, value)
325325
def size = JAVA_DOUBLE.byteSize
326326
end given

0 commit comments

Comments
 (0)