Skip to content

Commit

Permalink
feat: nio key set optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
yankun1992 committed Oct 21, 2024
1 parent 5f6f878 commit 3c68e23
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import java.nio.channels.{SelectionKey, SocketChannel}
import scala.collection.mutable
import scala.language.unsafeNulls

class NioUnsafeSocketChannel(channel: AbstractChannel, ch: SocketChannel, readInterestOp: Int)
final class NioUnsafeSocketChannel(channel: AbstractChannel, ch: SocketChannel, readInterestOp: Int)
extends AbstractNioUnsafeChannel[SocketChannel](channel, ch, readInterestOp) {

private var flushQueue: mutable.ArrayDeque[FileRegion | RecyclablePageBuffer] = _
Expand Down Expand Up @@ -134,12 +134,13 @@ class NioUnsafeSocketChannel(channel: AbstractChannel, ch: SocketChannel, readIn
buf.next = null
if (!closed) {
val writable = buf.readableBytes
val byteBuffer = buf.byteBuffer
val byteBuffer = buf.underlying
byteBuffer.limit(buf.writerOffset)
byteBuffer.position(buf.readerOffset)
try {
val write = ch.write(byteBuffer)
if (write != writable) {
if (write == writable) buf.close()
else {
continue = false
buf.skipReadableBytes(write)
buf.next = cursor
Expand All @@ -148,7 +149,7 @@ class NioUnsafeSocketChannel(channel: AbstractChannel, ch: SocketChannel, readIn
val interestOps = _selectionKey.interestOps()
if ((interestOps & SelectionKey.OP_WRITE) == 0)
_selectionKey.interestOps(interestOps | SelectionKey.OP_WRITE)
} else buf.close()
}
} catch {
case e: IOException =>
unsafeClose(Some(e))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ final class NioHandler(val selectorProvider: SelectorProvider, val selectStrateg
override def isCompatible(handleType: Class[? <: AbstractChannel]): Boolean = ???
// classOf[AbstractNioChannel[?, ?]].isAssignableFrom(handleType)

@throws[IOException]
private def selectNow(): Int = selector.selectNow()

private def select(context: IoExecutionContext): Unit = {
Expand All @@ -410,7 +409,7 @@ final class NioHandler(val selectorProvider: SelectorProvider, val selectStrateg
// It might be pended until idle timeout if IdleStateHandler existed in pipeline.
selector.selectNow()
} else {
selector.select(10)
selector.select()
}
} catch {
case e: CancelledKeyException => // Harmless exception - log anyway
Expand All @@ -421,7 +420,6 @@ final class NioHandler(val selectorProvider: SelectorProvider, val selectStrateg
}
}

@throws[IOException]
private def selectRebuildSelector(selectCnt: Int): Selector = {
// The selector returned prematurely many times in a row.
// Rebuild the selector to work around the problem.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package cc.otavia.core.transport.reactor.nio

import java.nio.channels.SelectionKey
import java.util
import java.util.Arrays
import scala.language.unsafeNulls

final class SelectedSelectionKeySet extends util.AbstractSet[SelectionKey] {
Expand All @@ -37,21 +36,21 @@ final class SelectedSelectionKeySet extends util.AbstractSet[SelectionKey] {

override def remove(o: Any): Boolean = false

override def contains(o: Any): Boolean = false

// override def contains(o: Any): Boolean = {
// var i = 0
// var continue = true
// var res = false
// while (continue && i < _size) {
// val key = keys(i)
// if (key.equals(o)) {
// continue = false
// res = true
// } else i += 1
// }
// res
// }
// override def contains(o: Any): Boolean = false

override def contains(o: Any): Boolean = {
var i = 0
var continue = true
var res = false
while (continue && i < _size) {
val key = keys(i)
if (key.equals(o)) {
continue = false
res = true
} else i += 1
}
res
}

override def size(): Int = _size

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

package cc.otavia.core.transport.reactor.nio

import cc.otavia.core.transport.reactor.nio.SelectedSelectionKeySet

import java.io.IOException
import java.nio.channels.spi.SelectorProvider
import java.nio.channels.{SelectionKey, Selector}
import java.util
Expand All @@ -39,19 +36,16 @@ final class SelectedSelectionKeySetSelector(

override def selectedKeys(): util.Set[SelectionKey] = delegate.selectedKeys()

@throws[IOException]
override def selectNow(): Int = {
selectionKeys.reset()
delegate.selectNow()
}

@throws[IOException]
override def select(timeout: Long): Int = {
selectionKeys.reset()
delegate.select(timeout)
}

@throws[IOException]
override def select(): Int = {
selectionKeys.reset()
delegate.select()
Expand Down

0 comments on commit 3c68e23

Please sign in to comment.