Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load data with Marshal instead of YAML #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'bundler/gem_tasks'
require 'yaml'
require 'benchmark'

task :convert do
file 'lib/data/US.yml' do
codes = {}
File.open('lib/data/US.txt').each_line do |line|
array = line.split('\t')
Expand All @@ -16,3 +17,21 @@ task :convert do
file.write codes.to_yaml
end
end

file 'lib/data/US.marshal' => 'lib/data/US.yml' do
File.write('lib/data/US.marshal', Marshal.dump(YAML.load(File.read('lib/data/US.yml'))))
end

task :convert => ['lib/data/US.marshal', 'lib/data/US.yml']

task :benchmark => :convert do
Benchmark.bm(15) do |x|
x.report("YAML.load") do
YAML.load(File.read('lib/data/US.yml'))
end

x.report("Marshal.load") do
Marshal.load(File.read('lib/data/US.marshal'))
end
end
end
Binary file added lib/data/US.marshal
Binary file not shown.
6 changes: 2 additions & 4 deletions lib/zip-codes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'yaml'

module ZipCodes
VERSION = '0.2.1'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change (semver-1)

Suggested change
VERSION = '0.2.1'
VERSION = '1.0'

If it's going to be included one day, I think the PR needs to include a build script, that convert a YAML (easy for humans to deal with) into the marshaled format.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to also open a PR against https://github.com/suvie-eng/zip-codes/

I'm not wanting to create maintainer controversy - but until the maintainers here integrate all of these, I'd like to have a gem that is the most up to date. Also, I like the performance of this change 👌


Expand All @@ -11,8 +9,8 @@ def identify(code)
def db
@db ||= begin
this_file = File.expand_path(File.dirname(__FILE__))
us_data = File.join(this_file, 'data', 'US.yml')
YAML.load(File.open(us_data))
us_data = File.join(this_file, 'data', 'US.marshal')
Marshal.load(File.open(us_data))
end
end

Expand Down