Commit 11302ee
committed
Deduplicate Regexp literals
Ruby ticket: https://bugs.ruby-lang.org/issues/16557
Real world application contain many duplicated Regexp literals.
From a rails/console in Redmine:
```
>> ObjectSpace.each_object(Regexp).count
=> 6828
>> ObjectSpace.each_object(Regexp).uniq.count
=> 4162
>> ObjectSpace.each_object(Regexp).to_a.map { |r| ObjectSpace.memsize_of(r) }.sum
=> 4611957 # 4.4 MB total
>> ObjectSpace.each_object(Regexp).to_a.map { |r| ObjectSpace.memsize_of(r) }.sum - ObjectSpace.each_object(Regexp).to_a.uniq.map { |r| ObjectSpace.memsize_of(r) }.sum
=> 1490601 # 1.42 MB could be saved
```
Here's the to 10 duplicated regexps in Redmine:
```
147: /"/
107: /\s+/
103: //
89: /\n/
83: /'/
76: /\s+/m
37: /\d+/
35: /\[/
33: /./
33: /\\./
```
Any empty Rails application will have a similar amount of regexps.
Since https://bugs.ruby-lang.org/issues/16377 made literal regexps frozen, it is possible to deduplicate literal regexps without changing any semantic.
This patch is heavily inspired by the `frozen_strings` table, but applied to literal regexps.1 parent ad1ebef commit 11302ee
3 files changed
+47
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| |||
2956 | 2957 | | |
2957 | 2958 | | |
2958 | 2959 | | |
| 2960 | + | |
| 2961 | + | |
| 2962 | + | |
| 2963 | + | |
| 2964 | + | |
| 2965 | + | |
| 2966 | + | |
| 2967 | + | |
| 2968 | + | |
| 2969 | + | |
| 2970 | + | |
| 2971 | + | |
| 2972 | + | |
| 2973 | + | |
| 2974 | + | |
| 2975 | + | |
| 2976 | + | |
| 2977 | + | |
| 2978 | + | |
| 2979 | + | |
| 2980 | + | |
| 2981 | + | |
| 2982 | + | |
2959 | 2983 | | |
2960 | 2984 | | |
2961 | 2985 | | |
2962 | | - | |
2963 | | - | |
2964 | | - | |
2965 | 2986 | | |
| 2987 | + | |
| 2988 | + | |
| 2989 | + | |
| 2990 | + | |
| 2991 | + | |
| 2992 | + | |
| 2993 | + | |
| 2994 | + | |
| 2995 | + | |
2966 | 2996 | | |
2967 | | - | |
2968 | | - | |
| 2997 | + | |
| 2998 | + | |
2969 | 2999 | | |
2970 | 3000 | | |
2971 | 3001 | | |
| 3002 | + | |
| 3003 | + | |
2972 | 3004 | | |
2973 | 3005 | | |
2974 | 3006 | | |
| |||
4111 | 4143 | | |
4112 | 4144 | | |
4113 | 4145 | | |
| 4146 | + | |
| 4147 | + | |
4114 | 4148 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
65 | 72 | | |
66 | 73 | | |
67 | 74 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
634 | 634 | | |
635 | 635 | | |
636 | 636 | | |
| 637 | + | |
637 | 638 | | |
638 | 639 | | |
639 | 640 | | |
| |||
0 commit comments