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.

NOTE To use StringPool, you must explicitly import it with require "string_pool"

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

Included Modules

Direct Known Subclasses

Defined in:

string_store/string_pool_patch.cr

Instance Method Summary

Instance Method Detail

def [](hash : UInt64) : String #

[View source]
def [](str : String) : UInt64 #

[View source]
def []?(str : String) : UInt64 | Nil #

[View source]
def []?(hash : UInt64) : String | Nil #

[View source]
def each(&) #
Description copied from module Enumerable(String)

Must yield this collection's elements to the block.


[View source]
def get_id(str : Pointer(UInt8), len) : UInt64 #

[View source]
def get_id(str : String) #

[View source]
def get_id(str : IO::Memory) #

[View source]
def hash(str : String) : UInt64 #

[View source]
def hash(bytes : Bytes) : UInt64 #

[View source]