W3cubDocs

/Crystal

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.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(initial_capacity = 8)Source

Creates a new empty string pool.

The initial_capacity is useful to avoid unnecessary reallocations of the internal buffers in case of growth. If you have an estimate of the maximum number of elements the pool will hold it should be initialized with that capacity for improved performance.

pool = StringPool.new(256)
pool.size # => 0

Instance Method Detail

def empty?Source

Returns true if the StringPool has no element otherwise returns false.

require "string_pool"

pool = StringPool.new
pool.empty? # => true
pool.get("crystal")
pool.empty? # => false

def get(str : Pointer(UInt8), len)Source

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.

require "string_pool"

pool = StringPool.new
pool.get("hey".to_unsafe, 3)
pool.size # => 1

def get(slice : Bytes)Source

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.

require "string_pool"

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

def get(str : IO::Memory)Source

Returns a String with the contents of the given IO::Memory.

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.

require "string_pool"

pool = StringPool.new
io = IO::Memory.new "crystal"
pool.empty? # => true
pool.get(io)
pool.empty? # => false

def get(str : String)Source

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.

require "string_pool"

pool = StringPool.new
string = "crystal"
pool.empty? # => true
pool.get(string)
pool.empty? # => false

def rehashSource

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.

def size : Int32Source

Returns the size

require "string_pool"

pool = StringPool.new
pool.size # => 0

© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/StringPool.html