Skip to content

Conversation

Copy link

Copilot AI commented Nov 12, 2025

Identified and fixed performance bottlenecks and platform compatibility issues in NIF file parsing.

Build Fixes

  • Cross-platform NIFORGE_API macro (added GCC __attribute__((visibility)) for non-Windows)
  • Removed #pragma once from .cpp file
  • Added missing <cmath>, <cstring> includes

Performance Optimizations

String handling - Eliminated allocations in hot paths:

// Before: creates temporary string on every block
std::string blockType = getReadableText(header.blockTypes[...]);
auto it = factories.find(blockType);

// After: zero-copy view
std::string_view blockType = getReadableText(header.blockTypes[...]);
auto it = factories.find(blockType);  // map now uses string_view keys

Vector operations:

  • Pre-allocated blocks.reserve(header.numBlocks) before parsing loop
  • emplace_back() instead of push_back() for 10+ constructor calls (NiNode, NiMesh, NiHeader, etc.)
  • Fixed bug: NiHeader used capacity() in loop condition instead of actual count

Type correctness:

  • Loop indices now use proper unsigned types (uint16_t, uint32_t) matching NIF spec
  • Replaced dynamic_pointer_cast with direct dynamic_cast in parseDataStreams

Move semantics - Added noexcept to move ops (NiFile, NiDataStream) for container optimization

Const correctness - getBlocksOfType() and getRootNodes() now const-qualified

Example Impact

Factory lookup no longer allocates/copies strings for each of the ~100 blocks in a typical NIF file. Vector reservations eliminate 3-4 reallocations per parsing pass.

Original prompt

Identify and suggest improvements to slow or inefficient code


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 5 commits November 12, 2025 10:58
Co-authored-by: Phill030 <50775241+Phill030@users.noreply.github.com>
Co-authored-by: Phill030 <50775241+Phill030@users.noreply.github.com>
Co-authored-by: Phill030 <50775241+Phill030@users.noreply.github.com>
…sues

Co-authored-by: Phill030 <50775241+Phill030@users.noreply.github.com>
Co-authored-by: Phill030 <50775241+Phill030@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements to slow code Optimize NIF parsing: reduce allocations, fix cross-platform builds Nov 12, 2025
Copilot AI requested a review from Phill030 November 12, 2025 11:12
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.

2 participants