Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

implement ser_json_temporal configuration option#12068

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ollz272 wants to merge1 commit intopydantic:main
base:main
Choose a base branch
Loading
fromollz272:implement_ser_json

Conversation

ollz272
Copy link
Contributor

Change Summary

Following work on the pydantic-core branch, this PR implements the config hooks to allowser_json_temporal to be passed in.

Related issue number

Fixes#10454
Fixes#7940

Performance improvements

The main reason we wanted this was to improve performance when we want to serialize a datetime as a millisecond timestamp. Below is a small script using this branch:

fromtypingimportAnnotatedimportpendulumimportdatetimeasdtfrompydanticimportBaseModel,PlainSerializerimporttqdmimporttimeRANGE=10dts= [dtfordtinpendulum.interval(pendulum.datetime(2023,1,1),pendulum.datetime(2024,1,1)).range("seconds",30)]T_SerializedDatetime=Annotated[dt.datetime,PlainSerializer(lambdax:x.timestamp()*1000,return_type=float,when_used="json",    ),]classMyModelOld(BaseModel):"""A model with a datetime field."""dts:list[T_SerializedDatetime]classMyModelNew(BaseModel):"""A model with a datetime field."""dts:list[dt.datetime]model_config= {"ser_json_temporal":"milliseconds"    }instance_old=MyModelOld(dts=dts)instance_new=MyModelNew(dts=dts)t=time.time()for_intqdm.tqdm(range(RANGE)):instance_old.model_dump_json()print(time.time()-t)t=time.time()for_intqdm.tqdm(range(RANGE)):instance_new.model_dump_json()print(time.time()-t)

I've run this over a few different number of dts to try gauge performance. A note is that im not 100% sure how this is being built for me when im installing, i doubt its using PGO and not sure if its the dev or prod profile, so i'd guess performance will improve from these results.

For the second conversion:

num_datetimesnum_iterationsold_avg_speed(s)new_avg_speed (s)improvment (%)
3662400000.00027070.000270824.5%
8761100000.0062180.00500119.5%
5256011660.37950.292822.8%
10512011660.76680.560426.9%

and for the millisecond conversion:

num_datetimesnum_iterationsold_avg_speed(s)new_avg_speed (s)improvment (%)
366100000.00027230.000210022.8%
8761100000.0066590.00484927.2%
5256011660.40290.290028.0%
10512011660.79330.575527.4%

So for us, this is a great improvement, esspecially for when we need to return large timeseries.

NOTE: currently based against a branch of pydantic-core, because theres no pydantic core release yet.

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • [] Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review,please add a comment including the phrase "please review" to assign reviewers

@ollz272ollz272 changed the titleimplement ser_json_temporal configuration opetionimplement ser_json_temporal configuration optionJul 17, 2025
@github-actionsgithub-actionsbot added the relnotes-fixUsed for bugfixes. labelJul 17, 2025
@codspeed-hqCodSpeed HQ
Copy link

CodSpeed Performance Report

Merging#12068 willnot alter performance

Comparingollz272:implement_ser_json (6c673da) withmain (d156ba0)

Summary

✅ 46 untouched benchmarks

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
relnotes-fixUsed for bugfixes.
Projects
None yet
Milestone
No milestone
1 participant
@ollz272

[8]ページ先頭

©2009-2025 Movatter.jp