Skip to content

fix: track source pointers in layer/state duplication#381

Merged
spe-ciellt merged 1 commit intogerbv:developfrom
SourceParts:fix/image-copy-layer-state-comparison
Mar 14, 2026
Merged

fix: track source pointers in layer/state duplication#381
spe-ciellt merged 1 commit intogerbv:developfrom
SourceParts:fix/image-copy-layer-state-comparison

Conversation

@rampageservices
Copy link
Copy Markdown
Contributor

Summary

Fixes a comparison bug in gerbv_image_copy_all_nets() that caused O(n) redundant layer and state allocations during image merging/export.

The existing code compared currentNet->layer != lastLayer, but lastLayer pointed to a duplicated struct — it could never equal a source pointer. Every net therefore created a new duplicate layer and state, even when consecutive nets shared the same source layer/state.

  • Track source pointers (srcLayer, srcState) separately from duplicated pointers
  • Compare against source pointers to correctly detect shared layers/states
  • Avoids unnecessary gerbv_image_duplicate_layer() / gerbv_image_duplicate_state() calls

This complements PR #379 (which added ->next = NULL initialization to the duplicate functions to prevent use-after-free on export).

Attribution

Extracted from #163 (@meantaipan).

Test plan

  • cmake --preset linux-gnu-gcc && cmake --build build — clean build
  • ctest -C Debug — 94/105 pass (11 pre-existing mismatches, unchanged from develop)
  • No rendering changes (this fix reduces memory waste, not visual output)

The layer/state duplication logic compared currentNet->layer against
lastLayer, but lastLayer is a *duplicated* pointer that will never
equal a source pointer. This caused every net to create a redundant
duplicate layer and state, wasting O(n) memory.

Track source pointers (srcLayer, srcState) separately and compare
against those to correctly detect when consecutive nets share the
same layer or state, avoiding unnecessary duplication.

Extracted from gerbv#163.
@spe-ciellt spe-ciellt merged commit e91bcfe into gerbv:develop Mar 14, 2026
3 checks passed
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