Skip to content

Commit d84b8e7

Browse files
Update RenderingMode.hx
1 parent 8eed159 commit d84b8e7

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

src/ffmpeg/RenderingMode.hx

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class RenderingMode {
105105

106106
var batchSize = 16; // Write 16 frames at once
107107
var batch:Array<Bytes> = [];
108+
var batchBuffer:Bytes = null; // Reusable buffer for batch writes
108109

109110
while (!stopRequested) {
110111
// Collect batch
@@ -120,30 +121,41 @@ class RenderingMode {
120121
continue;
121122
}
122123

123-
// Write entire batch directly
124+
// Calculate total size and allocate/reuse buffer
125+
var totalSize = 0;
126+
for (frame in batch) totalSize += frame.length;
127+
128+
if (batchBuffer == null || batchBuffer.length < totalSize) {
129+
batchBuffer = Bytes.alloc(totalSize);
130+
}
131+
132+
// Blit all frames into single buffer
133+
var offset = 0;
134+
for (frame in batch) {
135+
batchBuffer.blit(offset, frame, 0, frame.length);
136+
offset += frame.length;
137+
}
138+
139+
// Write entire batch as single operation
124140
try {
125141
#if cpp
126142
if (nativeProcessHandle != null) {
127143
// Direct native write - FASTEST!
128-
for (frame in batch) {
129-
var written = NativeProcess.process_stdin_write(
130-
nativeProcessHandle,
131-
frame.getData(),
132-
0,
133-
frame.length
134-
);
135-
if (written != frame.length) {
136-
Sys.println("Incomplete write: " + written + "/" + frame.length);
137-
}
144+
var written = NativeProcess.process_stdin_write(
145+
nativeProcessHandle,
146+
batchBuffer.getData(),
147+
0,
148+
totalSize
149+
);
150+
if (written != totalSize) {
151+
Sys.println("Incomplete write: " + written + "/" + totalSize);
138152
}
139153
} else
140154
#end
141155
{
142-
// Fallback to normal write
156+
// Fallback to normal write (single operation)
143157
if (process != null && process.stdin != null) {
144-
for (frame in batch) {
145-
process.stdin.write(frame);
146-
}
158+
process.stdin.writeBytes(batchBuffer, 0, totalSize);
147159
process.stdin.flush();
148160
}
149161
}

0 commit comments

Comments
 (0)