From 13efc3bba7193edbe9ec7aa6a0120c7e540f127f Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Wed, 21 Jul 2021 22:08:50 +0200 Subject: [PATCH] Allocate scratchpad once and then dup copy a range Improved efficiency and memory usage (till next GC) --- source/libtun/adapter.d | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/source/libtun/adapter.d b/source/libtun/adapter.d index c528a2f..ac5ab19 100644 --- a/source/libtun/adapter.d +++ b/source/libtun/adapter.d @@ -20,6 +20,10 @@ public class TUNAdapter private int tunFD; private bool isClosed; + /* Temporary scratchpad buffer */ + private byte[] scratch; + + this(string interfaceName, AdapterType adapterType = AdapterType.TAP) { init(interfaceName); @@ -32,6 +36,10 @@ public class TUNAdapter { throw new TUNException("Error creating tun device"); } + + /* TODO: Get device MTU and add functions for setting it */ + ushort mtu = cast(ushort)-1; + scratch.length = mtu; } private void sanityCheck() @@ -55,12 +63,7 @@ public class TUNAdapter { sanityCheck(); - /* TODO: Get device MTU and add functions for setting it */ - ushort mtu = cast(ushort)-1; - - /* Temporary scratchpad buffer */ - byte[] scratch; - scratch.length = mtu; + /** * We read with a request of maximum possible @@ -71,7 +74,7 @@ public class TUNAdapter * Former, systemcall read error * Latter, ethernet frame size */ - int status = tunRead(tunFD, cast(char*)scratch.ptr, mtu); + int status = tunRead(tunFD, cast(char*)scratch.ptr, cast(int)scratch.length); if(status < 0) @@ -85,7 +88,7 @@ public class TUNAdapter else { /* Copy the data into their buffer (and of correct length) */ - buffer = scratch[0..status]; + buffer = scratch[0..status].dup; }