Class: OpenASN::BinaryFormat::ArraysBase

Inherits:
Object
  • Object
show all
Defined in:
lib/openasn/binary_format.rb

Overview

Arrays mode: unpack once into parallel Integer arrays. Measured on the real dataset: full lookups drop from ~15µs to ~9µs (the raw range probe alone is ~2µs, but IP parsing + overlay checks dominate the classify pipeline) at several× the memory. Worth it for lookup-heavy batch paths; configure via memory_mode :arrays.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes, family) ⇒ ArraysBase

Returns a new instance of ArraysBase.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/openasn/binary_format.rb', line 148

def initialize(bytes, family)
  asz = BinaryFormat.addr_size(family)
  rec = BinaryFormat.base_rec_size(family)
  @count = bytes.bytesize / rec
  @starts = Array.new(@count)
  @ends   = Array.new(@count)
  @asns   = Array.new(@count)
  @flags  = Array.new(@count)
  unpack_addr = family == :ipv4 ? ->(s) { s.unpack1("N") } : ->(s) { hi, lo = s.unpack("Q>Q>"); (hi << 64) | lo }
  @count.times do |i|
    off = i * rec
    @starts[i] = unpack_addr.call(bytes[off, asz])
    @ends[i]   = unpack_addr.call(bytes[off + asz, asz])
    @asns[i], @flags[i] = bytes[off + 2 * asz, 6].unpack("Nn")
  end
  [@starts, @ends, @asns, @flags].each(&:freeze)
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



146
147
148
# File 'lib/openasn/binary_format.rb', line 146

def count
  @count
end

Instance Method Details

#find(ip_int) ⇒ Object



166
167
168
169
170
171
# File 'lib/openasn/binary_format.rb', line 166

def find(ip_int)
  i = bsearch_le(@starts, ip_int)
  return nil unless i && ip_int <= @ends[i]

  [@asns[i], @flags[i]]
end