Skip to content

Comments

perf: Optimize SWAP and ROT operations to O(1)#556

Draft
Jim8y wants to merge 6 commits intoneo-project:masterfrom
Jim8y:pr-555
Draft

perf: Optimize SWAP and ROT operations to O(1)#556
Jim8y wants to merge 6 commits intoneo-project:masterfrom
Jim8y:pr-555

Conversation

@Jim8y
Copy link
Contributor

@Jim8y Jim8y commented Jan 28, 2026

Summary

Optimize SWAP and ROT stack operations from O(n) to O(1) complexity.

Changes

  • Add EvaluationStack.Swap(int index1, int index2) method for direct element exchange
  • Update Swap_Execute to use new O(1) swap method
  • Update Rot_Execute to use two swap calls instead of Remove+Push

Performance

Stack Size Before After
2 ~370ns ~72ns
100 ~14μs ~108ns
500 ~70μs ~126ns

SWAP time is now constant regardless of stack depth.

Testing

All 136 unit tests pass.

Jim8y and others added 3 commits January 27, 2026 13:42
- Add slot bounds checks and stack-pop slot constructor
- Add optimization benchmarks for slot operations
- Add NewItems benchmarks
- Remove redundant null check for engine (caller responsibility)
- Combine pop and AddStackReference into single loop
- Remove unnecessary temporary variable
- Add EvaluationStack.Swap() method for in-place element swapping
- SWAP now uses direct array swap instead of Remove()+Push()
- ROT now uses two swaps instead of Remove()+Push()
- No reference counting changes needed (items stay on stack)

Performance improvement:
- SWAP: O(n) -> O(1), ~70-130ns constant time regardless of stack depth
- ROT: O(n) -> O(1), uses two O(1) swaps

All 136 unit tests pass.
@Jim8y Jim8y self-assigned this Jan 28, 2026
@Jim8y Jim8y marked this pull request as draft January 28, 2026 07:09
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.

1 participant