Top Level Namespace

Defined Under Namespace

Modules: BatchJaroWinkler

Instance Method Summary collapse

Instance Method Details

#encode_utf32_le_without_memory_leak(str) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/batch_jaro_winkler.rb', line 34

def encode_utf32_le_without_memory_leak(str)
  str = str.encode('utf-32')
  # Ignore BOM, restore correct byte order within codepoint
  str = (str.bytes[4..-1] || []).map(&:chr).each_slice(4).map{ |c| c.reverse }.flatten.join
  str.force_encoding('utf-32le')
  str
end

#version_with_memory_leak?(version) ⇒ Boolean

Memory leak with older MRI versions which you can reproduce with the following program (at least on macOS 10.15.3 19D76): while true do

1000.times do
  # random 10 characters string
  str = (0...10).map{ (65 + rand(26)).chr }.join
  str.encode('utf-32le')
end
GC.start(full_mark: true, immediate_sweep: true)
GC.start

end Change utf-32le to utf-32 to watch to memory leak vanish This is the fix that was deployed: github.com/ruby/ruby/compare/v2_6_4…v2_6_5#diff-7a2f2c7dfe0bf61d38272aeaf68ac768R2117

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/batch_jaro_winkler.rb', line 19

def version_with_memory_leak?(version)
  major, minor, patch = version.split('.')
  memory_leak = false
  if !major.nil? && major.to_i <= 2 && !minor.nil? && minor.to_i <= 6
    major, minor, patch = major.to_i, minor.to_i, patch.to_i
    memory_leak = true
    if major == 2 && minor == 6 && patch >= 5
      memory_leak = false
    elsif major == 2 && minor == 5 && patch >= 8
      memory_leak = false
    end
  end
  memory_leak
end