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

Faster String#squish #1159

Merged
merged 3 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
89 changes: 77 additions & 12 deletions bench.cr
Original file line number Diff line number Diff line change
@@ -1,21 +1,86 @@
require "benchmark"

text = <<-TEXT
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
TEXT
class String
# Original
def squish_regex : String
gsub(/[[:space:]]+/, " ").strip
end

string_text = text * 40_000
io_text = IO::Memory.new(string_text)
def squish_new : String
if ascii_only?
squish_ascii
else
squish_unicode
end
end

Benchmark.ips do |x|
x.report("string") do
io = IO::Memory.new
def squish_simplified : String
squish_unicode
end

io.print(io_text.to_s)
private def squish_ascii : String
String.build(size) do |str|
print_blank = false
each_char do |chr|
if chr.ascii_whitespace?
if print_blank
str << ' '
print_blank = false
end
else
print_blank = true
str << chr
end
end
end.strip
end
x.report("io") do
io = IO::Memory.new

io.print(io_text)
private def squish_unicode : String
String.build(size) do |str|
print_blank = false
each_char do |chr|
if chr.whitespace?
if print_blank
str << ' '
print_blank = false
end
else
print_blank = true
str << chr
end
end
end.strip
end
end

puts "Sanity check the return output is consistent:"
example = " f f\u00A0\u00A0\u00A0f f \n \t \v\v \f\f 11111 a l0* あ\u00A0\u00A0\u00A0 "
Copy link
Member

Choose a reason for hiding this comment

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

🚀

puts "String to squish " + example.inspect
puts "regex: " + example.squish_regex.inspect
puts "new: " + example.squish_new.inspect
puts "simplified: " + example.squish_simplified.inspect

# Original regex doesn't seem to work correctly with trailing unicode
if example.squish_regex != example.squish_new
puts "WARN: regex version does not match:"
puts "Regex: #{example.squish_regex.inspect}".ljust(50)
puts "Ours: #{example.squish_new.inspect}".ljust(50)
end

puts
puts "Benchmarking String#squish ..."
puts
example = " f f f f \n \t\r\r 11111 a l0* " * 20
Benchmark.ips(warmup: 5, calculation: 10) do |x|
x.report("squish regex ascii-only whitespace (#{example.bytesize} bytes)") { example.squish_regex }
x.report("squish optimized ascii-only whitespace (#{example.bytesize} bytes)") { example.squish_new }
x.report("squish simplified ascii-only whitespace (#{example.bytesize} bytes)") { example.squish_simplified }
end

puts
example = "あ\u00A0あ\u00A0\u00A0 \t z \n \r \r \t \v \f zzzz XXX asdf k ; ;, \u1680\u1680 " * 10
Benchmark.ips(warmup: 5, calculation: 10) do |x|
x.report("squish regex w/ unicode whitespace (#{example.bytesize} bytes)") { example.squish_regex }
x.report("squish optimized w/ unicode whitespace (#{example.bytesize} bytes)") { example.squish_new }
x.report("squish simplified w/ unicode whitespace (#{example.bytesize} bytes)") { example.squish_simplified }
end
7 changes: 3 additions & 4 deletions spec/charms/string_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ require "../spec_helper"

describe "String charm" do
describe "squish" do
it "squishes the text by removing newlines and extra whitespace" do
og_string = " foo bar \n \t boo"
Copy link
Member

Choose a reason for hiding this comment

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

Can we also leave this in? I dig the new spec, but I'm thinking leave both of them in just for the extra safety. What are your thoughts?

it "squishes the text by removing ascii/unicode whitespace" do
og_string = "\u1680 \v\v\v\v\v\r\r\r\r hello foo bar\n\u00A0\t\t\u00A0\u1680\u1680 "

og_string.squish.should eq("foo bar boo")
og_string.should eq(" foo bar \n \t boo")
og_string.squish.should eq("hello foo bar")
end
end
end
41 changes: 40 additions & 1 deletion src/charms/string_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,45 @@ class String
# replace newlines with a single space and convert mutiple spaces to just one
# space.
def squish : String
gsub(/[[:space:]]+/, " ").strip
if ascii_only?
squish_ascii
else
squish_unicode
end
end

# Optimized for ASCII using String#ascii_whitespace?
private def squish_ascii : String
String.build(size) do |str|
print_blank = false
each_char do |chr|
if chr.ascii_whitespace?
if print_blank
str << ' '
print_blank = false
end
else
print_blank = true
str << chr
end
end
end.strip
end

private def squish_unicode : String
String.build(size) do |str|
print_blank = false
each_char do |chr|
if chr.whitespace?
if print_blank
str << ' '
print_blank = false
end
else
print_blank = true
str << chr
end
end
end.strip
end
end