README.md in batch-loader-1.3.0 vs README.md in batch-loader-1.4.0

- old
+ new

@@ -18,10 +18,11 @@ * [RESTful API example](#restful-api-example) * [GraphQL example](#graphql-example) * [Loading multiple items](#loading-multiple-items) * [Batch key](#batch-key) * [Caching](#caching) + * [Replacing methods](#replacing-methods) * [Installation](#installation) * [API](#api) * [Implementation details](#implementation-details) * [Development](#development) * [Contributing](#contributing) @@ -372,10 +373,25 @@ puts user_lazy(1) # SELECT * FROM users WHERE id IN (1) puts user_lazy(1) # SELECT * FROM users WHERE id IN (1) ``` +If you set `cache: false`, it's likely you also want `replace_methods: false` (see below section). + +### Replacing methods + +By default, `BatchLoader` replaces methods on its instance by calling `#define_method` after batching to copy methods from the loaded value. +This consumes some time but allows to speed up any future method calls on the instance. +In some cases, when there are a lot of instances with a huge number of defined methods, this initial process of replacing the methods can be slow. +You may consider avoiding the "up front payment" and "pay as you go" with `#method_missing` by disabling the method replacement: + +```ruby +BatchLoader.for(id).batch(replace_methods: false) do |ids, loader| + # ... +end +``` + ## Installation Add this line to your application's Gemfile: ```ruby @@ -391,23 +407,29 @@ $ gem install batch-loader ## API ```ruby -BatchLoader.for(item).batch(default_value: default_value, cache: cache, key: key) do |items, loader, args| +BatchLoader.for(item).batch( + default_value: default_value, + cache: cache, + replace_methods: replace_methods, + key: key +) do |items, loader, args| # ... end ``` -| Argument Key | Default | Description | -| --------------- | --------------------------------------------- | ------------------------------------------------------------- | -| `item` | - | Item which will be collected and used for batching. | -| `default_value` | `nil` | Value returned by default after batching. | -| `cache` | `true` | Set `false` to disable caching between the same executions. | -| `key` | `nil` | Pass custom key to uniquely identify the batch block. | -| `items` | - | List of collected items for batching. | -| `loader` | - | Lambda which should be called to load values loaded in batch. | -| `args` | `{default_value: nil, cache: true, key: nil}` | Arguments passed to the `batch` method. | +| Argument Key | Default | Description | +| --------------- | --------------------------------------------- | ------------------------------------------------------------- | +| `item` | - | Item which will be collected and used for batching. | +| `default_value` | `nil` | Value returned by default after batching. | +| `cache` | `true` | Set `false` to disable caching between the same executions. | +| `replace_methods` | `true` | Set `false` to use `#method_missing` instead of replacing the methods after batching. | +| `key` | `nil` | Pass custom key to uniquely identify the batch block. | +| `items` | - | List of collected items for batching. | +| `loader` | - | Lambda which should be called to load values loaded in batch. | +| `args` | `{default_value: nil, cache: true, replace_methods: true, key: nil}` | Arguments passed to the `batch` method. | ## Implementation details See the [slides](https://speakerdeck.com/exaspark/batching-a-powerful-way-to-solve-n-plus-1-queries) [37-42].