NativeHTTPRequest: Improve buffer management for O(1) performance

Fixes debilitating issue on Native where buffer creation created an O(n) slowdown
This commit is contained in:
Chris Speciale
2022-05-27 23:03:14 -04:00
parent 6db5820f97
commit 02617a854d
2 changed files with 14 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
package lime._internal.backend.native; package lime._internal.backend.native;
import haxe.io.Bytes; import haxe.io.Bytes;
import haxe.io.BytesBuffer;
import haxe.Timer; import haxe.Timer;
import lime.app.Future; import lime.app.Future;
import lime.app.Promise; import lime.app.Promise;
@@ -42,6 +43,7 @@ class NativeHTTPRequest
#end #end
private static var cookieList:Array<String>; private static var cookieList:Array<String>;
private var buffer:BytesBuffer = new BytesBuffer();
private var bytes:Bytes; private var bytes:Bytes;
private var bytesLoaded:Int; private var bytesLoaded:Int;
private var bytesTotal:Int; private var bytesTotal:Int;
@@ -51,7 +53,6 @@ class NativeHTTPRequest
private var promise:Promise<Bytes>; private var promise:Promise<Bytes>;
private var writeBytesLoaded:Int; private var writeBytesLoaded:Int;
private var writeBytesTotal:Int; private var writeBytesTotal:Int;
private var writePosition:Int;
private var timeout:Timer; private var timeout:Timer;
public function new() public function new()
@@ -94,7 +95,6 @@ class NativeHTTPRequest
bytesTotal = 0; bytesTotal = 0;
writeBytesLoaded = 0; writeBytesLoaded = 0;
writeBytesTotal = 0; writeBytesTotal = 0;
writePosition = 0;
if (curl == null) if (curl == null)
{ {
@@ -349,6 +349,7 @@ class NativeHTTPRequest
future.onComplete(function(bytes) future.onComplete(function(bytes)
{ {
bytes = buildBuffer();
if (bytes == null) if (bytes == null)
{ {
promise.complete(null); promise.complete(null);
@@ -362,14 +363,9 @@ class NativeHTTPRequest
return promise.future; return promise.future;
} }
private function growBuffer(length:Int) private function buildBuffer() {
{ bytes = buffer.getBytes();
if (length > bytes.length) return bytes;
{
var cacheBytes = bytes;
bytes = Bytes.alloc(length);
bytes.blit(0, cacheBytes, 0, cacheBytes.length);
}
} }
// Event Handlers // Event Handlers
@@ -399,11 +395,7 @@ class NativeHTTPRequest
private function curl_onWrite(curl:CURL, output:Bytes):Int private function curl_onWrite(curl:CURL, output:Bytes):Int
{ {
growBuffer(writePosition + output.length); buffer.add(output);
bytes.blit(writePosition, output, 0, output.length);
writePosition += output.length;
return output.length; return output.length;
} }

View File

@@ -110,6 +110,10 @@ public function load(uri:String = null):Future<T>
future.onComplete(function(bytes) future.onComplete(function(bytes)
{ {
#if sys
bytes = @:privateAccess __backend.buildBuffer();
#end
responseData = fromBytes(bytes); responseData = fromBytes(bytes);
promise.complete(responseData); promise.complete(responseData);
}); });