Skip to content

Commit

Permalink
Merge pull request #81 from dragonfruitnetwork/reduce-allocs
Browse files Browse the repository at this point in the history
Reduce allocs
  • Loading branch information
aspriddell authored Jan 29, 2024
2 parents 592f008 + 86c8307 commit 6d9c708
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
<ItemGroup>
<PackageReference Include="DnsClient" Version="1.7.0"/>
<PackageReference Include="DragonFruit.Data.Roslyn" Version="4.0.0" />
<PackageReference Include="DragonFruit.OnionFruit.Web.Worker.Native" Version="0.2.0"/>
<PackageReference Include="Google.Protobuf" Version="3.25.1" />
<PackageReference Include="Grpc.Tools" Version="2.59.0">
<PackageReference Include="DragonFruit.OnionFruit.Web.Worker.Native" Version="0.3.1" />
<PackageReference Include="Google.Protobuf" Version="3.25.2" />
<PackageReference Include="Grpc.Tools" Version="2.60.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="IPAddressRange" Version="6.0.0"/>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="libloc.Access" Version="2023.1220.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Redis.OM" Version="0.5.4" />
<PackageReference Include="Sentry.Extensions.Logging" Version="3.41.2" />
<PackageReference Include="Redis.OM" Version="0.6.1" />
<PackageReference Include="Sentry.Extensions.Logging" Version="3.41.4" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion DragonFruit.OnionFruit.Web.Worker/Native/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class NativeMethods
private const string LibraryName = "onionfruit_worker_native";

[DllImport(LibraryName, EntryPoint = "sort_network_entries", CallingConvention = CallingConvention.Cdecl)]
public static extern void PerformNetworkSort([In, MarshalAs(UnmanagedType.LPArray)] NetworkEntry[] entries, nint length, out NetworkSortResult result);
public static extern unsafe void PerformNetworkSort([In, MarshalAs(UnmanagedType.LPArray)] NetworkEntry[] entries, nint length, out NetworkSortResult result);

[DllImport(LibraryName, EntryPoint = "free_sort_result", CallingConvention = CallingConvention.Cdecl)]
public static extern void ClearSortResult([In] ref NetworkSortResult ptr);
Expand Down
2 changes: 2 additions & 0 deletions DragonFruit.OnionFruit.Web.Worker/Native/NativeStructs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public struct NetworkEntry
public struct NetworkSortResult
{
public IntPtr v4Entries;
public nint v4Capacity;
public nint v4Count;

public IntPtr v6Entries;
public nint v6Capacity;
public nint v6Count;
}
10 changes: 5 additions & 5 deletions DragonFruit.OnionFruit.Web.Worker/Sources/LocationDbSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public async Task CollectData()
Database = DatabaseLoader.LoadFromStream(dbFileStream);

// start with a relatively large buffer to pass all entries into
var networkList = new List<NetworkEntry>(1_500_000);
var counter = 0;
var networkList = new NetworkEntry[Database.Networks.Count];
var asciiCache = new Dictionary<string, byte[]>(Database.Countries.Count);

try
Expand All @@ -87,10 +88,10 @@ public async Task CollectData()
}

entry.country_code = asciiBuffer;
networkList.Add(entry);
networkList[counter++] = entry;
}

NativeMethods.PerformNetworkSort(networkList.ToArray(), networkList.Count, out var networkSortResult);
NativeMethods.PerformNetworkSort(networkList, counter, out var networkSortResult);

try
{
Expand All @@ -115,14 +116,13 @@ public async Task CollectData()
}

asciiCache.Clear();
networkList.Clear();
}
}

private static unsafe NetworkAddressRangeInfo[] GetIPv4AddressRanges(IntPtr start, nint length)
{
var v4NetworkRanges = new NetworkAddressRangeInfo[length];
Span<byte> v4AddressBytes = stackalloc byte[4];
var v4NetworkRanges = new NetworkAddressRangeInfo[length];

// collect all data and process into something useful
for (var i = 0; i < length; i++)
Expand Down
4 changes: 2 additions & 2 deletions DragonFruit.OnionFruit.Web/DragonFruit.OnionFruit.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JsonPath.Net" Version="0.6.7" />
<PackageReference Include="JsonPath.Net" Version="0.7.1" />
<PackageReference Include="libloc.Access" Version="2023.1220.0" />
<PackageReference Include="System.Text.Json" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions NuGet.config
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>

<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="github" value="https://nuget.pkg.github.com/dragonfruitnetwork/index.json" />
<add key="dragonfruit" value="https://nuget.pkg.github.com/dragonfruitnetwork/index.json" />
</packageSources>
<allowedPackageSources>
<add key="DragonFruit.OnionFruit.Web.Worker.Native" value="github" />
<add key="DragonFruit.OnionFruit.Web.Worker.Native" value="dragonfruit" />
</allowedPackageSources>
</configuration>
4 changes: 3 additions & 1 deletion native/src/src/interop_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ pub struct InteropNetworkRange<T> {
#[derive(Clone, Copy)]
pub struct InteropNetworkSortResult {
pub v4entries: *mut InteropNetworkRange<u32>,
pub v4capacity: usize, // usually matches v4count
pub v4count: usize,

pub v6entries: *mut InteropNetworkRange<u128>,
pub v6count: usize
pub v6capacity: usize, // usually matches v6Count
pub v6count: usize,
}
9 changes: 4 additions & 5 deletions native/src/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ pub extern "cdecl" fn sort_network_entries(ptr: *const c_void, length: usize, ou
v4results.shrink_to_fit();
v6results.shrink_to_fit();

assert!(v4results.len() == v4results.capacity());
assert!(v6results.len() == v6results.capacity());

unsafe {
*out = InteropNetworkSortResult {
v4entries: v4results.as_mut_ptr(),
v4capacity: v4results.capacity(),
v4count: v4results.len(),

v6entries: v6results.as_mut_ptr(),
v6capacity: v6results.capacity(),
v6count: v6results.len()
};
}
Expand All @@ -91,8 +90,8 @@ pub extern "cdecl" fn sort_network_entries(ptr: *const c_void, length: usize, ou
#[no_mangle]
pub unsafe extern "cdecl" fn free_sort_result(ptr: *const InteropNetworkSortResult) {
let result = *ptr;
drop(Vec::<InteropNetworkRange<u32>>::from_raw_parts(result.v4entries, result.v4count, result.v4count));
drop(Vec::<InteropNetworkRange<u128>>::from_raw_parts(result.v6entries, result.v6count, result.v6count));
mem::drop(Vec::<InteropNetworkRange<u32>>::from_raw_parts(result.v4entries, result.v4count, result.v4capacity));
mem::drop(Vec::<InteropNetworkRange<u128>>::from_raw_parts(result.v6entries, result.v6count, result.v6capacity));
}

fn create_network_list<T>(map: RangeInclusiveMap<T, NetworkInfo>, range_value_selector: fn(T) -> T) -> Vec::<InteropNetworkRange<T>> where T : Copy, T : Eq, T : PartialEq, T : Ord, T : PartialOrd, T : StepFns<T> {
Expand Down

0 comments on commit 6d9c708

Please sign in to comment.