Commit 7929c81
authored
perf: file read performance -- double reads, extra copies (#4507)
Doing some benchmarking and adding extra LoggedTimer instrumentation
(included in another PR), I discovered that quite frequently, the
in-memory flavor of ImageBuf was reading its image multiple times --
once upon construction, and then again upon later calls to read(), even
though the latter should have taken early outs when the read was
unnecessary.
So I put some additional mechanism for the IB to internally know if it
read the pixels from disk into the buffer, and more effectively make
calls to IB.read() be a no-op. To help facilitate this, I also clarified
in the docs that read() should not be used to move an already-read image
to a different subimage or MIP level or a different data type -- reset()
really should be used for that purpose. (I don't know that anybody ever
relied on read() to do reset's job, but assuming that they won't helps
eliminate some tricky cases.)
Also, I noticed that inside ImageInput::read_image(), depending on
whether or not a data type conversion is needed, it can either read
directly into the user's buffer, or must read the native data into a
temp buffer and subsequently do a copy-and-type-convert into the user's
buffer. The latter is obviously more expensive. The direct read approach
was used when the user asked for no type conversion (just to receive the
native data type in the file), but it neglected to take the same
shortcut if the user asked for an explicit type conversion, but it just
so happened that the requested type was the type in the file.
Additional changes:
The evaluation of whether the buffer was contiguous (for various
shortcuts) only applied to LOCALBUFFER style storage, not APPBUFFER.
Fixing these two problems dramatically speeds up some reads. A benchmark
I was looking at where it reads a 10k x 10k uint8 uncompressed TIFF file
into an ImageBuf while requesting uint8 data went from (timings on my
laptop):
function calls total
IB::read 2 0.505s (avg 0.25s)
II::read_image 2 0.478s (avg 0.24s)
to:
IB::read 1 0.172s (avg 0.17s)
II::read_image 1 0.172s (avg 0.17s)
So almost 3x faster. Most of which was due doing 2 reads instead of 1,
but still each individual read improved from 0.25s to 0.17s because of
the fix that lets us avoid the unnecessary buffer copies.
Grain of salt: this was a very large image, and it was stored
uncompressed (so NO decompression that can sometimes be a significant
amount of overhead), so you may not see this much improvement in real
world scenarios.
This is only scratching the surface, I believe there are lots of other
areas to optimize more carefully.
Signed-off-by: Larry Gritz <[email protected]>1 parent 95d246d commit 7929c81
File tree
5 files changed
+92
-42
lines changed- src
- include/OpenImageIO
- libOpenImageIO
5 files changed
+92
-42
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
416 | 416 | | |
417 | 417 | | |
418 | 418 | | |
419 | | - | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
425 | 423 | | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
440 | 432 | | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
445 | | - | |
446 | | - | |
447 | | - | |
448 | | - | |
449 | | - | |
450 | | - | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
451 | 457 | | |
452 | 458 | | |
453 | 459 | | |
| |||
460 | 466 | | |
461 | 467 | | |
462 | 468 | | |
463 | | - | |
| 469 | + | |
464 | 470 | | |
465 | 471 | | |
466 | 472 | | |
| |||
1219 | 1225 | | |
1220 | 1226 | | |
1221 | 1227 | | |
| 1228 | + | |
1222 | 1229 | | |
1223 | 1230 | | |
1224 | 1231 | | |
| |||
1242 | 1249 | | |
1243 | 1250 | | |
1244 | 1251 | | |
1245 | | - | |
| 1252 | + | |
1246 | 1253 | | |
1247 | 1254 | | |
1248 | 1255 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
317 | 317 | | |
318 | 318 | | |
319 | 319 | | |
320 | | - | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
321 | 323 | | |
322 | 324 | | |
323 | 325 | | |
| |||
347 | 349 | | |
348 | 350 | | |
349 | 351 | | |
| 352 | + | |
350 | 353 | | |
351 | 354 | | |
352 | 355 | | |
| |||
421 | 424 | | |
422 | 425 | | |
423 | 426 | | |
| 427 | + | |
424 | 428 | | |
425 | 429 | | |
426 | 430 | | |
| |||
506 | 510 | | |
507 | 511 | | |
508 | 512 | | |
| 513 | + | |
509 | 514 | | |
510 | 515 | | |
511 | 516 | | |
| |||
533 | 538 | | |
534 | 539 | | |
535 | 540 | | |
| 541 | + | |
536 | 542 | | |
537 | 543 | | |
538 | 544 | | |
| |||
804 | 810 | | |
805 | 811 | | |
806 | 812 | | |
| 813 | + | |
807 | 814 | | |
808 | 815 | | |
809 | 816 | | |
| |||
856 | 863 | | |
857 | 864 | | |
858 | 865 | | |
| 866 | + | |
859 | 867 | | |
860 | | - | |
861 | | - | |
862 | | - | |
863 | | - | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
864 | 874 | | |
865 | 875 | | |
866 | 876 | | |
| |||
1004 | 1014 | | |
1005 | 1015 | | |
1006 | 1016 | | |
1007 | | - | |
| 1017 | + | |
| 1018 | + | |
1008 | 1019 | | |
1009 | 1020 | | |
1010 | 1021 | | |
| |||
1138 | 1149 | | |
1139 | 1150 | | |
1140 | 1151 | | |
| 1152 | + | |
1141 | 1153 | | |
1142 | 1154 | | |
1143 | 1155 | | |
| |||
1207 | 1219 | | |
1208 | 1220 | | |
1209 | 1221 | | |
| 1222 | + | |
1210 | 1223 | | |
1211 | 1224 | | |
1212 | 1225 | | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
1213 | 1229 | | |
1214 | 1230 | | |
1215 | 1231 | | |
1216 | 1232 | | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
1217 | 1243 | | |
1218 | 1244 | | |
1219 | 1245 | | |
| |||
1241 | 1267 | | |
1242 | 1268 | | |
1243 | 1269 | | |
| 1270 | + | |
1244 | 1271 | | |
1245 | 1272 | | |
1246 | 1273 | | |
| |||
1348 | 1375 | | |
1349 | 1376 | | |
1350 | 1377 | | |
| 1378 | + | |
1351 | 1379 | | |
1352 | 1380 | | |
1353 | 1381 | | |
| |||
1943 | 1971 | | |
1944 | 1972 | | |
1945 | 1973 | | |
| 1974 | + | |
| 1975 | + | |
| 1976 | + | |
| 1977 | + | |
| 1978 | + | |
| 1979 | + | |
| 1980 | + | |
| 1981 | + | |
1946 | 1982 | | |
1947 | 1983 | | |
1948 | 1984 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
354 | 354 | | |
355 | 355 | | |
356 | 356 | | |
357 | | - | |
| 357 | + | |
358 | 358 | | |
359 | 359 | | |
360 | 360 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
1109 | 1110 | | |
1110 | 1111 | | |
1111 | 1112 | | |
1112 | | - | |
1113 | | - | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
1114 | 1117 | | |
1115 | 1118 | | |
1116 | 1119 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
302 | 302 | | |
303 | 303 | | |
304 | 304 | | |
| 305 | + | |
305 | 306 | | |
306 | 307 | | |
307 | 308 | | |
| |||
330 | 331 | | |
331 | 332 | | |
332 | 333 | | |
333 | | - | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
334 | 338 | | |
335 | 339 | | |
336 | 340 | | |
| |||
0 commit comments