Current version 1.0.2

Matrix reduce memory usage for large Hash.

Matrix store the hash like this:
# # '234234' => {'id' => '23232', 'name' => 'haha',
# '234235' => => '23235', 'name' => 'haha2', 'hp' => '232'
# '234236' => => '23235', 'name' => 'haha2', 'attack' => '2322'
# }
For example, loading a game config ./spec/equipment_strengthen.plist will use 480M when using ruby Hash like this:
# => {id: '23423', name: 'haha'}
Because there are about 1440000 ruby String objects when loading to memory.
And it will use 5M when using SMatrix.
Result from test: rspec ./spec/performance_spec.rb

Installation

Add this line to your application's Gemfile:

gem 's_matrix', '~>1.0'

And then execute:

$ bundle

Or install it yourself as:

$ gem install s_matrix

Usage

initialize the config const

# every thing will change to string (.to_s) when saving to SMatrix
EQUIPMENTS = SMatrix.new
EQUIPMENTS.add_row('10001', => '10001', 'name' => 'wood sword', 'attack' => '342')
EQUIPMENTS.add_row('20002', 10002, name: 'shoe', speed: 5)

use the config const

# everything get back will be string
EQUIPMENTS['10001'].should == => '10001', 'name' => 'wood sword', 'attack' => '342''
EQUIPMENTS['10001']['id'].should == '10001'
EQUIPMENTS['10001'][:id].should == '10001'
EQUIPMENTS['20002'].should == => '10002', 'name' => 'shoe', 'speed' => '5'
EQUIPMENTS['20002']['attack'].should == '552'
EQUIPMENTS.size.should == 2 # or you can use get_row instead of []
EQUIPMENTS.get_row('20002')
# each
EQUIPMENTS.each {|k, v| k == '10001', v == {'id' => '10001', 'name' => 'sword', ...} }
# all
EQUIPMENTS.all.should == { '20002' => => '10002', 'name' => 'shoe', 'speed' => '5', '20003' => {} }
# first
EQUIPMENTS.first.should == ['20002', => '10002', 'name' => 'shoe', 'speed' => '5'] # ids
EQUIPMENTS.ids.should == ['20002', '20003']
# keys
EQUIPMENTS.keys.should == ['id', 'name', 'hp']
# find -- find the first one
EQUIPMENTS.find('shoe').should == ['20002', => '10002', 'name' => 'shoe', 'speed' => '5'] # to_s for debug
puts EQUIPMENTS.to_s

todolist

https://github.com/libinzhangyuan/s\_matrix/blob/master/todolist.txt