Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 29, 2025

The get_root(path) method fetches the entire configuration tree from the backend then parses client-side to find the requested element. For large configurations, this wastes bandwidth and memory.

Changes

  • rpc_config_get(): Added optional xpath parameter (default /) to filter data at the NETCONF backend
  • path_to_xpath(): New helper converts simplified path notation to XPath (handles 0-to-1-based index conversion, quote normalization)
  • get_root(): When path is provided, converts to XPath and requests only relevant subtree from backend

Example

Before:

# Fetches and parses entire configuration tree
root = clixon.get_root()
device = get_path(root, "devices/device[0]")

After (optimized):

# Backend returns only devices/device[1] subtree
device = clixon.get_root("devices/device[0]")

Backward compatible - all existing code continues to work unchanged.

Original prompt

Problem

The get_root(path) method in clixon/clixon.py currently:

  1. Fetches the entire configuration tree from the backend using rpc_config_get() with XPath filter / (all data)
  2. Then uses client-side parsing with get_path() to locate the specific element

This approach is inefficient as it:

  • Transfers large amounts of unnecessary data over the network
  • Requires parsing the entire configuration tree in memory
  • Causes unnecessary overhead when only a small subset of data is needed

Solution

Optimize the backend data retrieval by:

  1. Modify rpc_config_get() in clixon/netconf.py:

    • Add an optional xpath parameter (default to / for backward compatibility)
    • Use the provided xpath in the XPath filter instead of always using /
  2. Update get_root() in clixon/clixon.py:

    • When a path parameter is provided, convert the path to an XPath expression
    • Pass the XPath directly to rpc_config_get() instead of requesting all data
    • Remove the get_path() call since the backend now returns only the requested element
    • Keep the existing behavior when no path is provided
  3. Update Tests:

    • Add tests for rpc_config_get() with custom XPath filters
    • Add tests for get_root() with various path formats

Benefits

  • Reduces network bandwidth usage
  • Decreases memory footprint by not parsing unnecessary data
  • Improves performance for large configuration trees
  • Maintains backward compatibility (default behavior unchanged)
  • Provides more efficient data retrieval for common use cases like get_root("devices/device[0]")

This pull request was created from Copilot chat.


💡 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 changed the title [WIP] Optimize data retrieval in get_root method Optimize get_root() to filter data at backend using XPath Dec 29, 2025
Copilot AI requested a review from krihal December 29, 2025 12:49
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