Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

performance: reduce_payload_size #46

Merged
merged 3 commits into from
Jan 1, 2025
Merged

performance: reduce_payload_size #46

merged 3 commits into from
Jan 1, 2025

Conversation

JarbasAl
Copy link
Member

@JarbasAl JarbasAl commented Jan 1, 2025

change encoding of encrypted payloads to reduce message size

Summary by CodeRabbit

  • Improvements

    • Enhanced type annotations for utility functions
    • Improved error handling for encryption and decryption methods
    • Added support for multiple encoding formats (base64 and hex)
  • Bug Fixes

    • Updated decryption logic to handle different encoding styles
    • Improved payload conversion and compression methods

change encoding of encrypted payloads to reduce message size
change encoding of encrypted payloads to reduce message size
change encoding of encrypted payloads to reduce message size
Copy link
Contributor

coderabbitai bot commented Jan 1, 2025

Walkthrough

The pull request focuses on enhancing the hivemind_bus_client/util.py file by introducing comprehensive type annotations and improving error handling for message serialization, encryption, and decryption functions. The changes primarily involve adding type hints to function signatures, updating the encrypt_as_json and decrypt_from_json methods to handle different encoding styles, and implementing more robust decoding logic for payloads.

Changes

File Changes
hivemind_bus_client/util.py - Added type hints to multiple utility functions
- Updated encrypt_as_json with optional b64 parameter
- Enhanced decrypt_from_json to handle hex and base64 encoding
- Improved error handling in decryption methods

Poem

🐰 Hop, hop, through the code we go,
Type hints dancing, errors laid low,
Encryption's magic, now more clear,
Bytes and strings, no more to fear!
A rabbit's refactor, precise and bright 🔒


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (5)
hivemind_bus_client/util.py (5)

105-118: New 'b64' parameter and fallback approach.
Allowing the caller to toggle between hex and base64 is good for backwards compatibility. Document the plan to default b64=True in the next major release.


129-129: Spare blank line.
Consider removing if not needed, to keep readability consistent.

- 
+

133-137: Streamline the assignment of decoder using a ternary operator.
This simplifies the control flow and is suggested by static analysis.

-def decode(b64=False):
-    if b64:
-        decoder = pybase64.b64decode
-    else:
-        decoder = unhexlify
+def decode(b64=False):
+    decoder = pybase64.b64decode if b64 else unhexlify
🧰 Tools
🪛 Ruff (0.8.2)

133-136: Use ternary operator decoder = pybase64.b64decode if b64 else unhexlify instead of if-else-block

Replace if-else-block with decoder = pybase64.b64decode if b64 else unhexlify

(SIM108)


222-222: Minor spacing.
No functional impact; keep or remove for clarity.


224-237: Informative main block for local testing.
Demonstrates how to use encrypt_as_json and decrypt_from_json with both b64 and hex encoding. Consider moving these tests into formal unit tests.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 252b192 and e1dc18c.

📒 Files selected for processing (1)
  • hivemind_bus_client/util.py (10 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
hivemind_bus_client/util.py

133-136: Use ternary operator decoder = pybase64.b64decode if b64 else unhexlify instead of if-else-block

Replace if-else-block with decoder = pybase64.b64decode if b64 else unhexlify

(SIM108)

🔇 Additional comments (19)
hivemind_bus_client/util.py (19)

3-4: Great addition of encoding imports.
These imports (hexlify, unhexlify, Union, and Dict) will facilitate encoding/decoding functionalities and strongly typed signatures.


6-6: Adoption of pybase64 noted.
This library provides a performance benefit for base64 operations compared to Python's built-in base64.


13-13: Clearer function signature with type hints.
Using Union[HiveMessage, Message, Dict] -> str helps maintain clarity about expected inputs/outputs.


27-27: Type annotation increases clarity.
Defining payload: Union[HiveMessage, Message, str] -> Dict ensures the function's return type is explicit and consistent.


58-58: Improved function signature.
Accepts varied message formats (HiveMessage, Message, str, Dict) for seamless handling, returning a normalized dictionary.


71-71: Conversion to a HiveMessage with type hints.
Ensures consistent transformation to a HiveMessage object, reducing runtime type errors.


85-85: Ensuring a Mycroft Message is returned.
Accepting Union[HiveMessage, str, Dict] and returning a Message clarifies the pipeline from various payload formats.


124-124: Expanded type hints for decrypt function.
Accepting Union[str, bytes] is more flexible and clarifies usage.


130-132: Helpful comment clarifying new vs. old encoding style.
Documents hex vs. base64 expectations.


138-145: Good handling of ciphertext, tag, and nonce.
Proper decoding from user-provided values can avoid mismatched or truncated data.


146-146: Potential edge cases in uppercase detection.
any(a.isupper() for a in str(data)) might misinterpret strings with uppercase JSON keys. If that's truly your detection method, consider clarifying or using metadata.


147-157: Robust fallback approach for decoding.
Attempting base64 decoding if the initial attempt fails can help older clients with mismatched encoding. Good user experience.


160-160: Enhanced type annotations for encrypt_bin.
Strengthens clarity by allowing both str or bytes.


171-171: Consistent key trimming logic for decrypt_bin.
Matches the approach used in encrypt_bin.


186-186: compress_payload with typed input.
Ensuring a str or bytes is compressed properly improves consistency.


195-195: decompress_payload now returns bytes.
Clearer function contract helps manage subsequent transformations.


197-202: Dynamic base64 vs. hex decoding.
Automatically inferring the encoding from uppercase presence is practical, but make sure it doesn't conflict with normal uppercase in hex strings.


206-206: cast2bytes usage clarified.
Correctly converts dictionaries and strings, with optional compression.


217-217: bytes2str with optional decompression.
Ensures consistent round-trip with cast2bytes.

@JarbasAl JarbasAl merged commit 6ef1f3f into dev Jan 1, 2025
3 checks passed
@JarbasAl JarbasAl deleted the perf/reduce_payload_size branch January 8, 2025 21:04
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