Skip to content
This repository was archived by the owner on Mar 15, 2022. It is now read-only.

Commit aa65753

Browse files
committed
Added a Gemfile and updated TravisCI build targets.
1 parent e1462f4 commit aa65753

File tree

6 files changed

+94
-46
lines changed

6 files changed

+94
-46
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ tmp
33
rdoc
44
*.rbc
55
coverage
6-
.ruby-version
6+
.ruby-version
7+
.ruby-gemset
8+
Gemfile.lock

.travis.yml

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
1+
language: ruby
2+
13
rvm:
2-
- 1.8.7
3-
- 1.9.2
4-
- rbx
5-
- jruby
4+
- 2.2.0
5+
- 2.1.5
6+
- 2.1.4
7+
- 2.0.0
8+
- 1.9.3
9+
- ruby-head
10+
- jruby-1.7.18
11+
- jruby-head
12+
- rbx-2
13+
14+
jdk:
15+
- oraclejdk8
16+
17+
sudo: false
18+
19+
branches:
20+
only:
21+
- master
22+
23+
matrix:
24+
allow_failures:
25+
- rvm: ruby-head
26+
- rvm: jruby-head
27+
- rvm: 1.9.3

Gemfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
source 'https://rubygems.org'
2+
3+
group :development do
4+
gem 'rake', '~> 10.3.2'
5+
end
6+
7+
group :testing do
8+
gem 'test-unit', '~> 3.0.9'
9+
#gem 'rspec', '~> 3.0.0'
10+
#gem 'simplecov', '~> 0.8.2', :require => false
11+
#gem 'coveralls', '~> 0.7.0', :require => false
12+
end
13+
14+
group :documentation do
15+
gem 'yard', '~> 0.8.7.4', :require => false
16+
gem 'inch', '~> 0.4.6', :platforms => :mri, :require => false
17+
gem 'redcarpet', '~> 3.1.2', platforms: :mri # understands github markdown
18+
end

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Ref
2+
3+
[![Gem Version](https://badge.fury.io/rb/ref.svg)](http://badge.fury.io/rb/ref) [![Build Status](https://travis-ci.org/ruby-concurrency/ref.svg?branch=master)](https://travis-ci.org/ruby-concurrency/ref) [![Code Climate](https://codeclimate.com/github/ruby-concurrency/ref.svg)](https://codeclimate.com/github/ruby-concurrency/ref) [![Dependency Status](https://gemnasium.com/ruby-concurrency/ref.svg)](https://gemnasium.com/ruby-concurrency/ref) [![License](https://img.shields.io/badge/license-MIT-green.svg)](http://opensource.org/licenses/MIT) [![Gitter chat](http://img.shields.io/badge/gitter-join%20chat%20%E2%86%92-brightgreen.svg)](https://gitter.im/ruby-concurrency/concurrent-ruby)
4+
5+
This library provides object references for Ruby as well as some common utilities for working with references. Object references are used to point to other objects and come in three distinct flavors that interact differently with the garbage collector.
6+
7+
* `Ref::StrongReference` - This is a plain old pointer to another object.
8+
* `Ref::WeakReference` - This is a pointer to another object, but it is not seen by the garbage collector and the memory used by the object can be reclaimed at any time.
9+
* `Ref::SoftReference` - This is similar to a weak reference, but the garbage collector is not as eager to reclaim the referenced object.
10+
11+
All of these classes extend from a common `Ref::Reference` class and have a common interface.
12+
13+
Weak and soft references are useful when you have instantiated objects that you may want to use again but can recreate if necessary. Since the garbage collector determines when to reclaim the memory used by the objects, you don't need to worry about bloating the Ruby heap.
14+
15+
## Example Usage
16+
17+
```ruby
18+
ref = Ref::WeakReference.new("hello")
19+
ref.object # should be "hello"
20+
ObjectSpace.garbage_collect
21+
ref.object # should be nil (assuming the garbage collector reclaimed the reference)
22+
```
23+
24+
## Goodies
25+
26+
This library also includes tools for some common uses of weak and soft references.
27+
28+
* `Ref::WeakKeyMap` - A map of keys to values where the keys are weak references
29+
* `Ref::WeakValueMap` - A map of keys to values where the values are weak references
30+
* `Ref::SoftKeyMap` - A map of keys to values where the keys are soft references
31+
* `Ref::SoftValueMap` - A map of keys to values where the values are soft references
32+
* `Ref::ReferenceQueue` - A thread safe implementation of a queue that will add references to itself as their objects are garbage collected.
33+
34+
## Problems with WeakRef
35+
36+
Ruby does come with the `WeakRef` class in the standard library. However, there are [issues with this class](https://bugs.ruby-lang.org/issues/4168) across several different Ruby runtimes. This gem provides a common interface to weak references that works across MRI, Ruby Enterprise Edition, YARV, Jruby, Rubinius, and IronRuby.
37+
38+
1. MRI and REE 1.8 - `WeakRef` extends from Delegator which is a very heavy weight class under Ruby 1.8. Creating a `WeakRef` object will allocate thousands of other objects and use up hundreds of kilobytes of memory. This makes `WeakRef` all but unusable even if you only need several hundred of them.
39+
2. YARV 1.9 - `WeakRef` is unsafe to use because the garbage collector can run in a different system thread than a thread allocating memory. This exposes a bug where a `WeakRef` may end up pointing to a completely different object than it originally referenced.
40+
3. Jruby and IronRuby - Jruby and IronRuby using the Ruby 1.8 libraries suffers from the same performance issue with the Delegator class. Furthermore, these VM's don't implement the method used to load an object from the heap using an object id and so cannot use a pure Ruby method to implement weak references.
41+
4. Rubinius - Rubinius implements `WeakRef` with a lighter weight version of delegation and works very well.
42+
5. MRI Ruby 2.0 has a good implementation of `WeakRef`.
43+
44+
## BasicObject
45+
46+
Note that weak references will not work with MRI 1.9 or earlier. References will be created, but the objects will never be stored so the reference object will always treat the object as if it is always garbage collected. BasicObject does not implement the necessary methods to maintain the reference.

README.rdoc

Lines changed: 0 additions & 40 deletions
This file was deleted.

test/weak_reference_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_inspect
4848
def test_basic_object_does_not_throw_exception
4949
obj = BasicObject.new
5050
ref = Ref::WeakReference.new(obj)
51-
assert_equal obj, ref.object
51+
assert_equal obj.__id__, ref.object.__id__
5252
end
5353
end
5454
end

0 commit comments

Comments
 (0)