Skip to content

Commit ff20241

Browse files
authored
perf: Reduce compile time by trimming template expansion in IBA (AcademySoftwareFoundation#4476)
I was profiling the builds and saw that modules with lots of template expansion dominate the compile time. For example, imagebufalgo_pixelmath.cpp alone took 290s to compile on my 2020 MacbookPro (!), and imagebufalgo_addsub.cpp took 84 seconds. This is all due to the combnatorics of expanding IBA templates via the DISPATCH macros in imagebufalgo_util.h separately for every type that the arguments can be. But I claim that most combinations are rarely if ever used. I mean, how often does anybody need IBA::add() to add an int8 image to an int16 image? So this PR rewrites those macros to simplify the cases as follows: * The common pixel data types are float, half, unint8, and uint16. * Specialized versions are fully expanded only when the result and input images are one of these types. Images not of one of those types are first automatically converted to float to make them reduce to a common case. That makes uncommon pixel data types like (signed) int16 not expand the template, but rather convert to and from float and use the float specializations. * For binary and ternary operations (those with 2 or 3 image inputs), if the pixel types of the inputs doesn't match, we make sure they both are converted to float. So, for example, we don't need a specialized version that adds a half image to a uint16 image -- just convert them to float and use the common case. But we do specalize if the two inputs are both the same common case, such as adding two uint16 images. * Assume that commonly, the result image will either be float, or will be the same pixel data type as the inputs. Other combinations trigger assignment to a temporary float IB, then copying with convertion to the uncommon use-supplied result buffer. * Additionally, we cut down on a little bit more templating by moving some "deep" methods from the type-templated ImageBuf::Iterator to its type-generic non-templated base class IteraterBase. The net result of all this is an awful lot less template expansion. With this in place, my laptop compiles imagebufalgo_pixelmath.cpp in 97s (vs 290 before) and imagebufalgo_addsub.cpp in 26s (from 84). It takes a big bite out of all the iba files, and reduces project-wide compile time by over 10%, around 30s out of 300 for a fresh, uncached, optimized build with 16 threads. Signed-off-by: Larry Gritz <[email protected]>
1 parent 704d0db commit ff20241

File tree

2 files changed

+305
-174
lines changed

2 files changed

+305
-174
lines changed

src/include/OpenImageIO/imagebuf.h

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,30 @@ class OIIO_API ImageBuf {
16041604
m_nchannels);
16051605
}
16061606

1607+
/// Set the number of deep data samples at this pixel. (Only use
1608+
/// this if deep_alloc() has not yet been called on the buffer.)
1609+
void set_deep_samples(int n)
1610+
{
1611+
ensure_writable();
1612+
return const_cast<ImageBuf*>(m_ib)->set_deep_samples(m_x, m_y, m_z,
1613+
n);
1614+
}
1615+
1616+
/// Set the deep data value of sample s of channel c. (Only use this
1617+
/// if deep_alloc() has been called.)
1618+
void set_deep_value(int c, int s, float value)
1619+
{
1620+
ensure_writable();
1621+
return const_cast<ImageBuf*>(m_ib)->set_deep_value(m_x, m_y, m_z, c,
1622+
s, value);
1623+
}
1624+
void set_deep_value(int c, int s, uint32_t value)
1625+
{
1626+
ensure_writable();
1627+
return const_cast<ImageBuf*>(m_ib)->set_deep_value(m_x, m_y, m_z, c,
1628+
s, value);
1629+
}
1630+
16071631
protected:
16081632
friend class ImageBuf;
16091633
friend class ImageBufImpl;
@@ -1799,30 +1823,6 @@ class OIIO_API ImageBuf {
17991823
TypeDesc::BASETYPE(m_pixeltype), m_proxydata,
18001824
m_nchannels);
18011825
}
1802-
1803-
/// Set the number of deep data samples at this pixel. (Only use
1804-
/// this if deep_alloc() has not yet been called on the buffer.)
1805-
void set_deep_samples(int n)
1806-
{
1807-
ensure_writable();
1808-
return const_cast<ImageBuf*>(m_ib)->set_deep_samples(m_x, m_y, m_z,
1809-
n);
1810-
}
1811-
1812-
/// Set the deep data value of sample s of channel c. (Only use this
1813-
/// if deep_alloc() has been called.)
1814-
void set_deep_value(int c, int s, float value)
1815-
{
1816-
ensure_writable();
1817-
return const_cast<ImageBuf*>(m_ib)->set_deep_value(m_x, m_y, m_z, c,
1818-
s, value);
1819-
}
1820-
void set_deep_value(int c, int s, uint32_t value)
1821-
{
1822-
ensure_writable();
1823-
return const_cast<ImageBuf*>(m_ib)->set_deep_value(m_x, m_y, m_z, c,
1824-
s, value);
1825-
}
18261826
};
18271827

18281828

0 commit comments

Comments
 (0)