The following code is the hottest spot in my program.
JAVA_OPTS = -Xprof output:
Compiled + native Method 5.7% 173 + 0 scala.collection.IndexedSeqOptimized$class.slice 5.1% 156 + 0 scala.collection.IndexedSeqOptimized$class.foreach 2.9% 87 + 0 java.util.regex.Pattern$BmpCharProperty.match 2.5% 76 + 0 scala.collection.IndexedSeqOptimized$class.sameElements 2.4% 73 + 0 trafacct.SubNet.contains
Slimes, the same Elements, and even foreach calls seem to be most used here. Can someone give some advice or two on how to optimize the contains()
method? Maybe some methods allow you to analyze Bytes without converting them to integers? Or a solid approach of a whole sequence without a cut?
The SubNet.contains () function corresponds to the IP address for the subnet.
object SubNet { def toInts(bytes: Seq[Byte]): Seq[Int] = bytes.map(_.toInt & 0xFF) } case class SubNet(ip:InetAddress, maskLength:Int) extends HostCategory { import SubNet.toInts private val bytes: Int = maskLength / 8 private val subnet = toInts(ip.getAddress) private val bits = bytes * 8 - maskLength def contains(host: Host) = { if (host.ip == null && ip == null) { true } else if (this.ip == null) { false } else { val address = toInts(host.ip.getAddress) if (address.length != subnet.length) { false } else { if (address.slice(0, bytes) != subnet.slice(0, bytes)) { false } else { ((address(bytes) >> (8-bits) ^ subnet(bytes) >> (8-bits)) & 0xFF) == 0 } } } } }
I understand that this optimization will not give me much better bandwidth, I just feel like I am doing something wrong, I spend so much time on this simple function.
This code must be compatible with IPv6 (16 bytes), and I don't like the idea of โโprocessing IPv4 code separately.
source share