开源项目 s_matrix - 解决 ruby 服务器内存占用问题

zyfire · 2015年01月05日 · 1749 次阅读

大量的小对象在 ruby 中存储也会占用很大的内存。 比如游戏开发中配置文件 excel,可能十几 M, 加载到 ruby 中占用 好几百 M 内存. 使用 SMatrix 后,又只占用几 M 了。

https://github.com/libinzhangyuan/s_matrix

every thing will change to string (.to_s) when saving to SMatrix

EQUIPMENTS = SMatrix.new EQUIPMENTS.add_row('10001', {'id' => '10001', 'name' => 'wood sword', 'attack' => '342'}) EQUIPMENTS.add_row('20002', {id: 10002, name: 'shoe', speed: 5})

everything get back will be string

EQUIPMENTS['10001'].should == {'id' => '10001', 'name' => 'wood sword', 'attack' => '342''} EQUIPMENTS['10001']['id'].should == '10001' EQUIPMENTS['10001'][:id].should == '10001' EQUIPMENTS['20002'].should == {'id' => '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' => {'id' => '10002', 'name' => 'shoe', 'speed' => '5'}, '20003' => {} }

first

EQUIPMENTS.first.should == ['20002', {'id' => '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({name: 'shoe'}).should == ['20002', {'id' => '10002', 'name' => 'shoe', 'speed' => '5'}] # to_s for debug

使用 c++ 开发 高质量代码,有测试,请放心使用。 谢谢关注。

1 楼 已删除
需要 登录 后方可回复, 如果你还没有账号请 注册新账号