This repository has been archived by the owner on Mar 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from ruby-concurrency/initial-updates
Initial updates
- Loading branch information
Showing
19 changed files
with
158 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,6 @@ tmp | |
rdoc | ||
*.rbc | ||
coverage | ||
.ruby-version | ||
.ruby-version | ||
.ruby-gemset | ||
Gemfile.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,27 @@ | ||
language: ruby | ||
|
||
rvm: | ||
- 1.8.7 | ||
- 1.9.2 | ||
- rbx | ||
- jruby | ||
- 2.2.0 | ||
- 2.1.5 | ||
- 2.1.4 | ||
- 2.0.0 | ||
- 1.9.3 | ||
- ruby-head | ||
- jruby-1.7.18 | ||
- jruby-head | ||
- rbx-2 | ||
|
||
jdk: | ||
- oraclejdk8 | ||
|
||
sudo: false | ||
|
||
branches: | ||
only: | ||
- master | ||
|
||
matrix: | ||
allow_failures: | ||
- rvm: ruby-head | ||
- rvm: jruby-head | ||
- rvm: 1.9.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
source 'https://rubygems.org' | ||
|
||
gemspec | ||
|
||
group :development do | ||
gem 'rake', '~> 10.3.2' | ||
end | ||
|
||
group :testing do | ||
gem 'test-unit', '~> 3.0.9' | ||
#gem 'rspec', '~> 3.0.0' | ||
#gem 'simplecov', '~> 0.8.2', :require => false | ||
gem 'coveralls', '~> 0.7.3', :require => false | ||
end | ||
|
||
group :documentation do | ||
gem 'yard', '~> 0.8.7.4', :require => false | ||
gem 'inch', '~> 0.4.6', :platforms => :mri, :require => false | ||
gem 'redcarpet', '~> 3.1.2', platforms: :mri # understands github markdown | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Ref | ||
|
||
[![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) [![Coverage Status](https://img.shields.io/coveralls/ruby-concurrency/ref/master.svg)](https://coveralls.io/r/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) | ||
|
||
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. | ||
|
||
* `Ref::StrongReference` - This is a plain old pointer to another object. | ||
* `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. | ||
* `Ref::SoftReference` - This is similar to a weak reference, but the garbage collector is not as eager to reclaim the referenced object. | ||
|
||
All of these classes extend from a common `Ref::Reference` class and have a common interface. | ||
|
||
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. | ||
|
||
## Example Usage | ||
|
||
```ruby | ||
ref = Ref::WeakReference.new("hello") | ||
ref.object # should be "hello" | ||
ObjectSpace.garbage_collect | ||
ref.object # should be nil (assuming the garbage collector reclaimed the reference) | ||
``` | ||
|
||
## Goodies | ||
|
||
This library also includes tools for some common uses of weak and soft references. | ||
|
||
* `Ref::WeakKeyMap` - A map of keys to values where the keys are weak references | ||
* `Ref::WeakValueMap` - A map of keys to values where the values are weak references | ||
* `Ref::SoftKeyMap` - A map of keys to values where the keys are soft references | ||
* `Ref::SoftValueMap` - A map of keys to values where the values are soft references | ||
* `Ref::ReferenceQueue` - A thread safe implementation of a queue that will add references to itself as their objects are garbage collected. | ||
|
||
## Problems with WeakRef | ||
|
||
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. | ||
|
||
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. | ||
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. | ||
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. | ||
4. Rubinius - Rubinius implements `WeakRef` with a lighter weight version of delegation and works very well. | ||
5. MRI Ruby 2.0 has a good implementation of `WeakRef`. | ||
|
||
## BasicObject | ||
|
||
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. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Ref | ||
VERSION = '1.0.5' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,28 @@ | ||
$:.push File.join(File.dirname(__FILE__), 'lib') | ||
|
||
require 'ref/version' | ||
|
||
Gem::Specification.new do |s| | ||
s.name = 'ref' | ||
s.version = File.read(File.expand_path("../VERSION", __FILE__)).strip | ||
s.summary = "Library that implements weak, soft, and strong references in Ruby." | ||
s.name = 'ref' | ||
s.version = Ref::VERSION | ||
s.authors = ['Brian Durand'] | ||
s.email = ['bbdurand@gmail.com'] | ||
s.homepage = "http://github.com/ruby-concurrency/ref" | ||
s.summary = "Library that implements weak, soft, and strong references in Ruby." | ||
s.description = "Library that implements weak, soft, and strong references in Ruby that work across multiple runtimes (MRI, REE, YARV, Jruby, Rubinius, and IronRuby). Also includes implementation of maps/hashes that use references and a reference queue." | ||
s.license = "MIT" | ||
s.date = Time.now.strftime('%Y-%m-%d') | ||
|
||
s.authors = ['Brian Durand'] | ||
s.email = ['bbdurand@gmail.com'] | ||
s.homepage = "http://github.com/bdurand/ref" | ||
s.files = ['README.md', 'MIT_LICENSE'] | ||
s.files += Dir['lib/**/*.*'] | ||
s.files += Dir['ext/**/*.*'] | ||
s.files += Dir['test/**/*.*'] | ||
|
||
s.files = ['README.rdoc', 'VERSION', 'MIT_LICENSE'] + Dir.glob('lib/**/*'), Dir.glob('test/**/*'), Dir.glob('ext/**/*') | ||
s.require_path = 'lib' | ||
s.require_paths = ['lib'] | ||
|
||
s.has_rdoc = true | ||
s.rdoc_options = ["--charset=UTF-8", "--main", "README.rdoc"] | ||
s.extra_rdoc_files = ["README.rdoc"] | ||
s.license = "MIT" | ||
s.has_rdoc = true | ||
s.rdoc_options = ["--charset=UTF-8", "--main", "README.md"] | ||
s.extra_rdoc_files = ["README.md"] | ||
|
||
s.required_ruby_version = '>= 1.9.3' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
require 'coveralls' | ||
Coveralls.wear! | ||
|
||
require 'test/unit' | ||
require File.expand_path("../../lib/ref", __FILE__) | ||
require File.expand_path("../reference_key_map_behavior", __FILE__) | ||
require File.expand_path("../reference_value_map_behavior", __FILE__) | ||
|
||
require 'ref' | ||
require 'ref/mock' | ||
|
||
require_relative './reference_key_map_behavior' | ||
require_relative './reference_value_map_behavior' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters