File: README.md

package info (click to toggle)
ruby-ddmemoize 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 140 kB
  • sloc: ruby: 327; makefile: 3
file content (120 lines) | stat: -rw-r--r-- 2,865 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
[![Gem version](https://img.shields.io/gem/v/ddmemoize.svg)](http://rubygems.org/gems/ddmemoize)
[![Gem downloads](https://img.shields.io/gem/dt/ddmemoize.svg)](http://rubygems.org/gems/ddmemoize)
[![Build status](https://img.shields.io/travis/ddfreyne/ddmemoize.svg)](https://travis-ci.org/ddfreyne/ddmemoize)
[![Code Climate](https://img.shields.io/codeclimate/github/ddfreyne/ddmemoize.svg)](https://codeclimate.com/github/ddfreyne/ddmemoize)
[![Code Coverage](https://img.shields.io/codecov/c/github/ddfreyne/ddmemoize.svg)](https://codecov.io/gh/ddfreyne/ddmemoize)

# DDMemoize

_DDMemoize_ adds support for memoizing Ruby functions.

For example, the following Fibonacci implementation runs quickly (in O(n) rather than in O(2^n)):

```ruby
class FibFast
  DDMemoize.activate(self)

  memoized def run(n)
    if n == 0
      0
    elsif n == 1
      1
    else
      run(n - 1) + run(n - 2)
    end
  end
end
```

Features:

* Supports memoizing functions on frozen objects
* Releases memoized values when needed in order to reduce memory pressure
* Optionally records metrics

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'ddmemoize'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install ddmemoize

## Usage

First, require `ddmemoize` and enable it using `DDMemoize.activate`:

```ruby
require 'ddmemoize'

class FibFast
  DDMemoize.activate(self)

  # …
end
```

To memoize a function, call `memoize` with the name of the function:

```ruby
  def run(n)
    # …
  end
  memoize :run
```

Alternatively, prepend `memoized` to the function definition:

```ruby
  memoized def run(n)
    # …
  end
```

Do not memoize functions that depend on mutable state.

### Metrics

To activate metrics, call `DDMemoize.enable_metrics` after requiring `ddmemoize`.

To print the collected metrics, call `DDMemoize.print_metrics`:

```ruby
DDMemoize.print_metrics
```

```
memoization │ hit   miss       %
────────────┼───────────────────
FibFast#fib │ 998   1001   49.9%
```

## Development

Install dependencies:

    $ bundle

Run tests:

    $ bundle exec rake

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ddfreyne/ddmemoize. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.

## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

## Code of Conduct

Everyone interacting in the DDMemoize project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ddfreyne/ddmemoize/blob/master/CODE_OF_CONDUCT.md).