rails/rails — pull request #17077 by schneems (2014-09-27 03:08:42+00:00)
Title: Do not create 700 string objects on EVERY request

Right now BenchmarkCleaner allocates hundreds of strings on every request. This patch moves those strings to be generated at boot once and re-used.

## Bench Methods

I took a modified form of CodeTriage https://github.com/schneems/codetriage-ko1-test-app/blob/master/perf.rake and ran given rake tasks with and without the patch to BacktraceCleaner.

I made an endpoint that does very little

```
def index
render text: "foo"
end
```

The gem `memory_profiler` was used to capture objects allocated for a single request. Then `benchmark/ips` was used to test the speed of the patch.

You will see that we use less objects and the code becomes measurably faster with this patch.

## With patch:

Memory for one request:

```
Total allocated 1919
Total retained 5
```

Requests per second:

```
Calculating -------------------------------------
ips 23 i/100ms
-------------------------------------------------
ips 235.1 (±8.9%) i/s - 1173 in 5.041271s
```

## Without patch:

Memory used for one request:

```
Total allocated 2619
Total retained 6
```

Requests per second:

```
Calculating -------------------------------------
ips 21 i/100ms
-------------------------------------------------
ips 218.7 (±6.4%) i/s - 1092 in 5.012819s
```

## Analysis

The patch is faster:

```
(235.1 - 218.7 ) / 218.7 * 100
# => 7.4 # % ~ speed bump
```

It also allocates less objects:

```
2619 - 1919
# => 700
```

These strings are allocated on __EVERY SINGLE REQUEST__. This patch saves us 700 objects __PER REQUEST__.

I wanted to focus this patch on one performance optimization, but we can likely optimize BacktraceCleaner much more.