turbopipe 1.0.0__tar.gz → 1.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of turbopipe might be problematic. Click here for more details.

@@ -0,0 +1,223 @@
1
+ Metadata-Version: 2.1
2
+ Name: turbopipe
3
+ Version: 1.0.1
4
+ Summary: 🌀 Faster ModernGL Buffer inter process data transfers
5
+ Home-page: https://brokensrc.dev
6
+ Author-Email: Tremeschin <29046864+Tremeschin@users.noreply.github.com>
7
+ License: MIT License
8
+
9
+ Copyright (c) 2024 Gabriel Tremeschin
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+ Project-URL: Issues, https://github.com/BrokenSource/TurboPipe/issues
29
+ Project-URL: Repository, https://github.com/BrokenSource/TurboPipe
30
+ Project-URL: Documentation, https://github.com/BrokenSource/TurboPipe
31
+ Project-URL: Homepage, https://brokensrc.dev
32
+ Requires-Python: >=3.7
33
+ Requires-Dist: moderngl
34
+ Description-Content-Type: text/markdown
35
+
36
+ > [!IMPORTANT]
37
+ > <sub>Also check out [**ShaderFlow**](https://github.com/BrokenSource/ShaderFlow), where **TurboPipe** shines! 😉</sub>
38
+
39
+ <div align="center">
40
+ <a href="https://brokensrc.dev/"><img src="https://raw.githubusercontent.com/BrokenSource/TurboPipe/main/turbopipe/resources/images/turbopipe.png" width="200"></a>
41
+ <h1>TurboPipe</h1>
42
+ <br>
43
+ Faster <a href="https://github.com/moderngl/moderngl"><b>ModernGL</b></a> inter-process data transfers
44
+ </div>
45
+
46
+ <br>
47
+
48
+ # 🔥 Description
49
+
50
+ > TurboPipe speeds up sending raw bytes from `moderngl.Buffer` objects primarily to `FFmpeg` subprocess
51
+
52
+ The **optimizations** involved are:
53
+
54
+ - **Zero-copy**: Avoid unnecessary memory copies or allocation (intermediate `buffer.read()`)
55
+ - **C++**: The core of TurboPipe is written in C++ for speed, efficiency and low-level control
56
+ - **Chunks**: Write in chunks of 4096 bytes (RAM page size), so the hardware is happy
57
+ - **Threaded**:
58
+ - Doesn't block Python code execution, allows to render next frame
59
+ - Decouples the main thread from the I/O thread for performance
60
+
61
+ ✅ Don't worry, there's proper **safety** in place. TurboPipe will block Python if a memory address is already queued for writing, and guarantees order of writes per file-descriptor. Just call `.sync()` when done 😉
62
+
63
+ <br>
64
+
65
+ # 📦 Installation
66
+
67
+ It couldn't be easier! Just install in your package manager:
68
+
69
+ ```bash
70
+ pip install turbopipe
71
+ poetry add turbopipe
72
+ pdm add turbopipe
73
+ rye add turbopipe
74
+ ```
75
+
76
+ <br>
77
+
78
+ # 🚀 Usage
79
+
80
+ See also the [**Examples**](examples) folder for more controlled usage, and [**ShaderFlow**](https://github.com/BrokenSource/ShaderFlow/blob/main/ShaderFlow/Scene.py) usage of it!
81
+
82
+ ```python
83
+ import subprocess
84
+ import moderngl
85
+ import turbopipe
86
+
87
+ # Create ModernGL objects
88
+ ctx = moderngl.create_standalone_context()
89
+ buffer = ctx.buffer(reserve=1920*1080*3)
90
+
91
+ # Make sure resolution, pixel format matches!
92
+ ffmpeg = subprocess.Popen(
93
+ 'ffmpeg -f rawvideo -pix_fmt rgb24 -s 1920x1080 -i - -f null -'.split(),
94
+ stdin=subprocess.PIPE
95
+ )
96
+
97
+ # Rendering loop of yours
98
+ for _ in range(100):
99
+ turbopipe.pipe(buffer, ffmpeg.stdin.fileno())
100
+
101
+ # Finalize writing
102
+ turbo.sync()
103
+ ffmpeg.stdin.close()
104
+ ffmpeg.wait()
105
+ ```
106
+
107
+ <br>
108
+
109
+ # ⭐️ Benchmarks
110
+
111
+ > [!NOTE]
112
+ > **The tests conditions are as follows**:
113
+ > - The tests are the average of 3 runs to ensure consistency, with 3 GB of the same data being piped
114
+ > - The data is a random noise per-buffer between 128-135. So, multi-buffers runs are a noise video
115
+ > - All resolutions are wide-screen (16:9) and have 3 components (RGB) with 3 bytes per pixel (SDR)
116
+ > - Multi-buffer cycles through a list of buffer (eg. 1, 2, 3, 1, 2, 3... for 3-buffers)
117
+ > - All FFmpeg outputs are scrapped with `-f null -` to avoid any disk I/O bottlenecks
118
+ > - The `gain` column is the percentage increase over the standard method
119
+ > - When `x264` is Null, no encoding took place (passthrough)
120
+ > - The test cases emoji signifies:
121
+ > - 🐢: Standard `ffmpeg.stdin.write(buffer.read())` on just the main thread, pure Python
122
+ > - 🚀: Threaded `ffmpeg.stdin.write(buffer.read())` with a queue (similar to turbopipe)
123
+ > - 🌀: The magic of `turbopipe.pipe(buffer, ffmpeg.stdin.fileno())`
124
+ >
125
+ > Also see [`benchmark.py`](examples/benchmark.py) for the implementation
126
+
127
+ ✅ Check out benchmarks in a couple of systems below:
128
+
129
+ <details>
130
+ <summary><b>Desktop</b> • (AMD Ryzen 9 5900x) • (NVIDIA RTX 3060 12 GB) • (DDR4 2x32 GB 3200 MT/s) • (Arch Linux)</summary>
131
+ <br>
132
+
133
+ | 720p | x264 | Buffers | Framerate | Bandwidth | Gain |
134
+ |:----:|:----------|:---------:|----------:|------------:|---------:|
135
+ | 🐢 | Null | 1 | 882 fps | 2.44 GB/s | |
136
+ | 🚀 | Null | 1 | 793 fps | 2.19 GB/s | -10.04% |
137
+ | 🌀 | Null | 1 | 1911 fps | 5.28 GB/s | 116.70% |
138
+ | 🐢 | Null | 4 | 818 fps | 2.26 GB/s | |
139
+ | 🚀 | Null | 4 | 684 fps | 1.89 GB/s | -16.35% |
140
+ | 🌀 | Null | 4 | 1494 fps | 4.13 GB/s | 82.73% |
141
+ | 🐢 | ultrafast | 4 | 664 fps | 1.84 GB/s | |
142
+ | 🚀 | ultrafast | 4 | 635 fps | 1.76 GB/s | -4.33% |
143
+ | 🌀 | ultrafast | 4 | 869 fps | 2.40 GB/s | 31.00% |
144
+ | 🐢 | slow | 4 | 204 fps | 0.57 GB/s | |
145
+ | 🚀 | slow | 4 | 205 fps | 0.57 GB/s | 0.58% |
146
+ | 🌀 | slow | 4 | 208 fps | 0.58 GB/s | 2.22% |
147
+
148
+ | 1080p | x264 | Buffers | Framerate | Bandwidth | Gain |
149
+ |:-----:|:----------|:---------:|----------:|------------:|--------:|
150
+ | 🐢 | Null | 1 | 385 fps | 2.40 GB/s | |
151
+ | 🚀 | Null | 1 | 369 fps | 2.30 GB/s | -3.91% |
152
+ | 🌀 | Null | 1 | 641 fps | 3.99 GB/s | 66.54% |
153
+ | 🐢 | Null | 4 | 387 fps | 2.41 GB/s | |
154
+ | 🚀 | Null | 4 | 359 fps | 2.23 GB/s | -7.21% |
155
+ | 🌀 | Null | 4 | 632 fps | 3.93 GB/s | 63.40% |
156
+ | 🐢 | ultrafast | 4 | 272 fps | 1.70 GB/s | |
157
+ | 🚀 | ultrafast | 4 | 266 fps | 1.66 GB/s | -2.14% |
158
+ | 🌀 | ultrafast | 4 | 405 fps | 2.53 GB/s | 49.24% |
159
+ | 🐢 | slow | 4 | 117 fps | 0.73 GB/s | |
160
+ | 🚀 | slow | 4 | 122 fps | 0.76 GB/s | 4.43% |
161
+ | 🌀 | slow | 4 | 124 fps | 0.77 GB/s | 6.48% |
162
+
163
+ | 1440p | x264 | Buffers | Framerate | Bandwidth | Gain |
164
+ |:-----:|:----------|:---------:|----------:|------------:|--------:|
165
+ | 🐢 | Null | 1 | 204 fps | 2.26 GB/s | |
166
+ | 🚀 | Null | 1 | 241 fps | 2.67 GB/s | 18.49% |
167
+ | 🌀 | Null | 1 | 297 fps | 3.29 GB/s | 45.67% |
168
+ | 🐢 | Null | 4 | 230 fps | 2.54 GB/s | |
169
+ | 🚀 | Null | 4 | 235 fps | 2.61 GB/s | 2.52% |
170
+ | 🌀 | Null | 4 | 411 fps | 4.55 GB/s | 78.97% |
171
+ | 🐢 | ultrafast | 4 | 146 fps | 1.62 GB/s | |
172
+ | 🚀 | ultrafast | 4 | 153 fps | 1.70 GB/s | 5.21% |
173
+ | 🌀 | ultrafast | 4 | 216 fps | 2.39 GB/s | 47.96% |
174
+ | 🐢 | slow | 4 | 73 fps | 0.82 GB/s | |
175
+ | 🚀 | slow | 4 | 78 fps | 0.86 GB/s | 7.06% |
176
+ | 🌀 | slow | 4 | 79 fps | 0.88 GB/s | 9.27% |
177
+
178
+ | 2160p | x264 | Buffers | Framerate | Bandwidth | Gain |
179
+ |:-----:|:----------|:---------:|----------:|------------:|---------:|
180
+ | 🐢 | Null | 1 | 81 fps | 2.03 GB/s | |
181
+ | 🚀 | Null | 1 | 107 fps | 2.67 GB/s | 32.26% |
182
+ | 🌀 | Null | 1 | 213 fps | 5.31 GB/s | 163.47% |
183
+ | 🐢 | Null | 4 | 87 fps | 2.18 GB/s | |
184
+ | 🚀 | Null | 4 | 109 fps | 2.72 GB/s | 25.43% |
185
+ | 🌀 | Null | 4 | 212 fps | 5.28 GB/s | 143.72% |
186
+ | 🐢 | ultrafast | 4 | 59 fps | 1.48 GB/s | |
187
+ | 🚀 | ultrafast | 4 | 67 fps | 1.68 GB/s | 14.46% |
188
+ | 🌀 | ultrafast | 4 | 95 fps | 2.39 GB/s | 62.66% |
189
+ | 🐢 | slow | 4 | 37 fps | 0.94 GB/s | |
190
+ | 🚀 | slow | 4 | 43 fps | 1.07 GB/s | 16.22% |
191
+ | 🌀 | slow | 4 | 44 fps | 1.11 GB/s | 20.65% |
192
+
193
+ </details>
194
+
195
+ <details>
196
+ <summary><b>Desktop</b> • (AMD Ryzen 9 5900x) • (NVIDIA RTX 3060 12 GB) • (DDR4 2x32 GB 3200 MT/s) • (Windows 11)</summary>
197
+ <br>
198
+ </details>
199
+
200
+ <br>
201
+
202
+ <div align="justify">
203
+
204
+ # 🌀 Conclusion
205
+
206
+ TurboPipe significantly increases the feeding speed of FFmpeg with data, especially at higher resolutions. However, if there's few CPU compute available, or the video is too hard to encode (slow preset), the gains are insignificant over the other methods (bottleneck). Multi-buffering didn't prove to have an advantage, debugging shows that TurboPipe C++ is often starved of data to write (as the file stream is buffered on the OS most likely), and the context switching, or cache misses, might be the cause of the slowdown.
207
+
208
+ Interestingly, due either Linux's scheduler on AMD Ryzen CPUs, or their operating philosophy, it was experimentally seen that Ryzen's frenetic thread switching degrades a bit the single thread performance, which can be _"fixed"_ with prepending the command with `taskset --cpu 0,2` (not recommended at all), comparatively speaking to Windows performance on the same system (Linux 🚀 = Windows 🐢). This can also be due the topology of tested CPUs having more than one Core Complex Die (CCD). Intel CPUs seem to stick to the same thread for longer, which makes the Python threaded method an unecessary overhead.
209
+
210
+ ### Personal experience
211
+
212
+ On realistically loads, like [**ShaderFlow**](https://github.com/BrokenSource/ShaderFlow)'s default lightweight shader export, TurboPipe increases rendering speed from 1080p260 to 1080p330 on my system, with mid 80% CPU usage than low 60%s. For [**DepthFlow**](https://github.com/BrokenSource/ShaderFlow)'s default depth video export, no gains are seen, as the CPU is almost saturated encoding at 1080p130.
213
+
214
+ </div>
215
+
216
+ <br>
217
+
218
+ # 📚 Future work
219
+
220
+ - Add support for NumPy arrays, memoryviews, and byte-like objects
221
+ - Improve the thread synchronization and/or use a ThreadPool
222
+ - Maybe use `mmap` instead of chunks writing
223
+ - Test on MacOS 🙈
@@ -0,0 +1,188 @@
1
+ > [!IMPORTANT]
2
+ > <sub>Also check out [**ShaderFlow**](https://github.com/BrokenSource/ShaderFlow), where **TurboPipe** shines! 😉</sub>
3
+
4
+ <div align="center">
5
+ <a href="https://brokensrc.dev/"><img src="https://raw.githubusercontent.com/BrokenSource/TurboPipe/main/turbopipe/resources/images/turbopipe.png" width="200"></a>
6
+ <h1>TurboPipe</h1>
7
+ <br>
8
+ Faster <a href="https://github.com/moderngl/moderngl"><b>ModernGL</b></a> inter-process data transfers
9
+ </div>
10
+
11
+ <br>
12
+
13
+ # 🔥 Description
14
+
15
+ > TurboPipe speeds up sending raw bytes from `moderngl.Buffer` objects primarily to `FFmpeg` subprocess
16
+
17
+ The **optimizations** involved are:
18
+
19
+ - **Zero-copy**: Avoid unnecessary memory copies or allocation (intermediate `buffer.read()`)
20
+ - **C++**: The core of TurboPipe is written in C++ for speed, efficiency and low-level control
21
+ - **Chunks**: Write in chunks of 4096 bytes (RAM page size), so the hardware is happy
22
+ - **Threaded**:
23
+ - Doesn't block Python code execution, allows to render next frame
24
+ - Decouples the main thread from the I/O thread for performance
25
+
26
+ ✅ Don't worry, there's proper **safety** in place. TurboPipe will block Python if a memory address is already queued for writing, and guarantees order of writes per file-descriptor. Just call `.sync()` when done 😉
27
+
28
+ <br>
29
+
30
+ # 📦 Installation
31
+
32
+ It couldn't be easier! Just install in your package manager:
33
+
34
+ ```bash
35
+ pip install turbopipe
36
+ poetry add turbopipe
37
+ pdm add turbopipe
38
+ rye add turbopipe
39
+ ```
40
+
41
+ <br>
42
+
43
+ # 🚀 Usage
44
+
45
+ See also the [**Examples**](examples) folder for more controlled usage, and [**ShaderFlow**](https://github.com/BrokenSource/ShaderFlow/blob/main/ShaderFlow/Scene.py) usage of it!
46
+
47
+ ```python
48
+ import subprocess
49
+ import moderngl
50
+ import turbopipe
51
+
52
+ # Create ModernGL objects
53
+ ctx = moderngl.create_standalone_context()
54
+ buffer = ctx.buffer(reserve=1920*1080*3)
55
+
56
+ # Make sure resolution, pixel format matches!
57
+ ffmpeg = subprocess.Popen(
58
+ 'ffmpeg -f rawvideo -pix_fmt rgb24 -s 1920x1080 -i - -f null -'.split(),
59
+ stdin=subprocess.PIPE
60
+ )
61
+
62
+ # Rendering loop of yours
63
+ for _ in range(100):
64
+ turbopipe.pipe(buffer, ffmpeg.stdin.fileno())
65
+
66
+ # Finalize writing
67
+ turbo.sync()
68
+ ffmpeg.stdin.close()
69
+ ffmpeg.wait()
70
+ ```
71
+
72
+ <br>
73
+
74
+ # ⭐️ Benchmarks
75
+
76
+ > [!NOTE]
77
+ > **The tests conditions are as follows**:
78
+ > - The tests are the average of 3 runs to ensure consistency, with 3 GB of the same data being piped
79
+ > - The data is a random noise per-buffer between 128-135. So, multi-buffers runs are a noise video
80
+ > - All resolutions are wide-screen (16:9) and have 3 components (RGB) with 3 bytes per pixel (SDR)
81
+ > - Multi-buffer cycles through a list of buffer (eg. 1, 2, 3, 1, 2, 3... for 3-buffers)
82
+ > - All FFmpeg outputs are scrapped with `-f null -` to avoid any disk I/O bottlenecks
83
+ > - The `gain` column is the percentage increase over the standard method
84
+ > - When `x264` is Null, no encoding took place (passthrough)
85
+ > - The test cases emoji signifies:
86
+ > - 🐢: Standard `ffmpeg.stdin.write(buffer.read())` on just the main thread, pure Python
87
+ > - 🚀: Threaded `ffmpeg.stdin.write(buffer.read())` with a queue (similar to turbopipe)
88
+ > - 🌀: The magic of `turbopipe.pipe(buffer, ffmpeg.stdin.fileno())`
89
+ >
90
+ > Also see [`benchmark.py`](examples/benchmark.py) for the implementation
91
+
92
+ ✅ Check out benchmarks in a couple of systems below:
93
+
94
+ <details>
95
+ <summary><b>Desktop</b> • (AMD Ryzen 9 5900x) • (NVIDIA RTX 3060 12 GB) • (DDR4 2x32 GB 3200 MT/s) • (Arch Linux)</summary>
96
+ <br>
97
+
98
+ | 720p | x264 | Buffers | Framerate | Bandwidth | Gain |
99
+ |:----:|:----------|:---------:|----------:|------------:|---------:|
100
+ | 🐢 | Null | 1 | 882 fps | 2.44 GB/s | |
101
+ | 🚀 | Null | 1 | 793 fps | 2.19 GB/s | -10.04% |
102
+ | 🌀 | Null | 1 | 1911 fps | 5.28 GB/s | 116.70% |
103
+ | 🐢 | Null | 4 | 818 fps | 2.26 GB/s | |
104
+ | 🚀 | Null | 4 | 684 fps | 1.89 GB/s | -16.35% |
105
+ | 🌀 | Null | 4 | 1494 fps | 4.13 GB/s | 82.73% |
106
+ | 🐢 | ultrafast | 4 | 664 fps | 1.84 GB/s | |
107
+ | 🚀 | ultrafast | 4 | 635 fps | 1.76 GB/s | -4.33% |
108
+ | 🌀 | ultrafast | 4 | 869 fps | 2.40 GB/s | 31.00% |
109
+ | 🐢 | slow | 4 | 204 fps | 0.57 GB/s | |
110
+ | 🚀 | slow | 4 | 205 fps | 0.57 GB/s | 0.58% |
111
+ | 🌀 | slow | 4 | 208 fps | 0.58 GB/s | 2.22% |
112
+
113
+ | 1080p | x264 | Buffers | Framerate | Bandwidth | Gain |
114
+ |:-----:|:----------|:---------:|----------:|------------:|--------:|
115
+ | 🐢 | Null | 1 | 385 fps | 2.40 GB/s | |
116
+ | 🚀 | Null | 1 | 369 fps | 2.30 GB/s | -3.91% |
117
+ | 🌀 | Null | 1 | 641 fps | 3.99 GB/s | 66.54% |
118
+ | 🐢 | Null | 4 | 387 fps | 2.41 GB/s | |
119
+ | 🚀 | Null | 4 | 359 fps | 2.23 GB/s | -7.21% |
120
+ | 🌀 | Null | 4 | 632 fps | 3.93 GB/s | 63.40% |
121
+ | 🐢 | ultrafast | 4 | 272 fps | 1.70 GB/s | |
122
+ | 🚀 | ultrafast | 4 | 266 fps | 1.66 GB/s | -2.14% |
123
+ | 🌀 | ultrafast | 4 | 405 fps | 2.53 GB/s | 49.24% |
124
+ | 🐢 | slow | 4 | 117 fps | 0.73 GB/s | |
125
+ | 🚀 | slow | 4 | 122 fps | 0.76 GB/s | 4.43% |
126
+ | 🌀 | slow | 4 | 124 fps | 0.77 GB/s | 6.48% |
127
+
128
+ | 1440p | x264 | Buffers | Framerate | Bandwidth | Gain |
129
+ |:-----:|:----------|:---------:|----------:|------------:|--------:|
130
+ | 🐢 | Null | 1 | 204 fps | 2.26 GB/s | |
131
+ | 🚀 | Null | 1 | 241 fps | 2.67 GB/s | 18.49% |
132
+ | 🌀 | Null | 1 | 297 fps | 3.29 GB/s | 45.67% |
133
+ | 🐢 | Null | 4 | 230 fps | 2.54 GB/s | |
134
+ | 🚀 | Null | 4 | 235 fps | 2.61 GB/s | 2.52% |
135
+ | 🌀 | Null | 4 | 411 fps | 4.55 GB/s | 78.97% |
136
+ | 🐢 | ultrafast | 4 | 146 fps | 1.62 GB/s | |
137
+ | 🚀 | ultrafast | 4 | 153 fps | 1.70 GB/s | 5.21% |
138
+ | 🌀 | ultrafast | 4 | 216 fps | 2.39 GB/s | 47.96% |
139
+ | 🐢 | slow | 4 | 73 fps | 0.82 GB/s | |
140
+ | 🚀 | slow | 4 | 78 fps | 0.86 GB/s | 7.06% |
141
+ | 🌀 | slow | 4 | 79 fps | 0.88 GB/s | 9.27% |
142
+
143
+ | 2160p | x264 | Buffers | Framerate | Bandwidth | Gain |
144
+ |:-----:|:----------|:---------:|----------:|------------:|---------:|
145
+ | 🐢 | Null | 1 | 81 fps | 2.03 GB/s | |
146
+ | 🚀 | Null | 1 | 107 fps | 2.67 GB/s | 32.26% |
147
+ | 🌀 | Null | 1 | 213 fps | 5.31 GB/s | 163.47% |
148
+ | 🐢 | Null | 4 | 87 fps | 2.18 GB/s | |
149
+ | 🚀 | Null | 4 | 109 fps | 2.72 GB/s | 25.43% |
150
+ | 🌀 | Null | 4 | 212 fps | 5.28 GB/s | 143.72% |
151
+ | 🐢 | ultrafast | 4 | 59 fps | 1.48 GB/s | |
152
+ | 🚀 | ultrafast | 4 | 67 fps | 1.68 GB/s | 14.46% |
153
+ | 🌀 | ultrafast | 4 | 95 fps | 2.39 GB/s | 62.66% |
154
+ | 🐢 | slow | 4 | 37 fps | 0.94 GB/s | |
155
+ | 🚀 | slow | 4 | 43 fps | 1.07 GB/s | 16.22% |
156
+ | 🌀 | slow | 4 | 44 fps | 1.11 GB/s | 20.65% |
157
+
158
+ </details>
159
+
160
+ <details>
161
+ <summary><b>Desktop</b> • (AMD Ryzen 9 5900x) • (NVIDIA RTX 3060 12 GB) • (DDR4 2x32 GB 3200 MT/s) • (Windows 11)</summary>
162
+ <br>
163
+ </details>
164
+
165
+ <br>
166
+
167
+ <div align="justify">
168
+
169
+ # 🌀 Conclusion
170
+
171
+ TurboPipe significantly increases the feeding speed of FFmpeg with data, especially at higher resolutions. However, if there's few CPU compute available, or the video is too hard to encode (slow preset), the gains are insignificant over the other methods (bottleneck). Multi-buffering didn't prove to have an advantage, debugging shows that TurboPipe C++ is often starved of data to write (as the file stream is buffered on the OS most likely), and the context switching, or cache misses, might be the cause of the slowdown.
172
+
173
+ Interestingly, due either Linux's scheduler on AMD Ryzen CPUs, or their operating philosophy, it was experimentally seen that Ryzen's frenetic thread switching degrades a bit the single thread performance, which can be _"fixed"_ with prepending the command with `taskset --cpu 0,2` (not recommended at all), comparatively speaking to Windows performance on the same system (Linux 🚀 = Windows 🐢). This can also be due the topology of tested CPUs having more than one Core Complex Die (CCD). Intel CPUs seem to stick to the same thread for longer, which makes the Python threaded method an unecessary overhead.
174
+
175
+ ### Personal experience
176
+
177
+ On realistically loads, like [**ShaderFlow**](https://github.com/BrokenSource/ShaderFlow)'s default lightweight shader export, TurboPipe increases rendering speed from 1080p260 to 1080p330 on my system, with mid 80% CPU usage than low 60%s. For [**DepthFlow**](https://github.com/BrokenSource/ShaderFlow)'s default depth video export, no gains are seen, as the CPU is almost saturated encoding at 1080p130.
178
+
179
+ </div>
180
+
181
+ <br>
182
+
183
+ # 📚 Future work
184
+
185
+ - Add support for NumPy arrays, memoryviews, and byte-like objects
186
+ - Improve the thread synchronization and/or use a ThreadPool
187
+ - Maybe use `mmap` instead of chunks writing
188
+ - Test on MacOS 🙈
@@ -5,6 +5,7 @@ from typing import Generator
5
5
 
6
6
  import moderngl
7
7
  import tqdm
8
+
8
9
  import turbopipe
9
10
 
10
11
  # User constants
@@ -75,6 +76,7 @@ with Progress() as progress, FFmpeg() as ffmpeg:
75
76
  for frame in range(TOTAL_FRAMES):
76
77
  turbopipe.pipe(buffer, fileno)
77
78
  progress()
79
+
78
80
  turbopipe.sync()
79
81
 
80
82
  turbopipe.close()
@@ -0,0 +1,192 @@
1
+ # python -m pip install pandas tqdm attrs tabulate
2
+ import contextlib
3
+ import enum
4
+ import subprocess
5
+ import time
6
+ from collections import deque
7
+ from queue import Queue
8
+ from threading import Thread
9
+ from typing import Optional
10
+
11
+ import attr
12
+ import moderngl
13
+ import numpy
14
+ import pandas
15
+ import tqdm
16
+
17
+ import turbopipe
18
+
19
+ ctx = moderngl.create_standalone_context()
20
+ AVERAGE_N_RUNS = 3
21
+ DATA_SIZE = 3
22
+
23
+ # -------------------------------------------------------------------------------------------------|
24
+
25
+ class Methods(enum.Enum):
26
+ TRADITIONAL = "🐢"
27
+ PYTHON_THREADED = "🚀"
28
+ TURBOPIPE = "🌀"
29
+
30
+ # -------------------------------------------------------------------------------------------------|
31
+
32
+ @attr.s(auto_attribs=True)
33
+ class StdinWrapper:
34
+ _process: subprocess.Popen
35
+ _queue: Queue = attr.Factory(lambda: Queue(maxsize=10))
36
+ _loop: bool = True
37
+ _stdin: any = None
38
+
39
+ def __attrs_post_init__(self):
40
+ Thread(target=self.worker, daemon=True).start()
41
+
42
+ def write(self, data):
43
+ self._queue.put(data)
44
+
45
+ def worker(self):
46
+ while self._loop:
47
+ self._stdin.write(self._queue.get())
48
+ self._queue.task_done()
49
+
50
+ def close(self):
51
+ self._queue.join()
52
+ self._stdin.close()
53
+ self._loop = False
54
+ while self._process.poll() is None:
55
+ time.sleep(0.01)
56
+
57
+ # -------------------------------------------------------------------------------------------------|
58
+
59
+ @attr.s(auto_attribs=True)
60
+ class Statistics:
61
+ BYTES_PER_FRAME: int
62
+
63
+ def __attrs_post_init__(self):
64
+ self.start_time = time.perf_counter()
65
+ self.frametimes = deque()
66
+
67
+ def next(self):
68
+ now = time.perf_counter()
69
+ self.frametimes.append(now - self.start_time)
70
+ self.start_time = now
71
+
72
+ @property
73
+ def average_fps(self):
74
+ return 1 / numpy.mean(self.frametimes)
75
+
76
+ @property
77
+ def std_fps(self):
78
+ return numpy.std([1/t for t in self.frametimes])
79
+
80
+ @property
81
+ def average_bps(self):
82
+ return self.BYTES_PER_FRAME * self.average_fps
83
+
84
+ @property
85
+ def std_bps(self):
86
+ return self.BYTES_PER_FRAME * self.std_fps
87
+
88
+ # -------------------------------------------------------------------------------------------------|
89
+
90
+ HD = (1280, 720)
91
+ FHD = (1920, 1080)
92
+ QHD = (2560, 1440)
93
+ UHD = (3840, 2160)
94
+
95
+ @attr.s(auto_attribs=True)
96
+ class Benchmark:
97
+ table: pandas.DataFrame = attr.Factory(pandas.DataFrame)
98
+
99
+ @contextlib.contextmanager
100
+ def ffmpeg(self, width: int, height: int, x264_preset: Optional[str] = None):
101
+ command = [
102
+ "ffmpeg",
103
+ "-hide_banner",
104
+ "-loglevel", "error",
105
+ "-f", "rawvideo",
106
+ "-pix_fmt", "rgb24",
107
+ "-s", f"{width}x{height}",
108
+ "-r", "60",
109
+ "-i", "-"
110
+ ]
111
+
112
+ command.extend([
113
+ "-c:v", "libx264",
114
+ "-preset", x264_preset,
115
+ ] if x264_preset else [])
116
+
117
+ command.extend(["-f", "null", "-"])
118
+
119
+ try:
120
+ process = subprocess.Popen(command, stdin=subprocess.PIPE)
121
+ yield process
122
+ finally:
123
+ turbopipe.sync()
124
+ process.stdin.close()
125
+ process.wait()
126
+
127
+ def run(self):
128
+ for (width, height) in (HD, FHD, QHD, UHD):
129
+ for test_case in "ABCD":
130
+ for index, method in enumerate(Methods):
131
+
132
+ # Selective x264 presets
133
+ if test_case == "C":
134
+ X264 = "ultrafast"
135
+ elif test_case == "D":
136
+ X264 = "slow"
137
+ else:
138
+ X264 = None
139
+
140
+ # Test constants for fairness
141
+ nbuffer = (4 if test_case in ["B", "C", "D"] else 1)
142
+ buffers = [ctx.buffer(data=numpy.random.randint(128, 135, (height, width, 3), dtype=numpy.uint8)) for _ in range(nbuffer)]
143
+ bytes_per_frame = (width * height * 3)
144
+ total_frames = int((DATA_SIZE * 1024**3) / bytes_per_frame)
145
+ statistics = Statistics(bytes_per_frame)
146
+
147
+ for run in range(AVERAGE_N_RUNS):
148
+ statistics.start_time = time.perf_counter()
149
+
150
+ with self.ffmpeg(width, height, X264) as process:
151
+ if method == Methods.PYTHON_THREADED:
152
+ process.stdin = StdinWrapper(process=process, stdin=process.stdin)
153
+
154
+ for frame in tqdm.tqdm(
155
+ desc=f"Processing (Run #{run}) ({method.value} {width}x{height})",
156
+ iterable=range(total_frames),
157
+ smoothing=0, unit=" Frame"
158
+ ):
159
+ buffer = buffers[frame % nbuffer]
160
+
161
+ if method == Methods.TURBOPIPE:
162
+ turbopipe.pipe(buffer, process.stdin.fileno())
163
+ else:
164
+ process.stdin.write(buffer.read())
165
+
166
+ statistics.next()
167
+
168
+ for buffer in buffers:
169
+ buffer.release()
170
+
171
+ # Calculate the gain%
172
+ if index != 0:
173
+ baseline = float(self.table.iloc[-index]['Framerate'].split()[0])
174
+ gain = (float(statistics.average_fps) - baseline) / baseline * 100
175
+
176
+ self.table = self.table._append({
177
+ 'Test': f"{method.value} {height}p",
178
+ 'x264': (X264 or "Null"),
179
+ 'Buffers': nbuffer,
180
+ 'Framerate': f"{int(statistics.average_fps)} fps",
181
+ 'Bandwidth': f"{statistics.average_bps / 1e9:.2f} GB/s",
182
+ 'Gain': (f"{gain:.2f}%" if index != 0 else None)
183
+ }, ignore_index=True)
184
+
185
+ print("\nBenchmark Results:")
186
+ print(self.table.to_markdown(index=False))
187
+
188
+ # -------------------------------------------------------------------------------------------------|
189
+
190
+ if __name__ == "__main__":
191
+ benchmark = Benchmark()
192
+ benchmark.run()
@@ -21,6 +21,8 @@ cpp_args = ['-fpermissive']
21
21
 
22
22
  if host_machine.system() == 'darwin'
23
23
  cpp_args += ['-Wno-deprecated-declarations']
24
+ elif host_machine.system() == 'windows'
25
+ cpp_args += ['-static', '-static-libgcc', '-static-libstdc++']
24
26
  endif
25
27
 
26
28
  # ----------------------------------------------|
@@ -17,3 +17,6 @@ requires-python = ">=3.7"
17
17
  [build-system]
18
18
  requires = ["meson-python", "ninja"]
19
19
  build-backend = "mesonpy"
20
+
21
+ [tool.ruff.format]
22
+ exclude = ["*"]
@@ -0,0 +1,92 @@
1
+ <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
+ <svg
3
+ width="32"
4
+ height="32"
5
+ version="1"
6
+ id="svg3"
7
+ sodipodi:docname="turbopipe.svg"
8
+ inkscape:version="1.3.2 (091e20ef0f, 2023-11-25, custom)"
9
+ inkscape:export-filename="turbopipe.png"
10
+ inkscape:export-xdpi="1536"
11
+ inkscape:export-ydpi="1536"
12
+ xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
13
+ xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
14
+ xmlns="http://www.w3.org/2000/svg"
15
+ xmlns:svg="http://www.w3.org/2000/svg">
16
+ <defs
17
+ id="defs3" />
18
+ <sodipodi:namedview
19
+ id="namedview3"
20
+ pagecolor="#ffffff"
21
+ bordercolor="#000000"
22
+ borderopacity="0.25"
23
+ inkscape:showpageshadow="2"
24
+ inkscape:pageopacity="0.0"
25
+ inkscape:pagecheckerboard="0"
26
+ inkscape:deskcolor="#d1d1d1"
27
+ inkscape:zoom="15.59375"
28
+ inkscape:cx="7.1182365"
29
+ inkscape:cy="23.054108"
30
+ inkscape:window-width="2001"
31
+ inkscape:window-height="1437"
32
+ inkscape:window-x="1115"
33
+ inkscape:window-y="510"
34
+ inkscape:window-maximized="0"
35
+ inkscape:current-layer="g5" />
36
+ <g
37
+ transform="matrix(.91242 0 0 .91242 1.4013026 1.6537216)"
38
+ id="g3">
39
+ <rect
40
+ style="opacity:.2"
41
+ width="28"
42
+ height="29"
43
+ x="2"
44
+ y="2"
45
+ rx="4.1999998"
46
+ ry="4.1999998"
47
+ id="rect1" />
48
+ <rect
49
+ style="fill:#0286e3;fill-opacity:1"
50
+ width="28"
51
+ height="29"
52
+ x="2"
53
+ y="1"
54
+ rx="4.1999998"
55
+ ry="4.1999998"
56
+ id="rect2" />
57
+ <path
58
+ style="opacity:.1;fill:#fff"
59
+ d="M6.1992188 1C3.8724189 1 2 2.8724189 2 5.1992188v1C2 3.8724189 3.8724189 2 6.1992188 2H25.800781C28.127581 2 30 3.8724189 30 6.1992188v-1C30 2.8724189 28.127581 1 25.800781 1Z"
60
+ id="path2" />
61
+ <g
62
+ id="g5"
63
+ transform="translate(2.8943202,-0.90493489)">
64
+ <g
65
+ id="g1"
66
+ transform="matrix(0.88452864,0,0,0.88452864,-2.0420221,2.7524767)">
67
+ <text
68
+ xml:space="preserve"
69
+ style="font-size:26.9514px;font-family:'Roboto Slab';-inkscape-font-specification:'Roboto Slab';opacity:0.2;fill:#000000;fill-opacity:1;stroke:none;stroke-width:42.808;stroke-linejoin:round;stroke-dasharray:none"
70
+ x="7.6854787"
71
+ y="26.417761"
72
+ id="text2"
73
+ transform="translate(0.21518099,-0.33798856)"><tspan
74
+ x="7.6854787"
75
+ y="26.417761"
76
+ style="font-weight:400;fill:#000000;fill-opacity:1;stroke-width:42.808;stroke-dasharray:none"
77
+ id="tspan2">T</tspan></text>
78
+ <text
79
+ xml:space="preserve"
80
+ style="font-size:26.9514px;font-family:'Roboto Slab';-inkscape-font-specification:'Roboto Slab';fill:#ffffff;fill-opacity:1;stroke:none;stroke-width:42.8078;stroke-linejoin:round;stroke-dasharray:none"
81
+ x="7.6843672"
82
+ y="25.418978"
83
+ id="text3"
84
+ transform="translate(0.21518099,-0.33798856)"><tspan
85
+ x="7.6843672"
86
+ y="25.418978"
87
+ style="font-style:normal;font-weight:400;fill:#ffffff;fill-opacity:1;stroke-width:42.8078"
88
+ id="tspan3">T</tspan></text>
89
+ </g>
90
+ </g>
91
+ </g>
92
+ </svg>
@@ -1,3 +1,3 @@
1
1
  #!/usr/bin/env python3
2
- __version__ = "1.0.0"
2
+ __version__ = "1.0.1"
3
3
  print(__version__)
turbopipe-1.0.0/PKG-INFO DELETED
@@ -1,34 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: turbopipe
3
- Version: 1.0.0
4
- Summary: 🌀 Faster ModernGL Buffer inter process data transfers
5
- Home-page: https://brokensrc.dev
6
- Author-Email: Tremeschin <29046864+Tremeschin@users.noreply.github.com>
7
- License: MIT License
8
-
9
- Copyright (c) 2024 Gabriel Tremeschin
10
-
11
- Permission is hereby granted, free of charge, to any person obtaining a copy
12
- of this software and associated documentation files (the "Software"), to deal
13
- in the Software without restriction, including without limitation the rights
14
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
- copies of the Software, and to permit persons to whom the Software is
16
- furnished to do so, subject to the following conditions:
17
-
18
- The above copyright notice and this permission notice shall be included in all
19
- copies or substantial portions of the Software.
20
-
21
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
- SOFTWARE.
28
- Project-URL: Issues, https://github.com/BrokenSource/TurboPipe/issues
29
- Project-URL: Repository, https://github.com/BrokenSource/TurboPipe
30
- Project-URL: Documentation, https://github.com/BrokenSource/TurboPipe
31
- Project-URL: Homepage, https://brokensrc.dev
32
- Requires-Python: >=3.7
33
- Requires-Dist: moderngl
34
- Description-Content-Type: text/markdown
turbopipe-1.0.0/Readme.md DELETED
File without changes
File without changes
File without changes
File without changes
File without changes