• Stars
    star
    610
  • Rank 70,693 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 11 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

String#blank? Ruby Extension

Gem Version Build Status

fast_blank is a simple C extension which provides a fast implementation of Active Support's String#blank? method.

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

Change log:

1.0.1:

  • Minor, avoid warnings if redefining blank?

1.0.0:

0.0.2:

  • Removed rake dependency (@tmm1 — #2)
  • Unrolled internal loop to improve perf (@tmm1 — #2)

More Repositories

1

memory_profiler

memory_profiler for ruby
Ruby
1,651
star
2

MiniProfiler

A simple but effective mini-profiler for ASP.NET MVC, ASP.NET and Ruby.
1,051
star
3

flamegraph

Flamegraph profiling support for Ruby 2.0
HTML
382
star
4

lru_redux

An efficient optionally thread safe LRU Cache
Ruby
282
star
5

graphite_docker

docker container for graphite
Python
165
star
6

redismux

A simple redis multiplexer
Go
55
star
7

allocator_bench

bench of various memory allocators
Ruby
51
star
8

So-Slow

Stack Overflow creative commons database importer
C#
41
star
9

blog

samsaffron.com blog plugin for Discourse
Ruby
37
star
10

cpu-analyzer

C#
36
star
11

dotfiles

various dotfiles I use
Vim Script
31
star
12

fast_stack

Ruby
19
star
13

react-native-key-pair

Objective-C
19
star
14

performance

my personal playground for various perf stuff
Ruby
12
star
15

tvdb-scraper

Scrapes TV metadata from thetvdb
Ruby
9
star
16

media-library

.Net library for management of media
C#
7
star
17

youtube-home

a toy I use to disable youtube requires ubiquity router
Ruby
7
star
18

DataExplorerPG

DataExplorerPG
C#
6
star
19

discourse-ansible

ansible script for setting up discourse from scratch on ubuntu
Ruby
5
star
20

autotest-shoes-gui

A simple GUI for autotest written in shoes
Ruby
5
star
21

simplestorageengine

A simple storage engine for .Net apps supporting active record
C#
4
star
22

ruby-bench-play

Ruby
4
star
23

Media-Browser

Media Browser Git Fork
C#
4
star
24

logviewer

A simple GUI logviewer for rails and ruby
Ruby
2
star
25

discourse_public_import

A tool to import all public posts on discourse
Ruby
2
star
26

super-ssh

A tiny script that help keep ssh tunnels up, similar to autossh
2
star
27

stacktrace

An extension for Ruby that provides with class and method information in the backtrace
Ruby
1
star
28

discourse-solarized

discourse solarized color scheme
1
star
29

brotli-test

A container to test brotli dynamic compression
HTML
1
star
30

mysqldump-x-restore

Automatically exported from code.google.com/p/mysqldump-x-restore
XSLT
1
star
31

discourse-roboto-theme

roboto theme demo
CSS
1
star
32

discourse-nginx-performance-report

Ruby
1
star
33

discourse-dev-benchmark

Ruby
1
star
34

mini_locale

Ruby
1
star
35

bench_web

quick benchmarking tool for web sites
Ruby
1
star