db icon indicating copy to clipboard operation
db copied to clipboard

Suggestion to speed up the casting of values

Open Tigrov opened this issue 2 years ago • 3 comments

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

Tigrov avatar Aug 07 '23 10:08 Tigrov

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.

vjik avatar Aug 09 '23 11:08 vjik

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 avatar Aug 09 '23 18:08 Tigrov

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

Tigrov avatar Aug 13 '23 08:08 Tigrov