-
-
Notifications
You must be signed in to change notification settings - Fork 158
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
jwoertink
merged 3 commits into
luckyframework:master
from
jamescook:james/faster-squish
May 30, 2020
Merged
Faster String#squish #1159
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 " | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀