cheap_strings 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +93 -0
  3. data/lib/cheap_strings.rb +5 -0
  4. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cda4eeb76ca4ad68d5a3e45ebdae9911a2a6c053
4
+ data.tar.gz: 7f0c49de814d60426f3b516bd030e94c7d98bc4b
5
+ SHA512:
6
+ metadata.gz: 41878e70b54d735bacc09127dba91c30817b0fa514a053b86f57566e93d0756102db26281c7db91b3a7ac7d9ae18d9c598b521547318f6380dc929d0f9e72d36
7
+ data.tar.gz: 6f7a819773e70411be259a96a9d3ded5c8b12b786af42d1bdc1f5a24badf82f24a05e8536aa58793d9dea1ed5a876ab760445ac3920bbd8098a768a711b32a77
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # Cheap Strings
2
+
3
+ Cheap Strings is a simple gem that makes it convenient to use frozen strings in your code.
4
+
5
+ Since Ruby strings are mutable, every string literal must allocate a new string whenever it is evaluated. Often this is a waste of time, as most strings in Ruby programs are never mutated.
6
+
7
+ Cheap Strings freezes your string literals and then reuses the same object every time - making your program faster and reducing GC pressure by not creating huge amounts of objects.
8
+
9
+ ## Benchmark
10
+
11
+ The benchmarks below use this common setup code:
12
+
13
+ ```ruby
14
+ require "cheap_strings"
15
+
16
+ module A
17
+ extend CheapStrings
18
+
19
+ def self.create_lots_of_strings
20
+ 1_000_000.times do
21
+ "hello world"
22
+ end
23
+ end
24
+
25
+ def self.create_lots_of_cheap_strings
26
+ 1_000_000.times do
27
+ `hello world`
28
+ end
29
+ end
30
+ end
31
+ ```
32
+
33
+ ### Time benchmark
34
+
35
+ ```ruby
36
+ require "benchmark"
37
+
38
+ Benchmark.bmbm do |b|
39
+ b.report "regular strings" do
40
+ A.create_lots_of_strings
41
+ end
42
+ b.report "cheap strings" do
43
+ A.create_lots_of_cheap_strings
44
+ end
45
+ end
46
+ ```
47
+
48
+ **Results:**
49
+
50
+ ```
51
+ Rehearsal ---------------------------------------------------
52
+ regular strings 0.240000 0.000000 0.240000 ( 0.242331)
53
+ cheap strings 0.140000 0.000000 0.140000 ( 0.135519)
54
+ ------------------------------------------ total: 0.380000sec
55
+
56
+ user system total real
57
+ regular strings 0.240000 0.000000 0.240000 ( 0.235980)
58
+ cheap strings 0.140000 0.000000 0.140000 ( 0.139030)
59
+ ```
60
+
61
+ ### Object allocation benchmark
62
+
63
+ ```ruby
64
+ def object_bench(name)
65
+ before, after = {}, {}
66
+
67
+ GC.start
68
+ GC.disable
69
+ ObjectSpace.count_objects(before)
70
+
71
+ yield
72
+
73
+ ObjectSpace.count_objects(after)
74
+ GC.enable
75
+
76
+ printf "%20s %6d -> %6d (+%8d)\n", name, before[:T_STRING], after[:T_STRING], after[:T_STRING] - before[:T_STRING]
77
+ end
78
+
79
+ object_bench "regular strings" do
80
+ A.create_lots_of_strings
81
+ end
82
+
83
+ object_bench "cheap strings" do
84
+ A.create_lots_of_cheap_strings
85
+ end
86
+ ```
87
+
88
+ **Results:**
89
+
90
+ ```
91
+ regular strings 5629 -> 1005655 (+ 1000026)
92
+ cheap strings 5657 -> 5657 (+ 0)
93
+ ```
@@ -0,0 +1,5 @@
1
+ module CheapStrings
2
+ def `(str)
3
+ str
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cheap_strings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Charlie Somerville
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Cheap string literals for Ruby
14
+ email: charlie@charliesomerville.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/cheap_strings.rb
20
+ - README.md
21
+ homepage:
22
+ licenses: []
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Cheap string literals for Ruby
44
+ test_files: []