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

Suggestion to speed up the casting of values #737

Closed
Tigrov opened this issue Aug 7, 2023 · 3 comments
Closed

Suggestion to speed up the casting of values #737

Tigrov opened this issue Aug 7, 2023 · 3 comments
Assignees
Labels
status:ready for adoption Feel free to implement this issue. type:enhancement Enhancement
Milestone

Comments

@Tigrov
Copy link
Member

Tigrov commented Aug 7, 2023

Suggestion to predefine cast methods in a separate class (e.g. Typecast)
https://github.com/Tigrov/db-pgsql/blob/speedup_typecast_predefined/src/Typecast.php

Then initialize the predefined methods in ColumnSchema objects
https://github.com/Tigrov/db-pgsql/blob/speedup_typecast_predefined/src/Schema.php#L819

And use the predefined methods when casting values
https://github.com/Tigrov/db-pgsql/blob/speedup_typecast_predefined/src/ColumnSchema.php#L84
https://github.com/Tigrov/db-pgsql/blob/speedup_typecast_predefined/src/ColumnSchema.php#L131

This increase the speed of casting values by an average of 30-40%

More information and benchmarks
https://github.com/Tigrov/db-pgsql/tree/speedup_typecast_predefined

Update:

If phpTypecast() casts values retrieved from DB only, some checks can be removed, this will increase the speed by 10-20% more.

Q A
Version 1.1.0
PHP version any
Operating system any
@vjik
Copy link
Member

vjik commented Aug 9, 2023

I like idea clearly separate type casting "db → php" and "php → db".

May be split typecast class to two classes:

  • DbTypeCaster - converts a value from its PHP representation to a database-specific representation;
  • PhpTypeCaster - converts the input value to PHP type after retrieval from the database.

These classes should be placed in DB implementations packages and uses into ColumnSchema. May be in Yii DB create AbstractTypeCaster.


Also has idea create classes for each column types. In this case type casting should be implemented into this clasess.

@Tigrov
Copy link
Member Author

Tigrov commented Aug 9, 2023

Also has idea create classes for each column types. In this case type casting should be implemented into this clasess.

Also thought about this, looks better 👍

These classes should be placed in DB implementations packages and uses into ColumnSchema. May be in Yii DB create AbstractTypeCaster

👍

Sure, this is just an example.

@Tigrov
Copy link
Member Author

Tigrov commented Aug 13, 2023

Realized example of creating classes for each column type with optimization of phpTypecast() for values retrieved from DB only

Results for dbTypecast()

  • Current implementation 0.381μs
  • Predefined methods are 0.252μs
  • Column type classes are 0.142μs x2.5 faster

Results for phpTypecast()

  • Current implementation 0.449μs
  • Predefined methods are 0.279μs
  • Column type classes are 0.147μs x3 faster

More info https://github.com/Tigrov/db-pgsql/tree/speedup_typecast_columns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready for adoption Feel free to implement this issue. type:enhancement Enhancement
Projects
None yet
Development

No branches or pull requests

3 participants