Skip to content

Add multi-variable read optimizer#641

Open
gijzelaerr wants to merge 2 commits intomasterfrom
feature/multi-read-optimizer
Open

Add multi-variable read optimizer#641
gijzelaerr wants to merge 2 commits intomasterfrom
feature/multi-read-optimizer

Conversation

@gijzelaerr
Copy link
Owner

Summary

  • Introduces a read optimization pipeline (snap7/optimizer.py) that merges scattered read_multi_vars() requests into contiguous blocks and packs them into minimal PDU-sized S7 exchanges
  • Adds multi-item READ_AREA PDU support in both protocol layer and server, so multiple address specs are sent/responded in a single round-trip
  • read_multi_vars() with 2+ dict items now automatically optimizes: adjacent reads are merged (configurable gap tolerance via client.multi_read_max_gap), and the optimization plan is cached for repeated layouts

Test plan

  • 18 unit tests for optimizer pipeline (sort, merge, packetize, extract)
  • 5 integration tests against the built-in server (basic, cross-DB, single-item, empty, many-items)
  • Full existing test suite passes (222 passed, 1 skipped)
  • mypy strict mode clean
  • ruff check and format clean

🤖 Generated with Claude Code

gijzelaerr and others added 2 commits March 20, 2026 12:47
Introduces a read optimization pipeline that merges scattered read requests
into contiguous blocks and packs them into minimal PDU exchanges, reducing
round-trips when reading multiple variables.

- snap7/optimizer.py: Pure-logic optimization with sort, merge, packetize,
  and extract_results functions
- snap7/s7protocol.py: build_multi_read_request and extract_multi_read_data
  methods for multi-item S7 READ_AREA PDUs
- snap7/server/__init__.py: Server-side multi-item read support
- snap7/client.py: read_multi_vars now uses optimizer for 2+ dict items,
  with plan caching for repeated layouts
- tests/test_optimizer.py: 23 tests covering unit and integration scenarios

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…sues

- Fix cache mutation bug: deep-copy cached ReadPackets before assigning
  buffers so repeated calls don't corrupt cached state
- Remove dead _map_area_int method
- Replace per-call set comprehension with module-level _VALID_AREA_VALUES
  frozenset for Area validation
- Fix O(n^2) byte concatenation in build_multi_read_request using list
  and b"".join()
- Clear optimizer cache on disconnect
- Add use_optimizer attribute (default True) to allow opting out

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gijzelaerr gijzelaerr force-pushed the feature/multi-read-optimizer branch from 1895d86 to 0f3790c Compare March 20, 2026 10:47
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