Skip to content

bug: ImageInput::open() forces full OpenColorIO config initialization even when no color management is requested #5490

Closed

Description

@bartosz216

Opening a single image adds a fixed ~20 ms to the first ImageInput::open()
in a process, spent constructing the default OpenColorIO config. This happens
for callers that only want metadata and never perform a color transform.

Where

ImageSpec::set_colorspace() (src/libOpenImageIO/formatspec.cpp):

void ImageSpec::set_colorspace(string_view colorspace)
{
    ColorConfig::default_colorconfig().set_colorspace(*this, colorspace);
}

ColorConfig::default_colorconfig() is a function-local static ColorConfig,
so the first call constructs the whole config. Several readers call
set_colorspace() unconditionally in open(), among them:

  • src/jpeg.imageio/jpeginput.cpp — m_spec.set_colorspace("srgb_rec709_scene")
  • src/hdr.imageio/hdrinput.cpp — m_spec.set_colorspace("lin_rec709_scene")
  • src/dds.imageio/ddsinput.cpp — m_spec.set_colorspace(colorspace)

Measurement

OIIO 3.1.14.0 (vcpkg), release build, JPEG input (960x639, 3 ch), median of
5 runs. Timed region is ImageInput::open() plus reading the ImageSpec.
The only difference between the two rows is one added line; OIIO is not
rebuilt.

median range
as-is 0.0208 s 0.0203 – 0.0217
ColorConfig::default_colorconfig(); called before the timer 0.00049 s 0.00045 – 0.00055

Spread is under 10% in both, so this is not a cold-cache artifact. Standalone
reproducer attached.

The cost scales with the OpenColorIO version: ~10 ms with OCIO 2.2.1, ~20 ms with OCIO 2.5.2, both with no OCIO environment variable set.

Why it looks avoidable

ColorConfig::set_colorspace() needs the config for exactly one thing:

if (!equivalent(colorspace, "sRGB"))
    spec.erase_attribute("Exif:ColorSpace");

In JpgInput::open(), set_colorspace() runs at ~line 329 while
decode_exif() runs at ~line 352, so at that point Exif:ColorSpace,
tiff:ColorSpace, tiff:PhotometricInterpretation and oiio:Gamma are all
absent from the freshly constructed spec. Every erase is a no-op and the
equivalent() result cannot change the outcome.

The tree already does this the cheap way elsewhere. src/png.imageio/png_pvt.h
makes the same kind of assumption without touching the config:

spec.attribute("oiio:ColorSpace", "srgb_rec709_scene");

and tiffinput.cpp sidesteps set_colorspace() explicitly, though for an
unrelated reason:

// NOTE: We must set "oiio:ColorSpace" explicitly, not call
// set_colorspace, or it will erase several other TIFF attribs...

#4433 did the equivalent for oiiotool startup.

Possible directions

  1. Have ImageSpec::set_colorspace() avoid touching default_colorconfig()
    when none of the attributes it would erase are present.
  2. Make the default ColorConfig construction cheaper or lazier.

Happy to prepare a PR for either if you have a preference.

Environment

OpenImageIO 3.1.14.0 (vcpkg), OpenColorIO 2.5.2, no OCIO environment
variable set (so this is the built-in config). Linux 6.18 (WSL2), gcc 13.3.0,
Intel i7-5820K. The absolute cost will be lower on newer hardware; the claim
here is the fixed cost that disappears when the config is constructed before
the timed region, not the specific number.

Activity

  1. zachlewis commented on Sep 23, 2026

    @zachlewis
    Collaborator

    I replied in many more words in #4629, but basically, what I'm proposing amounts to readers not invoking set_colorspace() at all, and setting the 'oiio:ColorSpace' attribute directly (to known colorInteropID strings); and otherwise ensuring that ColorConfig instances properly recognize those values as valid color spaces.
    In other words, readers won't be creating new ColorConfig instances, they'll just be setting attributes in the spec; and those attributes will be reconciled with the active color config as lazily as possible.

    Sound good?

  2. lgritz commented on Sep 23, 2026

    @lgritz
    Collaborator

    So the fundamental problem is that there's more overhead than we'd like in OCIO when you set up a config, including the default built-in configs?

    And ImageSpec::set_colorspace is a really thin wrapper for default_colorconfig().set_colorspace(spec, colorspace), which mostly does some inexpensive operations on the spec, except for one call to ColorConig::equivalent(). But the problem is that merely calling this method means that the ColorConfig has to been constructed.

    So how about this:

    Move most of the spec-related logic from ColorConfig::set_colorspace into ImageSpec::set_colorspace, so now you have

    void
    ImageSpec::set_colorspace(string_view colorspace)
    {
        // HERE, do everything that ColorConfig::set_colorspace used to do, EXCEPT the part
        // where it says 
        //     if (!equivalent(colorspace, "srgb_rec709_scene"))
        //         spec.erase_attribute("Exif:ColorSpace");
    
        // do those things
    
        // Now, the magic bits:
        if (colorsace != "srgb_rec709_scene" && spec.has_attribute("Exif:ColorSpace"))
            if (!default_colorconfig().equivalent(colorspace, "srgb_rec709_scene"))
                spec.erase_attribute("Exif:ColorSpace");
    }
    

    See? Now it's only in the specific circumstance that you might need to erase that attribute that it even does the check that triggers construction of the default_colorconfig.

    Just need to double check that we aren't directly calling ColorConfig::set_colorspace anywhere else that will need to get directed to ImageSpec::set_colorspace.

  3. zachlewis commented on Sep 24, 2026

    @zachlewis
    Collaborator

    Just need to double check that we aren't directly calling ColorConfig::set_colorspace anywhere else that will need to get directed to ImageSpec::set_colorspace.

    This makes me nervous. I'm doing lots of stuff to set_colorspace. Need to double check, but... is there any reason why we can't just have the readers set oiio:ColorSpace directly instead of invoking set_colorspace at this stage?

  4. zachlewis commented on Sep 24, 2026

    @zachlewis
    Collaborator

    (Or... I mean, it's fine as an intermediate fix; and I can work around it, if that's the direction we want to go.)

  5. lgritz commented on Sep 24, 2026

    @lgritz
    Collaborator

    This makes me nervous. I'm doing lots of stuff to set_colorspace. Need to double check, but... is there any reason why we can't just have the readers set oiio:ColorSpace directly instead of invoking set_colorspace at this stage?

    In general, set_colorspace was supposed to be preferred over just a raw set of "oiio:ColorSpace" because it was a place where we could do other things, like clearing attributes that were contradicted by the choice of colorspace.

    But I suppose that for an input plugin, it's constructing the whole spec itself, so it's not necessarily a case of "you don't know what else is there."

    So, sure, I guess it's fine to have particular readers just set the attribute and no call set_colorspace, if you are sure it's safe.

    I continue to think that ImageSpec::set_colorspace should not need to cause a ColorConfig to be instantiated (even the default one) except in cases where it's needed.

  6. zachlewis commented on Sep 24, 2026

    @zachlewis
    Collaborator

    But I suppose that for an input plugin, it's constructing the whole spec itself, so it's not necessarily a case of "you don't know what else is there."

    Yes -- I think, beyond bringing encodings to RGB(A) full range float buffers, readers should probably take a passive role, and just persist metadata as-is, to whatever degree we can, and leave the reconciliation of oiio:ColorSpace from the various known "facts" collected by the reader to a downstream ColorConfig-aware process.

    To be perfectly honest, I'm planning on proposing that readers not directly populate oiio:ColorSpace at all, and, instead, populate the colorInteropID attribute; which would leave room for the actual reconciliation of oiio:ColorSpace to include the OCIO Config's FileRules as part of the deferred resolution process (instead of having readers always presuming that file metadata and/or format defaults always takes precedence over file naming). Just something to think about.

    In general, set_colorspace was supposed to be preferred over just a raw set of "oiio:ColorSpace" because it was a place where we could do other things, like clearing attributes that were contradicted by the choice of colorspac

    Yes, absolutely... it's just not clear to me how we would empirically assess the validity of potentially contradictory color metadata attributes in the spec without an actual OCIO color space to introspect and compare against (without resorting to hand-rolled heuristics that only cover a handful of real-world cases).

    I continue to think that ImageSpec::set_colorspace should not need to cause a ColorConfig to be instantiated (even the default one) except in cases where it's needed.

    I'm not totally against this, but I'm struggling to understand what you have in mind. To me, the clearing of contradictory metadata aspect of set_colorspace is pretty contingent on having a valid basis for comparison (or, at minimum, a valid alternative!). Specifically, I don't think it's safe to automatically clear attributes just because we're using set_colorspace to set oiio:ColorSpace -- if that value cannot be interpreted by a ColorConfig later, then we've lost information that might have otherwise been used to reconcile an appropriate OCIO Processor for an IBA that needs a ColorConfig...

  7. zachlewis commented on Sep 24, 2026

    @zachlewis
    Collaborator

    if you are sure it's safe.

    (Oh, and to be perfectly clear, I am still making it safe to set oiio:ColorSpace directly to a known colorInteropID, should someone want to)

  8. bartosz216 commented on Sep 24, 2026

    @bartosz216
    Author

    Yes - that sounds right. Deferring the config until something actually needs it removes the cost at the source, instead of special-casing readers one at a time.

    Two notes.

    Since the measurements in both threads have been EXR so far, it may be worth connecting this back to lgritz's "(b) do we care" in #4629: the cost isn't EXR-specific. As in the description above, jpeginput.cpp calls set_colorspace("srgb_rec709_scene") unconditionally in open(), so any process that opens a single JPEG pays for a config it never uses. hdrinput.cpp and ddsinput.cpp do the same. For a short-lived process - iinfo, a thumbnailer, an asset scanner - that is most of the cost of reading a header.

    On the narrower change proposed here: I think it already covers both of those. For JPEG the value being set is srgb_rec709_scene, so the first condition short-circuits. For HDR there is no Exif:ColorSpace on the spec at all - and in JpgInput::open() there wouldn't be one at that point either, since decode_exif() runs after the set_colorspace() call.

    @zachlewis - do you want that intermediate change kept separate from your PR? If it's useful standalone I'm happy to put it up. Either way, I can run my pinned 2.5.16.0 / 3.0.0.3 / 3.1.14.0 benchmark against your branch once it's open.

  9. lgritz commented on Sep 24, 2026

    @lgritz
    Collaborator

    readers should [...] just persist metadata as-is, to whatever degree we can, and leave the reconciliation of oiio:ColorSpace from the various known "facts" collected by the reader to a downstream ColorConfig-aware process.

    Agreed, that makes sense.

    To be perfectly honest, I'm planning on proposing that readers not directly populate oiio:ColorSpace at all, and, instead, populate the colorInteropID attribute; which would leave room for the actual reconciliation of oiio:ColorSpace to include the OCIO Config's FileRules as part of the deferred resolution process (instead of having readers always presuming that file metadata and/or format defaults always takes precedence over file naming). Just something to think about.

    Makes sense, I see.

    Yes, absolutely... it's just not clear to me how we would empirically assess the validity of potentially contradictory color metadata attributes in the spec without an actual OCIO color space to introspect and compare against (without resorting to hand-rolled heuristics that only cover a handful of real-world cases).

    I'm not totally against this, but I'm struggling to understand what you have in mind.

    Oh, I'm talking about obvious things like

    // BAD -- always constructs a ColorConfig
    if (!equivalent(myspace, "rec_709_scene"))
        spec.erase_attribute("thing that contradicts");
    
    // GOOD -- only constructs ColorConfig if truly needed
    if (myspace != "rec_709_scene"
           && spec.has_attribute("thing that contradicts")
           && !equivalent(myspace, "rec_709_scene"))
        spec.erase_attribute("thing that contradicts");
    

    There's no need to erase the attribute if it doesn't exist, and if myspace is exactly equal to "rec_709_scene", there is no need to call equivalent(), so the structure of the if && && prevents the expensive call in most cases.

    Maybe that's what you meant by "hand-rolled heuristics", but IMHO, as long as OCIO ColorConfig has a noticeable speed penalty to construct, even of a built-in config (which one might naively imagine to be free because it's the same on every run?), it's worth a little bit of refactoring to do it lazily if there are common cases where the cost is not necessary.

  10. zachlewis commented on Sep 25, 2026

    @zachlewis
    Collaborator

    Ah, I see your point...! yeah, you know what, that makes sense to me. In fact, I'm already sort of doing some of that, hardcoding chromaticities for the CIF color spaces, and I believe we've got something similar going on with CICP... maybe the thing to do is have set_colorspace "short-circuit" any comparisons with other metadata entirely for the set of published CIF color spaces, and just immediately set the corresponding chromaticities and "oiio:Gamma" attributes correctly in place, per a hardcoded table, no config needed. This sounds pretty reasonable to me!

    @bartosz216 -- I may be mistaken -- and correct me if I am -- but I think it's not quite safe yet to directly set oiio:ColorSpace to arbitrary colorInteropID values, at least without causing some regressions in some cases. Or, I should say, I think it's safe to directly set oiio:ColorSpace only if the value we're setting is "srgb_rec709_scene", "srgb_rec709_display", "lin_rec709_scene", "lin_ap1_scene", or "scene_linear" (and, currently, a handful of aliases for the aforementioned that we're trying to deprecate). These strings "get a pass", because, currently, the ColorConfig initialization phase attempts to identify definitional matches for those color spaces in the given config ahead of time. So, in areas you've identified where, e.g., the png reader is setting oiio:ColorSpace directly to "srgb_rec709_scene", it only gets away with doing so because [color-aware] IBAs always ColorConfig::resolve color space values; and that particular function encounters that particular string, it returns the name of the sRGB color space that was found during the ColorConfig instantiation. So setting "srgb_rec709_scene" is kind of invoking a promise that the name of the actual corresponding color space in the ColorConfig used by the IBA will be swapped in at the right moment, if it possibly can be; whereas, currently, I believe (and may be mistaken), ColorConfig::set_colorspace attempts to see if a known alias from OCIO-2.3 and OCIO-2.4's builtin configs happens to exist in the config for the published set of colorInteropIDs. It's not a super robust solution, but it's what we currently have in place, and it works well enough for cases where users are not providing custom OCIO configs.

    ANYWAY, long story short, my plan involves totally revamping the ColorConfig initialization / matching heuristics bit, which would make it safe to assign config-agnostic colorInteropIDs directly to oiio:ColorSpace (or, preferably, to the colorInteropID attribute instead), and would automatically defer the actual resolution until after a ColorConfig has been initialized (outside of the reader)... stay tuned!

  11. added 3 commits that reference this issue on Sep 29, 2026
    331731a
    4ec2209
    dc8762e
  12. bartosz216 commented on Sep 30, 2026

    @bartosz216
    Author

    You're right, thanks for spelling that out. I was reading pnginput.cpp setting
    oiio:ColorSpace directly as evidence that the cheap path is generally available,
    and it isn't - it works there because srgb_rec709_scene is one of the names
    ColorConfig resolves definitionally. Useful to know the guarantee is that narrow.

    For what it's worth, the three readers I flagged all land inside that safe set:
    jpeginput.cpp sets srgb_rec709_scene, hdrinput.cpp and ddsinput.cpp set
    lin_rec709_scene - which lines up with #5507 short-circuiting on the literal
    srgb_rec709_scene.

    Since all the measurements so far are on Apple silicon, I'll build #5507 against
    my pinned vcpkg tree and post before/after numbers for JPEG and EXR on Linux
    x86-64, gcc 13.3, OCIO 2.5.2. Anything in particular worth covering while I'm
    there?

  13. zachlewis commented on Sep 30, 2026

    @zachlewis
    Collaborator
  14. brechtvl commented on Oct 1, 2026

    @brechtvl
    Contributor

    For context, there is much room for optimization in OpenColorIO config loading as well: AcademySoftwareFoundation/OpenColorIO/pull/2357.

  15. added 3 commits that reference this issue on Oct 1, 2026
    4cb5211
    c758e0e
    ab1fcae
  16. bartosz216 commented on Oct 6, 2026

    @bartosz216
    Author

    Following up on the numbers I promised, now post-merge: Linux x86-64 (WSL2),
    gcc 13.3, OCIO 2.5.2, release builds of OIIO from the same vcpkg tree.
    First ImageInput::open() + spec() in a fresh process; 10 interleaved
    before/after runs, first pair discarded, median (range), ms:

    before (merge-base 5d186c29e) after (main f05656438)
    JPEG 21.267 (20.976 - 21.710) 0.671 (0.625 - 0.702)
    EXR, untagged (control) 0.837 (0.790 - 0.961) 0.839 (0.781 - 0.929)

    With OPENIMAGEIO_LOG_TIMES=2, ColorConfig::init no longer appears at all for
    a plain JPEG open. It still does for oiiotool --colorconvert, where it averages
    21.46 ms per init - close to the ~20.6 ms that disappeared from the JPEG open.

    Incidentally, the oiiotool run logs ColorConfig::init twice (43 ms total) -
    two config loads in one process, if I'm reading the counter right - which may be
    relevant to the shared-config change mentioned in #4629.

    Thanks both - this was a pleasure to follow.

  17. zachlewis commented on Oct 6, 2026

    @zachlewis
    Collaborator
  18. added a commit that references this issue on Oct 7, 2026
    f056564
  19. added 3 commits that reference this issue on Oct 9, 2026
    8123778
    fa5adea
    a542fd3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions