# \[Open\] \[PATCH\]Symbol GC

**URL:** <https://www.ruby-forum.com/t/open-patch-symbol-gc/239755>\
**Category:** Development\
**Created:** [March 13, 2014, 8:59am UTC](https://www.ruby-forum.com/t/open-patch-symbol-gc/239755 "2014-03-13T08:59:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [March 13, 2014, 8:59am UTC](https://www.ruby-forum.com/t/open-patch-symbol-gc/239755/1 "2014-03-13T08:59:22Z")

</div>

Issue #9633 has been reported by Narihiro N…

* * *

Feature #9633: [PATCH]Symbol GC

> **[Feature #9633: \[PATCH\]Symbol GC - Ruby master - Ruby Issue Tracking System](https://bugs.ruby-lang.org/issues/9633)**
>
> Redmine

- Author: Narihiro N.
- Status: Open
- Priority: Normal
- Assignee: Yukihiro M.
- Category: core
- Target version: current: 2.2.0

* * *

I’ve written a patch to collect most symbols.

PATCH:  
[https://github.com/authorNari/ruby/compare/13834fb3c...symbol\_gc.patch](https://github.com/authorNari/ruby/compare/13834fb3c...symbol_gc.patch)

## Summary

- Most symbols in Ruby level are GC-able（generated by #to\_sym, #intern,  
etc…）
- Exclude a symbol which is translated ID in C-level from GC-able  
symbols
- Keep Ruby’s C extension compatibility
- Pass `make test-all`

## Benchmark

A benchmark program is here.

```auto
obj = Object.new
100_000.times do |i|
  obj.respond_to?("sym#{i}".to_sym)
end
GC.start
puts"symbol : #{Symbol.all_symbols.size}"

```

```auto
% time RBENV_VERSION=ruby-r45059 ruby -v /tmp/a.rb
ruby 2.2.0dev (2014-02-20 trunk 45059) [x86_64-linux]
symbol : 102416
0.24s user 0.01s system 91% cpu 0.272 total

% time RBENV_VERSION=symgc ruby -v /tmp/a.rb
ruby 2.2.0dev (2014-02-20 trunk 45059) [x86_64-linux]
symbol : 2833
0.21s user 0.01s system 90% cpu 0.247 total

```

The total number of symbols is declined.  
The total time of symgc version is improved because Full GC pressure has  
been reduced.

The result of `make benchmark`.

> <https://gist.github.com/authorNari/9359704>

There is no significant slowdown.

(I would welcome to try an additional benchmark and report)

## Implementation Detail

I classify Dynamic symbol and Static symbol.

- Static symbol

- Dynamic symbol

Please read the patch if you want to know more information.

## Acknowledgment

The idea of this symbol GC is invented by Sasada Koichi in Heroku,inc.  
Thank you.

– ja –  
RubyレベルのシンボルをGC対象にするパッチを書きました。

> **[Comparing 13834fb3c...symbol\_gc · authorNari/ruby](https://github.com/authorNari/ruby/compare/13834fb3c...symbol_gc)**
>
> The Ruby Programming Language. Contribute to authorNari/ruby development by creating an account on GitHub.

## 概要

- RubyレベルのほとんどのシンボルがGC対象（to\_sym,internで作られたもの）
- C側でIDに変換された場合はGC対象から除外（rb\_intern、SYM2IDなど）
- C-APIの互換性維持
- make test-allが通る

## ベンチマーク

以下のプログラムを実行。

```auto
obj = Object.new
100_000.times do |i|
  obj.respond_to?("sym#{i}".to_sym)
end
GC.start
puts"symbol : #{Symbol.all_symbols.size}"

```

```auto
% time RBENV_VERSION=symgc ruby -v /tmp/a.rb
ruby 2.2.0dev (2014-02-20 trunk 45059) [x86_64-linux]
symbol : 2833
0.21s user 0.01s system 90% cpu 0.247 total

% time RBENV_VERSION=ruby-r45059 ruby -v /tmp/a.rb
ruby 2.2.0dev (2014-02-20 trunk 45059) [x86_64-linux]
symbol : 102416
0.24s user 0.01s system 91% cpu 0.272 total

```

総シンボル数が減少していることがわかる。  
シンボル数の現象でFull GCのプレッシャーが削減されたことにより、symgcの速度が向上した。

make benchmarkの結果。

> <https://gist.github.com/authorNari/9359704>

大幅な速度低下は見られない。

（上記以外の追試を歓迎します）

## （ちょっとした）詳細

symbolをstatic symbolとdynamic symbolに分類。

- static symbol

- dynamic symbol

その他の詳細はパッチを読んでもらえると…。

## 謝辞

シンボルGCのアイデアはHeroku社のささだこういち様によるものです。  
ありがとうございます。
