Switch to orjson, fallback to stdlib json #6387

Closed
japandotorg wants to merge 9 commits from feat/orjson into V3/develop
japandotorg commented 2024-06-09 07:32:40 +02:00 (Migrated from github.com)

Description of the changes

To optimize performance we should utilize orjson, which is already included in requirements/base.in, but unused in Red.

I have been using a modified version of Red with orjson (replacing stdlib json, no fallback) without any problem and have noticed performance gains so far, I primarily used orjson.dumps and orjson.loads, while other methods like json.dump, json.load, aiohttp.ClientSession(json_serialize=...) remained unchanged using stdlib json, also never in audio before.

While I don't think a full switch to orjson is necessary, but I think we should give orjson a chance.

Have the changes in this PR been tested?

No

### Description of the changes To optimize performance we should utilize orjson, which is already included in [`requirements/base.in`](https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/requirements/base.in#L9), but unused in Red. I have been using a modified version of Red with orjson (replacing stdlib json, no fallback) without any problem and have noticed performance gains so far, I primarily used `orjson.dumps` and `orjson.loads`, while other methods like `json.dump`, `json.load`, `aiohttp.ClientSession(json_serialize=...)` remained unchanged using stdlib json, also never in audio before. While I don't think a full switch to orjson is necessary, but I think we should give orjson a chance. ### Have the changes in this PR been tested? No
palmtree5 (Migrated from github.com) reviewed 2024-06-09 07:32:40 +02:00
Jackenmen (Migrated from github.com) reviewed 2024-06-09 07:32:40 +02:00
aikaterna (Migrated from github.com) reviewed 2024-06-09 07:32:40 +02:00
PredaaA (Migrated from github.com) reviewed 2024-06-09 07:32:40 +02:00
Jackenmen commented 2024-06-09 10:02:24 +02:00 (Migrated from github.com)

It raises JSONEncodeError on an integer that exceeds 64 bits by default or, with OPT_STRICT_INTEGER, 53 bits.
Source: https://github.com/ijl/orjson?tab=readme-ov-file#serialize

This makes it unsuitable for Config. Additionally, the support for dataclasses and other types would make it harder to migrate back.

> It raises `JSONEncodeError` on an integer that exceeds 64 bits by default or, with `OPT_STRICT_INTEGER`, 53 bits. > **Source:** https://github.com/ijl/orjson?tab=readme-ov-file#serialize This makes it unsuitable for Config. Additionally, the support for dataclasses and other types would make it harder to migrate back.
japandotorg commented 2024-06-09 18:48:29 +02:00 (Migrated from github.com)

yikes, well that's a bummer.

Is there anything that can be done? or maybe an alternative?

yikes, well that's a bummer. Is there anything that can be done? or maybe an alternative?
Jackenmen commented 2024-07-10 14:50:19 +02:00 (Migrated from github.com)

Nothing can really be done about this in the current Config API. If there's ever a new one, probably, though I don't know if a new one would even have a JSON driver in the first place.

You could probably sprinkle some orjson usage in other places in Core, just not anything to do with Config. I don't know if it will really give any significant gains though, I don't think we use JSON outside of Config much.

Other than that, I glanced at the diff and I just want to say that there's no point in the _json module - orjson is not an optional dependency of Red.

Nothing can really be done about this in the current Config API. If there's ever a new one, probably, though I don't know if a new one would even have a JSON driver in the first place. You could probably sprinkle some orjson usage in other places in Core, just not anything to do with Config. I don't know if it will really give any significant gains though, I don't think we use JSON outside of Config much. Other than that, I glanced at the diff and I just want to say that there's no point in the `_json` module - `orjson` is not an optional dependency of Red.
japandotorg commented 2024-07-18 22:05:25 +02:00 (Migrated from github.com)

ah I see

You could probably sprinkle some orjson usage in other places in Core, just not anything to do with Config. I don't know if it will
really give any significant gains though, I don't think we use JSON outside of Config much.

for now I'll just look into this for this PR, if the org thinks that this is really unnecessary than the PR can be closed

It raises JSONEncodeError on an integer that exceeds 64 bits by default or, with OPT_STRICT_INTEGER, 53 bits.

perhaps can we use a fallback to json incase this happens?

ah I see > You could probably sprinkle some orjson usage in other places in Core, just not anything to do with Config. I don't know if it will really give any significant gains though, I don't think we use JSON outside of Config much. for now I'll just look into this for this PR, if the org thinks that this is really unnecessary than the PR can be closed > It raises JSONEncodeError on an integer that exceeds 64 bits by default or, with OPT_STRICT_INTEGER, 53 bits. perhaps can we use a fallback to `json` incase this happens?
Jackenmen commented 2024-07-19 14:37:08 +02:00 (Migrated from github.com)

perhaps can we use a fallback to json incase this happens?

That is an interesting idea though there are more cases where orjson differs in serialization and that could lead to introducing discrepancies between drivers (and even within the driver in case of the fallback) - for example it serializes UUID types while normal JSON would fail at that point.

> perhaps can we use a fallback to `json` incase this happens? That is an interesting idea though there are more cases where orjson differs in serialization and that could lead to introducing discrepancies between drivers (and even within the driver in case of the fallback) - for example it serializes UUID types while normal JSON would fail at that point.
Jackenmen commented 2026-05-20 13:00:02 +02:00 (Migrated from github.com)
Per https://github.com/Cog-Creators/Red-DiscordBot/pull/6387#issuecomment-2156377027, we cannot really use this, closing.

Pull request closed

Sign in to join this conversation.
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!6387
No description provided.