Skip to content

CP-54826/CP-54827: optimize handling of the Pool object #6445

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

Merged
merged 9 commits into from
May 7, 2025

Conversation

edwintorok
Copy link
Contributor

This doesn't have feature flags, they are optimizations.

The pool object has gained a JSON field recently, which slowed it down significantly, because the " was getting escaped (so the fastpath that checks whether there are any escapable characters or not couldn't be triggerred).

The date field is also very slow to handle due to various backward compatibility format transformations. Add a direct parser for the format used in memory (it'd be good to skip all these serializing/deserializing, but that is a bigger change).

Handling the database requires several finally calls, which perform a lot of backtrace formatting, and some code keeps raising exceptions even on happy paths (e.g. Hashtbl.find). We need to fix those too, but we should also speed up finally.

Xapi_local_session was also raising exceptions all the time, replace this with Hashtbl.mem

@edwintorok edwintorok force-pushed the pr/pool-opt branch 3 times, most recently from 3a87c35 to ae60d68 Compare April 28, 2025 10:48
Copy link
Contributor

@last-genius last-genius left a comment

Choose a reason for hiding this comment

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

Would also be great to see some performance comparisons from before and after.

invalid_arg (Printf.sprintf "%s: %s" __FUNCTION__ x)
| Ok (t, tz, _) ->
{t; tz}
if String.length x > 5 && x.[4] <> '-' && x.[String.length x - 1] = 'Z' then
Copy link
Contributor

@last-genius last-genius Apr 28, 2025

Choose a reason for hiding this comment

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

It would be nice to have some comment explaining the particular reasons behind each condition here

@edwintorok
Copy link
Contributor Author

Would also be great to see some performance comparisons from before and after.

I can adapt some microbenchmarks that I have to measure these situations, I think the most impactful optimization is the 'sexpr' one but lets see.

Unused.
Avoids having to maintain the function when the code around it changes.

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
@edwintorok
Copy link
Contributor Author

Would also be great to see some performance comparisons from before and after.

I can adapt some microbenchmarks that I have to measure these situations, I think the most impactful optimization is the 'sexpr' one but lets see.

Added a benchmark, it shows an ~15% improvement, but only when considering all commits cumulatively, when measuring one-by-one there is no discernable change at 95% confidence level. Probably not benchmarking the same thing I've seen in the flamegraph though, will add a few more benchmarks.

@edwintorok
Copy link
Contributor Author

Added benchmarks for individual functions and we can more reliably measure improvements.
The overall benchmark for Db.Pool.get_all_records doesn't show a measurable improvement, there are probably other bottlenecks there, I'll see whether I have some commits that improve that.

x /tmp/all/1745917017.25/Date.of_iso8601.dat
+ /tmp/all/1745917172.71/Date.of_iso8601.dat
Difference at 95.0% confidence
        -1.33392e+06 +/- 382482
        -42.4814% +/- 8.87103%
        (Student's t, pooled s = 3.52946e+06)
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          +                                                                                                                                                  |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          +*                                                                                                                                                 |
|          +*                                                                                                                                                 |
|          **                                                                                                                                                 |
|          **                                                                                                                                                 |
|          **x                                                                                                                                                |
|          **x                                                                                                                                                |
|          **x                                                                                                                                                |
|          **x                                                                                                                                                |
|          **x                                                                                                                                                |
|          **x                                                                                                                                                |
|          ***                                                                                                                                                |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x+                                                                                                                                              |
|          ****+                                                                                                                                              |
|          ****+                                                                                                                                              |
|          ****+                                                                                                                                              |
|          ****+                                                                                                                                              |
|          ****+                                                                                                                                              |
|          ****+                                                                                                                                              |
|          ***** x                                                                                                                                            |
|          ***** x                                                                                                                                            |
|          ***** x                                                                                                                                            |
|          ***** x+                                                                                                                                           |
|          ***** x+                                                                                                                                           |
|          ***** x+                                                                                                                                           |
|          *****+x+                                                                                                                                           |
|          *****+x+                                                                                                                                           |
|          *****+x*                                                                                                                                           |
|          *****+x*   +                                                                                                                                       |
|         +*****+x* x +                                                                                                                                       |
|         +*****+x*+x +x                                                                                                                                      |
|         +******x*+x +x                                                                                                                                      |
|         +******x*+x +x                                                                                                                                      |
|         +******x*+x +x                                                                                                                                      |
|         +******x*+x +x  +                                                                                                                                   |
|         +******x*+x *x  +                                                                                                                                   |
|         +******x*+xx**x +                                                                                                                                   |
|         +******x*+xx**x + x                                                                                                                                 |
|         +******x*+xx**x +xxx +                                                                                                                              |
|         +********+*x**x +xx* +          x +                                                                                                                 |
|         +********+*x**x++*x* * xx       x +   x      x                                                                                                      |
|         +********+*x***+**x*+*+x*+   x  x +   x      x                                                                                                      |
|         +********+*****+******+x*+*++x+xx +  +xx + + * + +                     x                                                                            |
|         +**************+******+x******+** *+ *x* + * *x* + + x*  + +x + x x + +xx    x         +  +            x                               x          x |
|         ***************+*****************x******+* *x*x*x+x* ***x+x+* + * **+ +x* + *x+ x+x   x+  *x +x x x    x  x x      x   x        x    x xx       x x |
|         **********************************************x*x*********x**x* *****x+**+*x*****+**x*** +***+xxx x x xx xx xxx  xxx   x x xx  xx x xx xx  x x  x xx|
||_________________M________________A_________________________________|                                                                                       |
|  |___________M_________A____________________|                                                                                                               |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
    N           Min           Max        Median           Avg        Stddev
x 622         16863      18127835     1123564.5     3140007.3     4294436.6
+ 690          7043      11496089      533753.5     1806087.4       2657571
x /tmp/all/1745917017.25/Rpc.t -> pool_t.dat
+ /tmp/all/1745917172.71/Rpc.t -> pool_t.dat
No difference proven at 95.0% confidence
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
|             +                                                                                                                                               |
|             +                                                                                                                                               |
|             +                                                                                                                                               |
|             +                                                                                                                                               |
|             +                                                                                                                                               |
|             +                                                                                                                                               |
|             +                                                                                                                                               |
|             +                                                                                                                                               |
|           + +                                                                                                                                               |
|           + +                                                                                                                                               |
|           + +                                                                                                                                               |
|           + +                                                                                                                                               |
|           + +                                                                                                                                               |
|           + +                                                                                                                                               |
|       + *++ +                                                                                                                                               |
|       + *++ +                                                                                                                                               |
|       + *++ +  x                                                                                                                                            |
|       ++*++ +  x                                                                                                                                            |
|       ++*+*++x x                                                                                                                                            |
|       ++*+*++xxx                                                                                                                                            |
|       ++*+*++xxx                                                                                                                                            |
|       ++*+**+xxx                                                                                                                                            |
|       *+*****xxxx                                                                                                                                           |
|       *+*****x*xx                                                                                                                                           |
|       *********xx                                                                                                                                           |
|       ***********                                                                                                                                           |
|       ***********+                                                                                                                                          |
|       ***********+ x   +                                                                                                                                    |
|       ***********+ *x  +     +                                                                                                                              |
|       ***********++*x+ +     +  x                                                                                                                           |
|      +**************** +     +  x                                                                                                                           |
|      *****************x* x x +  x                                                                                                                           |
|      *****************x* * x +  x  +       +                                                                                                                |
|      *****************x*x*x* +x+*  *  x+   +  +                                               x                                                             |
|      *****************x***x**+***x * *x+x  ++ +  + +  +                         x             x                                                             |
|      *********************x**+****x* ***x x** +xx+ *xx+ * x *          +        x     + +x +  x   x       +                      x             +            |
|      *********************x**+************x**x*xx*+*xx+x*+*** x+*x++x+**x+*+ *  x  +x +x*x *+ x++ *  +   ++*x  x  +xx   +   *+   x       x    x+x  *  x+    |
|      *************************************x*************************x***x***** ** *+* *x**+*+xx*+ *x**x+x*+*x*** ++**+x*+*xx*+x **++** x+* *  x+x++*xx*+x+ *|
|  |___________________M________________A___________________________________|                                                                                 |
||__________________M________________A____________________________________|                                                                                   |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
    N           Min           Max        Median           Avg        Stddev
x 493         57653      49328823       5208953      10736130      12106684
+ 527         39579      49317198       4330964     9980742.9      11959405
x /tmp/all/1745917017.25/pool_t -> Rpc.t.dat
+ /tmp/all/1745917172.71/pool_t -> Rpc.t.dat
No difference proven at 95.0% confidence
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
|            +                                                                                                                                                |
|            +                                                                                                                                                |
|            +                                                                                                                                                |
|            +                                                                                                                                                |
|            +                                                                                                                                                |
|            +                                                                                                                                                |
|          x +                                                                                                                                                |
|          x +                                                                                                                                                |
|          x +                                                                                                                                                |
|          x +                                                                                                                                                |
|          * +                                                                                                                                                |
|          * +                                                                                                                                                |
|          * +                                                                                                                                                |
|          * +                                                                                                                                                |
|          * +*                                                                                                                                               |
|          * **                                                                                                                                               |
|          * **                                                                                                                                               |
|          *x**                                                                                                                                               |
|          *x**                                                                                                                                               |
|          *x**                                                                                                                                               |
|         x*x**                                                                                                                                               |
|         x*x**                                                                                                                                               |
|         x****                                                                                                                                               |
|         x****                                                                                                                                               |
|         *****                                                                                                                                               |
|         *****                                                                                                                                               |
|         *****                                                                                                                                               |
|         *****                                                                                                                                               |
|         *****                                                                                                                                               |
|         *****                                                                                                                                               |
|         *****  +                                                                                                                                            |
|         ***** x+                                                                                                                                            |
|         ***** x+                                                                                                                                            |
|         *****xx+                                                                                                                                            |
|         *****xx*                                                                                                                                            |
|         *****xx*+                                                                                                                                           |
|         ******x*+                                                                                                                                           |
|         ******x*+                                                                                                                                           |
|         ******x**                                                                                                                                           |
|         ******x**                                                                                                                                           |
|         **********                                                                                                                                          |
|         **********                                                                                                                                          |
|         **********+*                                                                                                                                        |
|         **********+*x  x                                                                                                                                    |
|        +************x x*   x                                                                                                                                |
|        *************x **+  x                                                                                                                                |
|        *************x+**++ x                                                                                                                                |
|        **************+***++x+x++ x                                                                                                                          |
|        ******************++x*x*+ x    x x                     x                                                                       +                     |
|        ******************+*****+x* +++**x  * *+  x  +  x      x                                                                       +                     |
|        *************************** *****x* * *+* *+x+ +x  +x  x  x * +    +   x  x +   +         x ++             x                   +    +                |
|        ***************************x*****x*******+**x*++* +**+** ** *x+* x **x+x +xx++ x** x   +  x++*    x + *    xx  +        +   x  +    +     +    +  +  |
|        ********************************************x*****+******** ******+***+*x+**** x**+***** xx*****  x+++* ******x**x*xx** * ***x++xx+ + x+ ++ ++ +  + +|
| |________________M______________A______________________________|                                                                                            |
||_________________M_________________A__________________________________|                                                                                     |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
    N           Min           Max        Median           Avg        Stddev
x 559         38736      35817415       2592007     6542898.3     8333432.4
+ 572         30038      39462144     2726835.5     7319895.8       9503851
x /tmp/all/1745917017.25/str_of_sexp_json.dat
+ /tmp/all/1745917172.71/str_of_sexp_json.dat
Difference at 95.0% confidence
        -1.04277e+06 +/- 367498
        -36.7617% +/- 10.076%
        (Student's t, pooled s = 3.38273e+06)
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          ++                                                                                                                                                 |
|          +*                                                                                                                                                 |
|          +*                                                                                                                                                 |
|          +*                                                                                                                                                 |
|          +*                                                                                                                                                 |
|          +*                                                                                                                                                 |
|          +*x                                                                                                                                                |
|          +*x                                                                                                                                                |
|          +*x                                                                                                                                                |
|          +*x                                                                                                                                                |
|          +*x                                                                                                                                                |
|          +*x                                                                                                                                                |
|          +*x                                                                                                                                                |
|          +**                                                                                                                                                |
|          +**                                                                                                                                                |
|          +**                                                                                                                                                |
|          +**                                                                                                                                                |
|          +**                                                                                                                                                |
|          +**                                                                                                                                                |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ***x                                                                                                                                               |
|          ****                                                                                                                                               |
|          ****                                                                                                                                               |
|          ****                                                                                                                                               |
|          ****x                                                                                                                                              |
|          ****x                                                                                                                                              |
|          *****x                                                                                                                                             |
|          *****x                                                                                                                                             |
|          ******                                                                                                                                             |
|          ******                                                                                                                                             |
|          ******                                                                                                                                             |
|          ******                                                                                                                                             |
|          ******x                                                                                                                                            |
|          ******x                                                                                                                                            |
|          ******x                                                                                                                                            |
|          ******x                                                                                                                                            |
|          ******x                                                                                                                                            |
|          *******                                                                                                                                            |
|          *******                                                                                                                                            |
|          ******* +x                                                                                                                                         |
|          ******* +x                                                                                                                                         |
|          ******* *x+x                                                                                                                                       |
|          *********x+x                                                                                                                                       |
|          ***********x +x                                                                                                                                    |
|          ************ +x+   x                                                                                                                               |
|          **************x+  +x                                                                                                                               |
|          **************x++ +x  x            x                                                                                                               |
|          **************x+*x+xx+x + + x  x   x                                                                                                               |
|          **************x+*x+xx+*x+x+ x xx + x   xx  x                    x                                                                                  |
|          ********************x+**+x**x+x* +xx   xx+ x+      +            x                  x                                                               |
|          *********************+**+x***+**++xx+  *** *+  * *x+   + +      xx+          x+    x*       +    +                x                         x      |
|          *********************+*******+**++**++ ***+*+xx*x**+ x**x**x * +x**   xx +x *x++   x*  + xx * x  +      xx        x  x x  x x       x     x x      |
|          *************************************+****+***x****++x*********+***+*+**********+**x*+++x** *+*xx*x++x+**x xxx xx x  xxxxxx x  x x  xx    xxx x  xx|
||________________M________________A__________________________________|                                                                                       |
|  |___________M__________A_______________________|                                                                                                           |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
    N           Min           Max        Median           Avg        Stddev
x 621          5220      16978018        879076     2836573.6     3979298.2
+ 684          3329      12084265      455498.5     1793800.1     2730567.4
x /tmp/all/1745917017.25/Db.Pool.get_all_records.dat
+ /tmp/all/1745917172.71/Db.Pool.get_all_records.dat
No difference proven at 95.0% confidence
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
|                            +                                                                                                                                |
|                            +                                                                                                                                |
|         +          +       +                                                                                                                                |
|    +    + +    x   +  x x  +x                                                                                                                               |
|    + +x++ +    x   +* * x  +x  x                                                                                                                            |
| +* + +x+* *+*+ *+ x+* * *+ *x  x          +                                                                                                                 |
| ** * *x**x*+*+ *+ *+*****+x*xx x          +       +                                                                                                         |
| **x*+*x**x***++** *+*****+x*xx x          +     x +                                                                                                         |
|+*********x*********+*****+***x** x++x+x  x+ x   x +  + +    x                                                                                               |
|********************+************+**+**x+*** x  +xx* x* +x  xx   *+   *x +  x      x      x                                 +     +                          |
|********************************************xx+ +***xx* *x++**x **+ x+*x +*+x  +x* **    *x++x*+     x xxxx *  *x    +++ x  +xx  x+  x  +  +   x    ++   +   |
|***********************************************+**********+********+x*** *****+******+x*+*x*+x*+x*+*+******+*****x++ ***xx*+*x* *x+x+*x*+xx*x++x*xx +*+ ** ++|
|      |________________________M______________A______________________________________|                                                                       |
|     |_______________________M_______________A_______________________________________|                                                                       |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
    N           Min           Max        Median           Avg        Stddev
x 378        141371      81403131      16612004      24304330      20719855
+ 394        133909      82970872      15396352      23950187      21331862
x /tmp/all/1745917017.25/local_session_hook.dat
+ /tmp/all/1745917172.71/local_session_hook.dat
Difference at 95.0% confidence
        -160446 +/- 50956.1
        -42.4683% +/- 9.70947%
        (Student's t, pooled s = 492820)
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      +                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *                                                                                                                                                      |
|      *x                                                                                                                                                     |
|      *x                                                                                                                                                     |
|      *x                                                                                                                                                     |
|      *x                                                                                                                                                     |
|      *x                                                                                                                                                     |
|      *x                                                                                                                                                     |
|      *x                                                                                                                                                     |
|      *x                                                                                                                                                     |
|      *x                                                                                                                                                     |
|      *x                                                                                                                                                     |
|      *x                                                                                                                                                     |
|      *x                                                                                                                                                     |
|      *x                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **                                                                                                                                                     |
|      **+                                                                                                                                                    |
|      **+                                                                                                                                                    |
|      **+                                                                                                                                                    |
|      **+                                                                                                                                                    |
|      **+                                                                                                                                                    |
|      **+                                                                                                                                                    |
|      **+                                                                                                                                                    |
|      **+                                                                                                                                                    |
|      **+                                                                                                                                                    |
|      **+                                                                                                                                                    |
|      **+                                                                                                                                                    |
|      **+                                                                                                                                                    |
|      **+                                                                                                                                                    |
|      **+                                                                                                                                                    |
|      ***                                                                                                                                                    |
|      ***                                                                                                                                                    |
|      ***                                                                                                                                                    |
|      ***                                                                                                                                                    |
|      ***                                                                                                                                                    |
|      ***                                                                                                                                                    |
|      ***x                                                                                                                                                   |
|      ***x                                                                                                                                                   |
|      ***x                                                                                                                                                   |
|      ***x                                                                                                                                                   |
|      ***x                                                                                                                                                   |
|      ***x                                                                                                                                                   |
|      ***x                                                                                                                                                   |
|      ***x                                                                                                                                                   |
|      ***x                                                                                                                                                   |
|      ***x                                                                                                                                                   |
|      ****x                                                                                                                                                  |
|      ****x                                                                                                                                                  |
|      *****                                                                                                                                                  |
|      *****                                                                                                                                                  |
|      *****                                                                                                                                                  |
|      *****x                                                                                                                                                 |
|      ******                                                                                                                                                 |
|      ******                                                                                                                                                 |
|      ******                                                                                                                                                 |
|      ****** x                                                                                                                                               |
|      ****** x                                                                                                                                               |
|      *******x                                                                                                                                               |
|      *******x x                                                                                                                                             |
|      *******x x                                                                                                                                             |
|      ******** *+                                                                                                                                            |
|      ******** *+                                                                                                                                            |
|      ******** ** +                                                                                                                                          |
|      ******** ** +  x                                                                                                                                       |
|      ********+**+*x x                                                                                                                                       |
|      ********+**+** x+++  +                                                                                                                                 |
|      ********+***** x+++ x+                                                                                                                                 |
|      ************** x+*+ x*x           +                                                                                                                    |
|      **************+x+**xx** +++       +             x           x                                                                                          |
|      ***************x+**xx**x***+x +  x+ xx          xx          x                                                                                          |
|      ***********************x***+**+x ***xxx x x   x xx  xx    x x                                                                                          |
|      ***************************+**********xxx x xxx xxx xx  xxx x                                                                                          |
|      **************************************xxx xxxxxxxxxxxx  xxx x x                                          +                                            x|
||_______M______A______________|                                                                                                                              |
|  |____M___A________|                                                                                                                                        |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
    N           Min           Max        Median           Avg        Stddev
x 695          3933       5940316         92424     377801.58     601781.05
+ 744          1824       4178883       50656.5     217355.57     362585.86
x /tmp/all/1745917017.25/sexpr_of_json_string.dat
+ /tmp/all/1745917172.71/sexpr_of_json_string.dat
Difference at 95.0% confidence
        -443701 +/- 423782
        -15.0326% +/- 13.2562%
        (Student's t, pooled s = 3.87758e+06)
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           +                                                                                                                                                 |
|           ++                                                                                                                                                |
|           ++                                                                                                                                                |
|           ++                                                                                                                                                |
|           ++                                                                                                                                                |
|           ++                                                                                                                                                |
|           ++                                                                                                                                                |
|           ++                                                                                                                                                |
|           ++                                                                                                                                                |
|           ++                                                                                                                                                |
|           ++                                                                                                                                                |
|           ++                                                                                                                                                |
|           ++                                                                                                                                                |
|           ++                                                                                                                                                |
|           ++                                                                                                                                                |
|           ++                                                                                                                                                |
|           ++x                                                                                                                                               |
|           *+x                                                                                                                                               |
|           *+x                                                                                                                                               |
|           *+x                                                                                                                                               |
|           **x                                                                                                                                               |
|           **x                                                                                                                                               |
|           **x                                                                                                                                               |
|           **x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +**x                                                                                                                                               |
|          +***                                                                                                                                               |
|          +***                                                                                                                                               |
|          +***                                                                                                                                               |
|          +***                                                                                                                                               |
|          +***                                                                                                                                               |
|          +***                                                                                                                                               |
|          +***                                                                                                                                               |
|          +***                                                                                                                                               |
|          +***                                                                                                                                               |
|          +***                                                                                                                                               |
|          +***                                                                                                                                               |
|          +***                                                                                                                                               |
|          ****                                                                                                                                               |
|          ****x                                                                                                                                              |
|          ****x                                                                                                                                              |
|          *****                                                                                                                                              |
|          *****                                                                                                                                              |
|          *****                                                                                                                                              |
|          *****                                                                                                                                              |
|          *****                                                                                                                                              |
|          *****x                                                                                                                                             |
|          *****xx                                                                                                                                            |
|          ******x                                                                                                                                            |
|          ******x                                                                                                                                            |
|          ******x                                                                                                                                            |
|          ******x                                                                                                                                            |
|          ******x x                                                                                                                                          |
|          ******* x                                                                                                                                          |
|          ******* x+   x                                                                                                                                     |
|          *******+*+   x                                                                                                                                     |
|          *******+*+  +x                                                                                                                                     |
|          *******+**  +x   x                                                                                                                                 |
|          *********** *x   x                                                                                                                                 |
|          *********** *x + x                                                                                                                                 |
|          *********** *x + x                                                                                                                                 |
|          ***********x** + x                                                                                                                                 |
|          ***********x** + *+                                                                                                                                |
|          ************** + **   x* +x       x  +  x                                                                                                          |
|          ****************x***x x*++x   x + x  +  x    +x       +                                                                                x           |
|          **************************x+x+x*+ x  +x x    +x       +   +                                        x                                   x           |
|          ******************************x*+ x ++xxx++ *** x   * + + + x *x +x  +   +  +      x               x    *           x                  x           |
|          ********************************+**x**x**+++***+**xx* + *+* x ****x  *x+x*x ++ x + x+ ++x + x+ x*x * +  *  +x   x   x  x  +   x++ +  + x     x     |
|          ********************************+*****x**+*************x*****x***** ***+x*****xx *x*+x***x++**x***+* * +* *+*x*x*** x++*  * *x*++ + ** x  x xxx   x|
| |________________M________________A_________________________________|                                                                                       |
||_______________M______________A______________________________|                                                                                              |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
    N           Min           Max        Median           Avg        Stddev
x 622          5476      17379769        951135     2951597.9     4064824.7
+ 666          4714      15869704        675624     2507897.4     3694162.5

@edwintorok
Copy link
Contributor Author

Found a commit from 2024 that can improve Db.Pool.get_record (by about 28%).
There is also a 2nd commit from 2024 May that I seem to have lost along the way, which would improve performance further: ffc2654. It needs some updates to work with the latest code, I'll likely open a separate PR for that one (there was an issue with backward compatibility in that form of the commit, but I found a solution meanwhile: use Schema.Value.t option * string instead of Schema.Value.t * string: that way if we have a cached deserialized form : use it, otherwise fall back to the old code that deserializes it).

@edwintorok edwintorok force-pushed the pr/pool-opt branch 2 times, most recently from f6c2fc2 to 27ed5d7 Compare May 1, 2025 12:58
@edwintorok
Copy link
Contributor Author

edwintorok commented May 6, 2025

There is a bug in the bechamel+ministat interaction here, will push a fixup.
The fix was:

diff --git a/ocaml/tests/bench/bechamel_simple_cli.ml b/ocaml/tests/bench/bechamel_simple_cli.ml
index c105e5576..9e68a960c 100644
--- a/ocaml/tests/bench/bechamel_simple_cli.ml
+++ b/ocaml/tests/bench/bechamel_simple_cli.ml
@@ -177,7 +177,10 @@ let cli ~always ~workloads cfg tests store =
         let label = Measure.label Instance.monotonic_clock in
         results.Benchmark.lr
         |> Array.iter @@ fun measurement ->
-           Printf.fprintf out "%.16g\n" (Measurement_raw.get ~label measurement)
+           Printf.fprintf out "%.16g\n"
+             (Measurement_raw.get ~label measurement
+             /. Measurement_raw.run measurement
+             )

@edwintorok edwintorok marked this pull request as draft May 7, 2025 09:14
@edwintorok
Copy link
Contributor Author

edwintorok commented May 7, 2025

Updated results:

Date.of_iso8601.dat
    N           Min           Max        Median           Avg        Stddev
x 787     1714.3056      102984.3     1803.7811     2083.1587     4050.0696
+ 906     510.72449     76567.429     553.89539     711.72983     2825.9458
Difference at 95.0% confidence
        -1371.43 +/- 329.44
        -65.8341% +/- 10.4184%
        (Student's t, pooled s = 3449.4)

Db.Pool.get_all_records.dat
    N           Min           Max        Median           Avg        Stddev
x 385     81587.406        501346     86572.986     87768.782     21350.859
+ 436     51569.267     474802.67     54387.452     55425.556     20179.019
Difference at 95.0% confidence
        -32343.2 +/- 2842.46
        -36.8505% +/- 2.71184%
        (Student's t, pooled s = 20736.7)

/Rpc.t -> pool_t.dat
    N           Min           Max        Median           Avg        Stddev
x 555      16966.75     271619.85     17578.645     18145.444      10806.11
+ 594     11514.608      267295.6     12063.637     12586.686     10488.391
Difference at 95.0% confidence
        -5558.76 +/- 1231.52
        -30.6345% +/- 5.8449%
        (Student's t, pooled s = 10643)

local_session_hook.dat
    N           Min           Max        Median           Avg        Stddev
x 1066     105.56609     17123.818     112.85689      135.5202     523.54823
+ 1170     37.056987     16767.938     40.308938     58.409015     489.83039
Difference at 95.0% confidence
        -77.1112 +/- 42.0078
        -56.9001% +/- 23.7033%
        (Student's t, pooled s = 506.185)

pool_t -> Rpc.t.dat
    N           Min           Max        Median           Avg        Stddev
x 658     5881.0645     230613.87     6389.6204     6801.4633     8755.5405
+ 660     5646.4516     259542.13     6296.8727      6731.495     9866.9415
No difference proven at 95.0% confidence

/sexpr_of_json_string.dat
    N           Min           Max        Median           Avg        Stddev
x 796     1555.6984     68929.878     1640.2099     1747.2214     2386.2715
+ 845     962.15315     52030.533     1017.9977     1089.0983     1755.6145
Difference at 95.0% confidence
        -658.123 +/- 201.897
        -37.6668% +/- 9.03743%
        (Student's t, pooled s = 2085.47)

str_of_sexp_json.dat
    N           Min           Max        Median           Avg        Stddev
x 790     1686.9744     40757.353     1739.3635     1801.7254     1390.9315
+ 907     522.44097     2973.7106     544.12747     552.12781     104.91282
Difference at 95.0% confidence
        -1249.6 +/- 90.8141
        -69.3556% +/- 1.59223%
        (Student's t, pooled s = 952.08)

@edwintorok edwintorok force-pushed the pr/pool-opt branch 2 times, most recently from 3a52289 to 30afc28 Compare May 7, 2025 16:39
@edwintorok edwintorok marked this pull request as ready for review May 7, 2025 16:39
@edwintorok edwintorok enabled auto-merge May 7, 2025 16:39
edwintorok added 4 commits May 7, 2025 18:33
Better progress indication, CLI tunables, and the ability to dump raw data to a directory,
that can be used by `ministat`.

Example usage:

```
mkdir /tmp/pool
dune exec ./bench_pool_field.exe --profile=release -- -d /tmp/pool --quota 20
...
dune exec ./bench_pool_field.exe --profile=release -- -d /tmp/pool --quota 20
for i in 'Db.Pool.get_all_records' 'Rpc.t -> pool_t' 'pool_t -> Rpc.t'; do ~/git/ministat/ministat -s "/tmp/pool/${OLD}/${i}.dat" "/tmp/pool/${NEW}/$i.dat" -c 99.5; done
```

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
Signed-off-by: Edwin Török <edwin.torok@cloud.com>
Write directly to the buffer, instead of writing to a buffer,
returning a string, and writing to a buffer again.

Escaping shows up in performance profiles, because `trusted_on_first_use` field
in the pool contains a JSON, which contains \" characters that need escaping.

Reduces memory allocations from 324.4308 mnw/run to 196.3633 mnw/run.  

Slight change in performance:
```
Db.Pool.get_all_records:
    N           Min           Max        Median           Avg        Stddev
x 384     81858.452     513398.88     87109.044      88314.45     21963.255
+ 389     78006.031     559052.12     83431.429     84780.535     24315.561
Difference at 95.0% confidence
        -3533.91 +/- 3267.84
        -4.00151% +/- 3.63493%
        (Student's t, pooled s = 23176.9)
```

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
The non-standard S-expression serializer wraps all strings in `'`.
The lexer also ignores every non-escaped character after a `'`, until it sees the next
`'` or escape char.

So it should be safe to avoid escaping `"`.

Unescaping is unchanged: any character can be escaped with `\`,
and it returns it unchanged after removing the escape char, so this preserves backwards
compatibility when loading an old database.

Escaping shows up in performance profiles, because `trusted_on_first_use` field
in the pool contains a JSON, which contains \" characters that needed escaping.
With this change it won't anymore.

`ministat` confirms that there is an improvement:
```
sexpr_of_json_string:
    N           Min           Max        Median           Avg        Stddev
x 806     1438.7128     63663.127     1490.3661     1594.5095     2192.0548
+ 850     911.23529     48528.173     967.25054     1037.7674     1632.6855
Difference at 95.0% confidence
        -556.742 +/- 185.531
        -34.9162% +/- 9.28192%
        (Student's t, pooled s = 1925.34)

str_of_sexp_json:
    N           Min           Max        Median           Avg        Stddev
x 792     1622.9135     49591.388     1719.5377     1786.3412     1702.6472
+ 893     605.37329     3354.8035     626.51812     636.34457     107.24734
Difference at 95.0% confidence
        -1150 +/- 111.92
        -64.3772% +/- 2.26583%
        (Student's t, pooled s = 1169.88)
```

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
edwintorok added 4 commits May 7, 2025 18:33
For backwards compatibility the serialized form looks like 20250319T04:16:24Z,
which had to go through several transformations before it was parsed: 2 sscanf, 2 sprintf,
and then the Ptime parser.

Since we're parsing the format with sscanf anyway, add a fastpath that builds a Ptime.t directly
without going through reformatting the string and reparsing it.

This speeds up API replies that contain dates, like pool which contains 'telemetry_next_collection' as a date.

`ministat` confirms:
```
Date.of_iso8601:
    N           Min           Max        Median           Avg        Stddev
x 786      1703.462     98255.061     1796.5826     2031.7502     3858.1277
+ 905      525.1954     73923.347     558.17972     711.02195     2732.7547
Difference at 95.0% confidence
        -1320.73 +/- 315.725
        -65.0045% +/- 10.3523%
        (Student's t, pooled s = 3303.82)

Db.Pool.get_all_records:
    N           Min           Max        Median           Avg        Stddev
x 390     76966.273      498179.5     82995.374     84536.667     21266.749
+ 401     69709.657        546133     74811.568     76379.246     23782.821
Difference at 95.0% confidence
        -8157.42 +/- 3147.12
        -9.64957% +/- 3.57009%
        (Student's t, pooled s = 22577.4)

Rpc.t -> pool_t  :
    N           Min           Max        Median           Avg        Stddev
x 554     16945.375     267477.23     17620.482      18195.16     10648.914
+ 594     11432.375     251226.67     12011.373     12493.367     9824.9986
Difference at 95.0% confidence
        -5701.79 +/- 1184.38
        -31.3369% +/- 5.53746%
        (Student's t, pooled s = 10230.9)

str_of_sexp_json:
    N           Min           Max        Median           Avg        Stddev
x 893     605.37329     3354.8035     626.51812     636.34457     107.24734
+ 911     510.23412     3298.9936     523.73684     532.25916     98.007142
Difference at 95.0% confidence
        -104.085 +/- 9.47756
        -16.3568% +/- 1.36325%
        (Student's t, pooled s = 102.685)
```

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
We called the wrong finally here. In general we mark the backtrace in finally as important
(which involves formatting it, this could be optimized separately).
However Mutex.unlock doesn't raise (in well-behaved code, unless you double unlock), so
we can use Fun.protect instead.

Hashtbl.find in xapi_local_session.ml raises every time in the RBAC checks,
this change avoids the costly backtrace formatting (which was discarded by a try/with later anyway).

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
We can use Hashtbl.mem instead of catching the exception from Hashtbl.find.

`ministat` confirms:
```
  local_session_hook  :
    N           Min           Max        Median           Avg        Stddev
x 1057     117.49169     15160.913      123.5201     142.19665     464.12468
+ 1166     38.851635      14365.19     41.619012     57.978805     423.21452
Difference at 95.0% confidence
        -84.2178 +/- 36.8873
        -59.2263% +/- 19.5015%
        (Student's t, pooled s = 443.137)
```

Although there are also some unexplained, but reproducible slowdowns in code that isn't touched by this commit at all:
```
str_of_sexp_json  :
    N           Min           Max        Median           Avg        Stddev
x 911     510.16149     3075.0192     523.44043     531.48578     97.085991
+ 892     605.73462     2951.0948     632.27053     652.10806      95.96889
Difference at 95.0% confidence
        120.622 +/- 8.91245
        22.6953% +/- 1.88103%
        (Student's t, pooled s = 96.5349)
```

Perhaps this is due to code layout changes?

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
read_record was marshaling the maps all the time, and some of these maps can be quite big.
Cache the marshaled form in the row itself. 
This is a trade-off between memory usage and performance
(caching increases global memory usage, but avoids repeated allocations every time get_record is called,
which may decrease memory usage/GC pressure under load).

Eventually we may be able to drop the marshaled form, but we need to change the db_rpc remote protocol for that
(it currently cannot marshal/unmarshal values on its own, because it doesn't have access to the type information).


`ministat` confirms:
```
Db.Pool.get_all_records  :
    N           Min           Max        Median           Avg        Stddev
x 400     71008.769     489829.75     75874.584     76957.497     20852.389
+ 871     51638.867     469264.11     54400.946     55511.462     19845.992
Difference at 95.0% confidence
        -21446 +/- 2387.53
        -27.8674% +/- 2.84131%
        (Student's t, pooled s = 20167.8)

sexpr_of_json_string  :
    N           Min           Max        Median           Avg        Stddev
x 862     819.74359     44758.053     851.53894      915.0725     1496.4717
+ 1688     970.16146      51923.24     1024.1987     1097.3771     1748.4532
Difference at 95.0% confidence
        182.305 +/- 136.827
        19.9224% +/- 15.8188%
        (Student's t, pooled s = 1667.57)

str_of_sexp_json  :
    N           Min           Max        Median           Avg        Stddev
x 1896     605.00587     2932.3931     627.51199     640.73142      89.48524
+ 1812     537.56839     3105.1991     550.25275     558.89761     93.633252
Difference at 95.0% confidence
        -81.8338 +/- 5.89411
        -12.7719% +/- 0.864486%
        (Student's t, pooled s = 91.5357)
```

Slight increase in 'sexpr_of_json_string'.

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
@edwintorok edwintorok added this pull request to the merge queue May 7, 2025
Merged via the queue into xapi-project:master with commit 69c312f May 7, 2025
17 checks passed
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.

3 participants