fast_blank-1.0.1/0000755000004100000410000000000014115406277013654 5ustar www-datawww-datafast_blank-1.0.1/README.md0000644000004100000410000001357114115406277015142 0ustar www-datawww-data### `String#blank?` Ruby Extension [![Gem Version](https://badge.fury.io/rb/fast_blank.png)](http://badge.fury.io/rb/fast_blank) [![Build Status](https://travis-ci.org/SamSaffron/fast_blank.png?branch=master)](https://travis-ci.org/SamSaffron/fast_blank) `fast_blank` is a simple C extension which provides a fast implementation of [Active Support's `String#blank?` method](http://api.rubyonrails.org/classes/String.html#method-i-blank-3F). ### How do you use it? require 'fast_blank' or add it to your Bundler Gemfile gem 'fast_blank' ### How fast is "Fast"? About 1.2–20x faster than Active Support on my machine (your mileage my vary, depends on string length): ``` $ bundle exec ./benchmark ================== Test String Length: 0 ================== Calculating ------------------------------------- Fast Blank 225.251k i/100ms Fast ActiveSupport 225.676k i/100ms Slow Blank 110.934k i/100ms New Slow Blank 221.792k i/100ms ------------------------------------------------- Fast Blank 29.673M (± 2.7%) i/s - 148.215M Fast ActiveSupport 28.249M (± 3.5%) i/s - 141.048M Slow Blank 2.158M (± 3.3%) i/s - 10.872M New Slow Blank 23.558M (± 3.2%) i/s - 117.772M Comparison: Fast Blank: 29673200.1 i/s Fast ActiveSupport: 28248894.5 i/s - 1.05x slower New Slow Blank: 23557900.0 i/s - 1.26x slower Slow Blank: 2157787.7 i/s - 13.75x slower ================== Test String Length: 6 ================== Calculating ------------------------------------- Fast Blank 201.185k i/100ms Fast ActiveSupport 205.076k i/100ms Slow Blank 102.061k i/100ms New Slow Blank 123.087k i/100ms ------------------------------------------------- Fast Blank 13.894M (± 2.3%) i/s - 69.409M Fast ActiveSupport 14.627M (± 3.5%) i/s - 73.212M Slow Blank 1.943M (± 2.3%) i/s - 9.798M New Slow Blank 2.796M (± 1.8%) i/s - 14.032M Comparison: Fast ActiveSupport: 14627063.7 i/s Fast Blank: 13893631.2 i/s - 1.05x slower New Slow Blank: 2795783.3 i/s - 5.23x slower Slow Blank: 1943025.9 i/s - 7.53x slower ================== Test String Length: 14 ================== Calculating ------------------------------------- Fast Blank 220.004k i/100ms Fast ActiveSupport 219.716k i/100ms Slow Blank 147.399k i/100ms New Slow Blank 106.651k i/100ms ------------------------------------------------- Fast Blank 24.949M (± 3.0%) i/s - 124.742M Fast ActiveSupport 24.491M (± 3.3%) i/s - 122.382M Slow Blank 4.292M (± 1.6%) i/s - 21.520M New Slow Blank 2.115M (± 2.4%) i/s - 10.665M Comparison: Fast Blank: 24948558.8 i/s Fast ActiveSupport: 24491245.1 i/s - 1.02x slower Slow Blank: 4292490.5 i/s - 5.81x slower New Slow Blank: 2115097.6 i/s - 11.80x slower ================== Test String Length: 24 ================== Calculating ------------------------------------- Fast Blank 206.555k i/100ms Fast ActiveSupport 208.513k i/100ms Slow Blank 137.733k i/100ms New Slow Blank 101.215k i/100ms ------------------------------------------------- Fast Blank 16.761M (± 2.7%) i/s - 83.861M Fast ActiveSupport 17.710M (± 3.2%) i/s - 88.618M Slow Blank 3.744M (± 2.0%) i/s - 18.732M New Slow Blank 1.962M (± 2.7%) i/s - 9.818M Comparison: Fast ActiveSupport: 17709936.5 i/s Fast Blank: 16760839.7 i/s - 1.06x slower Slow Blank: 3744048.4 i/s - 4.73x slower New Slow Blank: 1961831.1 i/s - 9.03x slower ================== Test String Length: 136 ================== Calculating ------------------------------------- Fast Blank 201.772k i/100ms Fast ActiveSupport 189.120k i/100ms Slow Blank 129.439k i/100ms New Slow Blank 90.677k i/100ms ------------------------------------------------- Fast Blank 16.718M (± 2.8%) i/s - 83.534M Fast ActiveSupport 17.617M (± 3.6%) i/s - 87.941M Slow Blank 3.725M (± 3.0%) i/s - 18.639M New Slow Blank 1.940M (± 4.8%) i/s - 9.702M Comparison: Fast ActiveSupport: 17616782.1 i/s Fast Blank: 16718307.8 i/s - 1.05x slower Slow Blank: 3725097.6 i/s - 4.73x slower New Slow Blank: 1940271.2 i/s - 9.08x slower ``` Additionally, this gem allocates no strings during the test, making it less of a GC burden. ### Compatibility note: `fast_blank` supports MRI Ruby 1.9.3, 2.0, 2.1, and 2.2, as well as Rubinius 2.x. Earlier versions of MRI are untested. `fast_blank` implements `String#blank?` as MRI would have implemented it, meaning it has 100% parity with `String#strip.length == 0`. Active Support's version also considers Unicode spaces. For example, `"\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000".blank?` is true in Active Support even though `fast_blank` would treat it as *not* blank. Therefore, `fast_blank` also provides `blank_as?` which is a 100%-compatible Active Support `blank?` replacement. ### Credits * Author: Sam Saffron (sam.saffron@gmail.com) * https://github.com/SamSaffron/fast_blank * License: MIT * Gem template based on [CodeMonkeySteve/fast_xor](https://github.com/CodeMonkeySteve/fast_xor) ### Change log: 1.0.1: - Minor, avoid warnings if redefining blank? 1.0.0: - Adds Ruby 2.2 support ([@tjschuck](https://github.com/tjschuck) — [#9](https://github.com/SamSaffron/fast_blank/pull/9)) 0.0.2: - Removed rake dependency ([@tmm1](https://github.com/tmm1) — [#2](https://github.com/SamSaffron/fast_blank/pull/2)) - Unrolled internal loop to improve perf ([@tmm1](https://github.com/tmm1) — [#2](https://github.com/SamSaffron/fast_blank/pull/2)) fast_blank-1.0.1/spec/0000755000004100000410000000000014115406277014606 5ustar www-datawww-datafast_blank-1.0.1/spec/fast_blank_spec.rb0000644000004100000410000000237414115406277020257 0ustar www-datawww-data$VERBOSE = true class ::String # Stub the original method to make sure it is redefined correctly. def blank? raise NotImplementedError end def blank2? /\A[[:space:]]*\z/ === self end end require 'fast_blank' describe String do it "works" do expect("".blank?).to eq(true) expect(" ".blank?).to eq(true) expect("\r\n".blank?).to eq(true) "\r\n\v\f\r\s\u0085".blank? == true end it "provides a parity with active support function" do (16*16*16*16).times do |i| c = i.chr('UTF-8') rescue nil unless c.nil? expect("#{i.to_s(16)} #{c.blank_as?}").to eq("#{i.to_s(16)} #{c.blank2?}") end end (256).times do |i| c = i.chr('ASCII') rescue nil unless c.nil? expect("#{i.to_s(16)} #{c.blank_as?}").to eq("#{i.to_s(16)} #{c.blank2?}") end end end it "has parity with strip.length" do (256).times do |i| c = i.chr('ASCII') rescue nil unless c.nil? expect("#{i.to_s(16)} #{c.strip.length == 0}").to eq("#{i.to_s(16)} #{c.blank?}") end end end it "treats \u0000 correctly" do # odd I know expect("\u0000".strip.length).to eq(0) expect("\u0000".blank_as?).to be_falsey expect("\u0000".blank?).to be_truthy end end fast_blank-1.0.1/benchmark0000755000004100000410000001241214115406277015534 0ustar www-datawww-data#!/usr/bin/env ruby $: << File.dirname(__FILE__)+'/lib' require 'benchmark/ips' require 'fast_blank' class String # active support implementation def slow_blank? /\A[[:space:]]*\z/ === self end def new_slow_blank? empty? || !(/[[:^space:]]/ === self) end end test_strings = [ "", "\r\n\r\n ", "this is a test", " this is a longer test", " this is a longer test this is a longer test this is a longer test this is a longer test this is a longer test" ] test_strings.each do |s| raise "failed on #{s.inspect}" if s.blank? != s.slow_blank? raise "failed on #{s.inspect}" if s.blank? != s.new_slow_blank? end test_strings.each do |s| puts "\n================== Test String Length: #{s.length} ==================" Benchmark.ips do |x| x.report("Fast Blank") do |times| i = 0 while i < times s.blank? i += 1 end end x.report("Fast ActiveSupport") do |times| i = 0 while i < times s.blank_as? i += 1 end end x.report("Slow Blank") do |times| i = 0 while i < times s.slow_blank? i += 1 end end x.report("New Slow Blank") do |times| i = 0 while i < times s.new_slow_blank? i += 1 end end x.compare! end end # ================== Test String Length: 0 ================== # Calculating ------------------------------------- # Fast Blank 225.251k i/100ms # Fast ActiveSupport 225.676k i/100ms # Slow Blank 110.934k i/100ms # New Slow Blank 221.792k i/100ms # ------------------------------------------------- # Fast Blank 29.673M (± 2.7%) i/s - 148.215M # Fast ActiveSupport 28.249M (± 3.5%) i/s - 141.048M # Slow Blank 2.158M (± 3.3%) i/s - 10.872M # New Slow Blank 23.558M (± 3.2%) i/s - 117.772M # # Comparison: # Fast Blank: 29673200.1 i/s # Fast ActiveSupport: 28248894.5 i/s - 1.05x slower # New Slow Blank: 23557900.0 i/s - 1.26x slower # Slow Blank: 2157787.7 i/s - 13.75x slower # # # ================== Test String Length: 6 ================== # Calculating ------------------------------------- # Fast Blank 201.185k i/100ms # Fast ActiveSupport 205.076k i/100ms # Slow Blank 102.061k i/100ms # New Slow Blank 123.087k i/100ms # ------------------------------------------------- # Fast Blank 13.894M (± 2.3%) i/s - 69.409M # Fast ActiveSupport 14.627M (± 3.5%) i/s - 73.212M # Slow Blank 1.943M (± 2.3%) i/s - 9.798M # New Slow Blank 2.796M (± 1.8%) i/s - 14.032M # # Comparison: # Fast ActiveSupport: 14627063.7 i/s # Fast Blank: 13893631.2 i/s - 1.05x slower # New Slow Blank: 2795783.3 i/s - 5.23x slower # Slow Blank: 1943025.9 i/s - 7.53x slower # # # ================== Test String Length: 14 ================== # Calculating ------------------------------------- # Fast Blank 220.004k i/100ms # Fast ActiveSupport 219.716k i/100ms # Slow Blank 147.399k i/100ms # New Slow Blank 106.651k i/100ms # ------------------------------------------------- # Fast Blank 24.949M (± 3.0%) i/s - 124.742M # Fast ActiveSupport 24.491M (± 3.3%) i/s - 122.382M # Slow Blank 4.292M (± 1.6%) i/s - 21.520M # New Slow Blank 2.115M (± 2.4%) i/s - 10.665M # # Comparison: # Fast Blank: 24948558.8 i/s # Fast ActiveSupport: 24491245.1 i/s - 1.02x slower # Slow Blank: 4292490.5 i/s - 5.81x slower # New Slow Blank: 2115097.6 i/s - 11.80x slower # # # ================== Test String Length: 24 ================== # Calculating ------------------------------------- # Fast Blank 206.555k i/100ms # Fast ActiveSupport 208.513k i/100ms # Slow Blank 137.733k i/100ms # New Slow Blank 101.215k i/100ms # ------------------------------------------------- # Fast Blank 16.761M (± 2.7%) i/s - 83.861M # Fast ActiveSupport 17.710M (± 3.2%) i/s - 88.618M # Slow Blank 3.744M (± 2.0%) i/s - 18.732M # New Slow Blank 1.962M (± 2.7%) i/s - 9.818M # # Comparison: # Fast ActiveSupport: 17709936.5 i/s # Fast Blank: 16760839.7 i/s - 1.06x slower # Slow Blank: 3744048.4 i/s - 4.73x slower # New Slow Blank: 1961831.1 i/s - 9.03x slower # # # ================== Test String Length: 136 ================== # Calculating ------------------------------------- # Fast Blank 201.772k i/100ms # Fast ActiveSupport 189.120k i/100ms # Slow Blank 129.439k i/100ms # New Slow Blank 90.677k i/100ms # ------------------------------------------------- # Fast Blank 16.718M (± 2.8%) i/s - 83.534M # Fast ActiveSupport 17.617M (± 3.6%) i/s - 87.941M # Slow Blank 3.725M (± 3.0%) i/s - 18.639M # New Slow Blank 1.940M (± 4.8%) i/s - 9.702M # # Comparison: # Fast ActiveSupport: 17616782.1 i/s # Fast Blank: 16718307.8 i/s - 1.05x slower # Slow Blank: 3725097.6 i/s - 4.73x slower # New Slow Blank: 1940271.2 i/s - 9.08x slower # fast_blank-1.0.1/fast_blank.gemspec0000644000004100000410000000360714115406277017333 0ustar www-datawww-data######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: fast_blank 1.0.1 ruby lib # stub: ext/fast_blank/extconf.rb Gem::Specification.new do |s| s.name = "fast_blank".freeze s.version = "1.0.1" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["Sam Saffron".freeze] s.date = "2021-08-17" s.description = "Provides a C-optimized method for determining if a string is blank".freeze s.email = "sam.saffron@gmail.com".freeze s.extensions = ["ext/fast_blank/extconf.rb".freeze] s.files = ["MIT-LICENSE".freeze, "README.md".freeze, "benchmark".freeze, "ext/fast_blank/extconf.rb".freeze, "ext/fast_blank/fast_blank.c".freeze, "lib/.gemkeep".freeze, "spec/fast_blank_spec.rb".freeze] s.homepage = "https://github.com/SamSaffron/fast_blank".freeze s.licenses = ["MIT".freeze] s.rubygems_version = "2.7.6.2".freeze s.summary = "Fast String blank? implementation".freeze s.test_files = ["spec/fast_blank_spec.rb".freeze] if s.respond_to? :specification_version then s.specification_version = 4 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, [">= 0"]) else s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) end else s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) end end fast_blank-1.0.1/lib/0000755000004100000410000000000014115406277014422 5ustar www-datawww-datafast_blank-1.0.1/lib/.gemkeep0000644000004100000410000000000014115406277016026 0ustar www-datawww-datafast_blank-1.0.1/MIT-LICENSE0000644000004100000410000000204514115406277015311 0ustar www-datawww-dataCopyright (c) 2006-2009 Steve Sloan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. fast_blank-1.0.1/ext/0000755000004100000410000000000014115406277014454 5ustar www-datawww-datafast_blank-1.0.1/ext/fast_blank/0000755000004100000410000000000014115406277016560 5ustar www-datawww-datafast_blank-1.0.1/ext/fast_blank/extconf.rb0000644000004100000410000000005414115406277020552 0ustar www-datawww-datarequire 'mkmf' create_makefile 'fast_blank' fast_blank-1.0.1/ext/fast_blank/fast_blank.c0000644000004100000410000000340014115406277021025 0ustar www-datawww-data#include #include #include #include #include #define STR_ENC_GET(str) rb_enc_from_index(ENCODING_GET(str)) #ifndef RUBY_API_VERSION_CODE # define ruby_version_before_2_2() 1 #else # define ruby_version_before_2_2() (RUBY_API_VERSION_CODE < 20200) #endif static VALUE rb_str_blank_as(VALUE str) { rb_encoding *enc; char *s, *e; enc = STR_ENC_GET(str); s = RSTRING_PTR(str); if (!s || RSTRING_LEN(str) == 0) return Qtrue; e = RSTRING_END(str); while (s < e) { int n; unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc); switch (cc) { case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0x20: case 0x85: case 0xa0: case 0x1680: case 0x2000: case 0x2001: case 0x2002: case 0x2003: case 0x2004: case 0x2005: case 0x2006: case 0x2007: case 0x2008: case 0x2009: case 0x200a: case 0x2028: case 0x2029: case 0x202f: case 0x205f: case 0x3000: #if ruby_version_before_2_2() case 0x180e: #endif /* found */ break; default: return Qfalse; } s += n; } return Qtrue; } static VALUE rb_str_blank(VALUE str) { rb_encoding *enc; char *s, *e; enc = STR_ENC_GET(str); s = RSTRING_PTR(str); if (!s || RSTRING_LEN(str) == 0) return Qtrue; e = RSTRING_END(str); while (s < e) { int n; unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc); if (!rb_isspace(cc) && cc != 0) return Qfalse; s += n; } return Qtrue; } void Init_fast_blank( void ) { rb_define_method(rb_cString, "blank?", rb_str_blank, 0); rb_define_method(rb_cString, "blank_as?", rb_str_blank_as, 0); }