class StringPool
Overview
A string pool is a collection of strings. It allows a runtime to save memory by preserving strings in a pool, allowing to reuse an instance of a common string instead of creating a new one.
require "string_pool"
pool = StringPool.new
a = "foo" + "bar"
b = "foo" + "bar"
a.object_id # => 136294360
b.object_id # => 136294336
a = pool.get(a)
b = pool.get(b)
a.object_id # => 136294312
b.object_id # => 136294312
Defined in:
string_pool.crClass Method Summary
-
.new
Creates a new empty string pool.
Instance Method Summary
-
#empty?
Returns
true
if the String Pool has no element otherwise returnsfalse
. -
#get(str : MemoryIO)
Returns a string with the contents of the given
MemoryIO
. -
#get(str : String)
Returns a string with the contents of the given string.
-
#get(slice : Slice(UInt8))
Returns a string with the contents of the given slice.
-
#get(str : Pointer(UInt8), len)
Returns a string with the contents given by the pointer str of size len.
-
#rehash
Rebuilds the hash based on the current hash values for each key, if values of key objects have changed since they were inserted.
-
#size : Int32
Returns the size
Instance methods inherited from class Reference
==(other)==(other : self) ==, dup dup, hash hash, inspect(io : IO) : Nil inspect, object_id : UInt64 object_id, same?(other : Reference)
same?(other : Nil) same?, to_s(io : IO) : Nil to_s
Instance methods inherited from class Object
!=(other)
!=,
!~(other)
!~,
==(other)
==,
===(other)===(other : YAML::Any)
===(other : JSON::Any) ===, =~(other) =~, class class, crystal_type_id crystal_type_id, dup dup, hash hash, inspect(io : IO)
inspect inspect, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ") to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml
to_yaml(io : IO) to_yaml, try(&block) try
Class methods inherited from class Object
==(other : Class)
==,
===(other)
===,
cast(other) : self
cast,
clone
clone,
dup
dup,
from_json(string_or_io) : selffrom_json(string_or_io, root : String) : self from_json, from_yaml(string : String) : self from_yaml, hash hash, inspect(io) inspect, name : String name, nilable? nilable?, to_s(io) to_s, |(other : U.class) forall U |
Class Method Detail
Instance Method Detail
Returns true
if the String Pool has no element otherwise returns false
.
pool = StringPool.new
pool.empty? # => true
pool.get("crystal")
pool.empty? # => false
Returns a string with the contents of the given MemoryIO
.
If a string with those contents was already present in the pool, that one is returned. Otherwise a new string is created, put in the pool and returned
pool = StringPool.new
io = MemoryIO.new "crystal"
pool.empty? # => true
pool.get(io)
pool.empty? # => false
Returns a string with the contents of the given string.
If a string with those contents was already present in the pool, that one is returned. Otherwise a new string is created, put in the pool and returned
pool = StringPool.new
string = "crystal"
pool.empty? # => true
pool.get(string)
pool.empty? # => false
Returns a string with the contents of the given slice.
If a string with those contents was already present in the pool, that one is returned. Otherwise a new string is created, put in the pool and returned.
pool = StringPool.new
ptr = Pointer.malloc(9) { |i| ('a'.ord + i).to_u8 }
slice = Slice.new(ptr, 3)
pool.empty? # => true
pool.get(slice)
pool.empty? # => false
```
Returns a string with the contents given by the pointer str of size len.
If a string with those contents was already present in the pool, that one is returned. Otherwise a new string is created, put in the pool and returned.
pool = StringPool.new
pool.get("hey".to_unsafe, 3)
pool.size # => 1
Rebuilds the hash based on the current hash values for each key, if values of key objects have changed since they were inserted.
Call this method if you modified a string submitted to the pool.