Need for Speed: Removing speed bumps from your Symfony projects ⚡️

ukaszChruciel1 40 views 43 slides Jun 07, 2024
Slide 1
Slide 1 of 88
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88

About This Presentation

No one wants their application to drag like a car stuck in the slow lane! Yet it’s all too common to encounter bumpy, pothole-filled solutions that slow the speed of any application. Symfony apps are not an exception.
In this talk, I will take you for a spin around the performance racetrack. We’...


Slide Content

Need for Speed
Removing speed bumps from your Symfony projects

Introduction

Weavers, Sylius

Why do we need to thinks
about performance?

~ Amazon
“100ms Faster
=
1% More Revenue.”

How?

Let’s start!

The most common performance bottlenecks
0
15
30
45
60
Amount of queries to DBCost of object serialization Latency Framework 54
28
56
Votes

Base

Algorithmic complexity

O(M) vs O(MN) vs O(M^2)

That’s why we usually forget
about it :/

Measure!

Understanding of tools

Tools are generally performant
But Your case is different

Project structure

Database turn

N+1

Product list

Let’s start small
{
"@context":"/api/contexts/Product",
"@id":"/api/products",
"@type":"hydra:Collection",
"hydra:totalItems":6,
"hydra:member":[
{
"@id":"/api/products/1",
"@type":"Product",
"id":1,
"name":"T-Shirt",
"price":1000
},
{
"@id":"/api/products/2",
"@type":"Product",
"id":2,
"name":"Trousers",
"price":5000
},
{
"“…“":"“…“"
}
]
}
Amount of products: 6
No associations between objects
Sample query on the left

O(1)

Order list

How many queries are executed here?
{
"@context": "/api/contexts/Order",
"@id": "/api/orders",
"@type": "hydra:Collection",
"hydra:totalItems": 3,
"hydra:member": [
{
"@id": "/api/orders/1",
"@type": "Order",
"id": 1,
"orderItems": [
"/api/order_items/1",
"/api/order_items/2"
]
},
{
“…”: “…“
}
]
}
Amount of orders: 3
Every order associated with 2 items
Sample query on the left
No total field

O(M)

Order list with additional field

How many queries are executed here?
{
"@context": "/api/contexts/Order",
"@id": "/api/orders",
"@type": "hydra:Collection",
"hydra:totalItems": 3,
"hydra:member": [
{
"@id": "/api/orders/1",
"@type": "Order",
"id": 1,
"orderItems": [
"/api/order_items/1",
"/api/order_items/2"
],
"total": 7000
},
{
“…”: “…“
}
]
}
Amount of orders: 3
Every order associated with 2 items
Sample query on the left
Added “total()” as a function of product
price and quantity of item

O(M*N) or O(M^2) !

How to spot it?

How to fix it?

Solution 1

#[ORM\OneToMany(fetch: ‘LAZY')]
#[ORM\OneToMany(fetch: ‘EXTRA_LAZY')]
#[ORM\OneToMany(fetch: ‘EAGER')]
What is the difference between them?

#[ORM\OneToMany(fetch: ‘LAZY')] => 11
#[ORM\OneToMany(fetch: ‘EXTRA_LAZY')] => ???
#[ORM\OneToMany(fetch: ‘EAGER')] => ???

#[ORM\OneToMany(fetch: ‘LAZY')] => 11
#[ORM\OneToMany(fetch: ‘EXTRA_LAZY')] => 11
#[ORM\OneToMany(fetch: ‘EAGER')] => ???

#[ORM\OneToMany(fetch: ‘LAZY')] => 11
#[ORM\OneToMany(fetch: ‘EXTRA_LAZY')] => 11
#[ORM\OneToMany(fetch: ‘EAGER')] => 9

#[ORM\OneToMany(fetch: ‘LAZY')] => 11
#[ORM\OneToMany(fetch: ‘EXTRA_LAZY')] => 11
#[ORM\OneToMany(fetch: ‘EAGER')] => 9
#[ORM\OneToMany(fetch: ‘EAGER')] => 5 (OrderItem and Product)

#[ORM\OneToMany(fetch: ‘LAZY')] => 11
#[ORM\OneToMany(fetch: ‘EXTRA_LAZY')] => 11
#[ORM\OneToMany(fetch: ‘EAGER')] => 5
#[ORM\OneToMany(fetch: ‘EAGER')] (OrderItem and Product) + Serialisation groups => 4

But….

Solution 2

$this->createQueryBuilder('o')
->addSelect('oi', 'p')
->join(OrderItem::class, 'oi', Join::WITH, 'oi.originOrder = o')
->join(Product::class, 'p', Join::WITH, 'oi.product = p')
;

So is single query an ultimate
solution?

⚠ Put the brakes on ⚠

Joins are expensive #

Process of
serialisation

Serialisation process
oversimplified

[
'id' => 1,
'name' => 'T-Shirt',
'price' => '1000'
]
Application
Database

[
'id' => 1,
'name' => 'T-Shirt',
'price' => '1000'
]
Hydration $
new Product()
Application
Database

[
'id' => 1,
'name' => 'T-Shirt',
'price' => '1000'
]
Hydration $
new Product()
Application
Serialization ⚙
{
‘id': 1,
‘name': 'T-Shirt',
‘price': '1000'
}
Database Frontend app

[
'id' => 1,
'name' => 'T-Shirt',
'price' => '1000'
]
Hydration $
new Product()
Application
Serialization ⚙
{
‘id': 1,
‘name': 'T-Shirt',
‘price': '1000'
}
Database Frontend app
&

Fullstack vs Headless

Usefulness of laziness

Profile Total time Network timeMemory usage Amount of queries
Reference build 1.1 s 562 ms 32.9 MB 500 ms / 134 rq
Profile with leftjoin 813 ms 511 ms 16.6 MB 456 ms / 54 rq
Profile with lazy translations833 ms 502 ms 27.3 MB 446 ms / 54 rq

Lazy vs Extra_Lazy

Straight of good
design

Huge objects are problem

Different representations

Minimising unnecessary
operations

Read model
shortcut

Saving data to different models

PUT REQUEST

Pre-computing

Sylius order

Saving read-optimised objects

Read models doesn’t have to
be models '

1. Store read models in key-value storage
2. Restore it by indexed key

Use “SELECT NEW” syntax

$query = $this->_em->createQuery(
'SELECT NEW OrderItemDTO(SUM(oi.quantity * p.price) FROM OrderItem oi JOIN oi.product p ’
);
$query->getResult(); // array of OrderItemDTO

Summary

Remember about algorithmic complexity of your queries
Do not hydrate too much data
Think out of the box of your “entities”

Thank you!