Commit 2fcb4af
committed
Optimize Font::CMap#read_codes to lower memory usage
The current code use string.each_byte which returns an Enumerator. It
then uses calls to enum.next for getting the next byte.
It turns out that this is quite wasteful in terms of memory usage as
each call to #next seems to allocate (don't really know why) an
Enumerator, an Array, a Proc and a Fiber.
By using manual iteration all these allocations are avoided, leading to
quite noticeable lower memory usage (sample script that decodes text of a
PDF):
Before:
Initial memory: 17.26MB, Memory maps: 179
Processing 306 pages...
<SNIP/>
Final memory: 193.55MB (total growth: +176.29MB)
Final memory maps: 14485 (total growth: +14306)
After:
Initial memory: 17.31MB, Memory maps: 177
Processing 306 pages...
<SNIP/>
Final memory: 73.47MB (total growth: +56.16MB)
Final memory maps: 200 (total growth: +23)1 parent ac073f2 commit 2fcb4af
2 files changed
Lines changed: 12 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
146 | | - | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
147 | 149 | | |
148 | | - | |
149 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
150 | 153 | | |
151 | 154 | | |
152 | 155 | | |
153 | 156 | | |
154 | 157 | | |
155 | 158 | | |
156 | 159 | | |
157 | | - | |
158 | | - | |
159 | | - | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
160 | 164 | | |
161 | 165 | | |
162 | 166 | | |
| |||
0 commit comments