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

Implement String#undump in C extension library #1

Merged
merged 42 commits into from
Nov 18, 2017
Merged

Implement String#undump in C extension library #1

merged 42 commits into from
Nov 18, 2017

Conversation

tadd
Copy link
Owner

@tadd tadd commented Nov 18, 2017

Add String#undump_roughly, a C extension implementation of String#undump.

Now undump with Ruby trunk is over 9 times faster than eval 🎉

$ cat bm.rb 
require 'string_undump'

def measure1
  t = Time.now
  N.times{yield}
  Time.now - t
end

def measure(&block)
  3.times.map{measure1(&block)}.min
end

N = 100_000
s = 'あいうえお'.dump

print "eval: "
e = measure{eval(s)}
puts e

print "undump: "
u = measure{s.undump}
puts u

puts "undump is #{e/u} times faster!"
$ ruby-trunk bm.rb 
eval: 0.502793878
undump: 0.051393841
undump is 9.783154327772465 times faster!

@@ -6,21 +6,21 @@ See [Feature #12275](https://bugs.ruby-lang.org/issues/12275) for details.
## Usage

```ruby
require 'string-undump'
require 'string_undump'
Copy link
Owner Author

Choose a reason for hiding this comment

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

Yes, the name of this library is renamed from string-undump (hyphen) to string_undump (underscore) due to naming convention of C extension.

s.gsub!(/(?:\\x#{hex}{2})+/) {|m|
m.gsub(/\\x/, '').scan(/../).map(&:hex).pack("C*").force_encoding(e)
}
s
end

alias_method(:undump, :undump_badly) unless self.new.respond_to?(:undump)
alias_method(:undump, :undump_roughly) unless self.new.respond_to?(:undump)
Copy link
Owner Author

Choose a reason for hiding this comment

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

Ruby implementation remains as String#undump_badly.

@tadd tadd merged commit e5d0263 into master Nov 18, 2017
@tadd tadd deleted the ext branch November 18, 2017 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant