Skip to content

Improve performance#54

Open
baso53 wants to merge 1 commit intosyoyo:releasefrom
baso53:improve-performance
Open

Improve performance#54
baso53 wants to merge 1 commit intosyoyo:releasefrom
baso53:improve-performance

Conversation

@baso53
Copy link
Copy Markdown

@baso53 baso53 commented Sep 18, 2025

Use vectorbuf for storing internal data. This gives much better performance than ostringstream.

Also, added a method for writing a DNG directly from DNGImage.

@syoyo
Copy link
Copy Markdown
Owner

syoyo commented Sep 18, 2025

Please provide performance results

@baso53
Copy link
Copy Markdown
Author

baso53 commented Sep 18, 2025

Here are the results:

Old code:
Screenshot 2025-09-18 at 16 59 56

New code:
Screenshot 2025-09-18 at 16 58 59

The code for the tests is here, I just iterated 1000 times to generate a DNG.

release...baso53:tinydng:improve-performance-tests

Note that this is compiled with clang++ and -O3.

For some reason, without optimizations, the old code is faster, but IMO this is not important.

@syoyo syoyo requested a review from Copilot September 18, 2025 15:07
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves DNG writing performance by replacing the internal std::ostringstream with a custom vectorbuf class that uses std::vector<char> for more efficient data storage. It also adds a new method WriteDNGToStream for directly writing DNG files from raw image data.

  • Replace std::ostringstream with custom vectorbuf implementation for better performance
  • Update function signatures from std::ostringstream* to std::ostream* for consistency
  • Add new WriteDNGToStream method for direct DNG file writing

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

buffer.push_back(char(c));
// reset the put pointers to cover the new end
setp(buffer.data(), buffer.data() + buffer.size());
pbump(int(buffer.size())); // position at end
Copy link

Copilot AI Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting buffer.size() to int can cause overflow for large buffers. Use static_cast<std::streamsize> or check for overflow before casting.

Suggested change
pbump(int(buffer.size())); // position at end
pbump(static_cast<std::streamsize>(buffer.size())); // position at end

Copilot uses AI. Check for mistakes.
buffer.insert(buffer.end(), s, s + n);
// reset the put pointers
setp(buffer.data(), buffer.data() + buffer.size());
pbump(int(oldsz + n));
Copy link

Copilot AI Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting oldsz + n to int can cause overflow for large buffer operations. Use static_cast<std::streamsize> or validate the value fits in an int.

Copilot uses AI. Check for mistakes.

char* base = buffer.data();
setp(base, base + buffer.size());
pbump(int(newpos));
Copy link

Copilot AI Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting newpos to int can cause overflow for large seek positions. Use static_cast<std::streamsize> or validate the value fits in an int.

Copilot uses AI. Check for mistakes.
/// Return error string to `err` when Write() returns false.
/// Returns true upon success.
///
/// @param[in] data : same as in SetImageData
Copy link

Copilot AI Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove trailing whitespace after 'SetImageData'.

Suggested change
/// @param[in] data : same as in SetImageData
/// @param[in] data : same as in SetImageData

Copilot uses AI. Check for mistakes.
Comment on lines +2582 to +2583
if (bits_per_samples_.empty())
{ err_ += "BitsPerSample is not set\n"; return false; }
Copy link

Copilot AI Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The opening brace should be on the same line as the if statement for consistency with project style.

Suggested change
if (bits_per_samples_.empty())
{ err_ += "BitsPerSample is not set\n"; return false; }
if (bits_per_samples_.empty()) { err_ += "BitsPerSample is not set\n"; return false; }

Copilot uses AI. Check for mistakes.
@syoyo
Copy link
Copy Markdown
Owner

syoyo commented Sep 18, 2025

@baso53 Thank you! Numbers are nice. Please answer some comments(and do updates) from Copilot review, then PR will be merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants