More code cleanup idk

Signed-off-by: Sam Therapy <sam@samtherapy.net>
This commit is contained in:
Sam Therapy 2023-10-23 16:19:46 +02:00
parent ee1c21fd2a
commit 5dbc9eb80c
Signed by: sam
GPG key ID: 4D8B07C18F31ACBD
9 changed files with 68 additions and 176 deletions

View file

@ -1,11 +1,11 @@
# Xdg
----
[![Drone Build Status](https://ci.git.froth.zone/api/badges/sam/Xdg.Net/status.svg)](https://ci.git.froth.zone/sam/Xdg.Net)
# Xdg.Net
[![Drone build status](https://img.shields.io/drone/build/sam/Xdg.Net?server=https%3A%2F%2Fci.git.froth.zone)](https://ci.git.froth.zone/sam/Xdg.Net)
[![GitHub Actions Build](https://github.com/xdg-net/Xdg.Net/actions/workflows/build-test.yaml/badge.svg)](https://github.com/xdg-net/Xdg.Net/actions/workflows/build-test.yaml)
[![GitHub Actions CodeQL](https://github.com/xdg-net/Xdg.Net/actions/workflows/codeql.yaml/badge.svg)](https://github.com/xdg-net/Xdg.Net/actions/workflows/codeql.yaml)
[![Nuget](https://img.shields.io/nuget/v/Xdg.Directories)](https://www.nuget.org/packages/Xdg.Directories/)
A ***small***, ***fast*** and ***portable*** .NET implementation of the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) and XDG user directories for Windows, MacOS and Linux/FreeBSD.
A ***small*** (the .dll is only 11 KB), [***fast***](./src/Xdg.Benchmarks/README.md) and ***portable*** (Completely supports .NET Standard 2.0 and even NativeAOT!) .NET implementation of the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) and XDG user directories for Windows, MacOS and Linux/FreeBSD.
## Default Locations

View file

@ -1,14 +1,13 @@
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
open BenchmarkDotNet.Jobs
open BenchmarkDotNet.Configs
open Xdg.Directories
[<MemoryDiagnoser>]
#if WINDOWS
[<SimpleJob(RuntimeMoniker.Net481)>]
#endif
[<SimpleJob(RuntimeMoniker.Net70)>]
[<SimpleJob(RuntimeMoniker.Net70, baseline=true)>]
// [<SimpleJob(RuntimeMoniker.NativeAot70)>]
type Benchmarks() =
// Base Directory
@ -55,5 +54,4 @@ type Benchmarks() =
[<Benchmark>]
member _.Fonts () = Other.Fonts
let config = DefaultConfig.Instance.WithOptions(ConfigOptions.DisableOptimizationsValidator)
BenchmarkRunner.Run<Benchmarks>(config) |> ignore
BenchmarkRunner.Run<Benchmarks>() |> ignore

View file

@ -0,0 +1,36 @@
# Xdg.Benchmarks
Here lies the benchmarks for all [Xdg.Directories](../../README.md) functions.
## Results
```
BenchmarkDotNet v0.13.8, Gentoo Linux
AMD Ryzen 9 5900X, 1 CPU, 24 logical and 12 physical cores
.NET SDK 7.0.402
[Host] : .NET 7.0.12 (7.0.1223.47720), X64 RyuJIT AVX2 DEBUG
.NET 7.0 : .NET 7.0.12 (7.0.1223.47720), X64 RyuJIT AVX2
Job=.NET 7.0 Runtime=.NET 7.0
```
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio |
|------------- |---------:|--------:|---------:|------:|--------:|-------:|----------:|------------:|
| DataHome | 198.0 ns | 1.80 ns | 1.60 ns | 1.00 | 0.00 | 0.0043 | 72 B | 1.00 |
| ConfigHome | 225.8 ns | 1.08 ns | 1.01 ns | 1.14 | 0.01 | 0.0038 | 64 B | 0.89 |
| StateHome | 229.9 ns | 2.05 ns | 1.92 ns | 1.16 | 0.02 | 0.0043 | 72 B | 1.00 |
| BinHome | 215.0 ns | 4.10 ns | 3.83 ns | 1.09 | 0.02 | 0.0038 | 64 B | 0.89 |
| DataDirs | 181.7 ns | 3.54 ns | 3.48 ns | 0.92 | 0.02 | 0.0024 | 40 B | 0.56 |
| ConfigDirs | 179.8 ns | 1.57 ns | 1.39 ns | 0.91 | 0.01 | 0.0019 | 32 B | 0.44 |
| CacheHome | 178.7 ns | 1.10 ns | 0.98 ns | 0.90 | 0.01 | 0.0033 | 56 B | 0.78 |
| RuntimeDir | 207.9 ns | 1.67 ns | 1.57 ns | 1.05 | 0.01 | 0.0033 | 56 B | 0.78 |
| DesktopDir | 382.8 ns | 2.58 ns | 2.29 ns | 1.93 | 0.02 | 0.0067 | 112 B | 1.56 |
| DownloadDir | 380.5 ns | 4.02 ns | 3.76 ns | 1.92 | 0.03 | 0.0067 | 112 B | 1.56 |
| DocumentsDir | 378.6 ns | 1.44 ns | 1.34 ns | 1.91 | 0.02 | 0.0067 | 112 B | 1.56 |
| MusicDir | 372.5 ns | 2.78 ns | 2.32 ns | 1.88 | 0.02 | 0.0062 | 104 B | 1.44 |
| PicturesDir | 379.9 ns | 2.51 ns | 1.96 ns | 1.92 | 0.02 | 0.0067 | 112 B | 1.56 |
| VideosDir | 382.0 ns | 3.20 ns | 2.99 ns | 1.93 | 0.03 | 0.0062 | 104 B | 1.44 |
| TemplatesDir | 390.3 ns | 3.43 ns | 3.04 ns | 1.97 | 0.02 | 0.0067 | 112 B | 1.56 |
| PublicDir | 371.9 ns | 4.49 ns | 3.75 ns | 1.88 | 0.02 | 0.0062 | 104 B | 1.44 |
| Home | 151.2 ns | 2.96 ns | 3.53 ns | 0.77 | 0.02 | 0.0029 | 48 B | 0.67 |
| Applications | 403.2 ns | 7.98 ns | 12.42 ns | 2.03 | 0.08 | 0.0219 | 368 B | 5.11 |
| Fonts | 615.7 ns | 9.39 ns | 8.79 ns | 3.12 | 0.05 | 0.0267 | 448 B | 6.22 |

View file

@ -9,81 +9,21 @@ namespace Xdg.Directories.FFI;
internal static partial class Exports
{
[UnmanagedCallersOnly(EntryPoint = "xdg_data_home")]
public static IntPtr DataHome()
{
try
{
return StringToPtr(BaseDirectory.DataHome);
}
catch
{
return IntPtr.Zero;
}
}
public static IntPtr DataHome() => StringToPtr(BaseDirectory.DataHome);
[UnmanagedCallersOnly(EntryPoint = "xdg_config_home")]
public static IntPtr ConfigHome()
{
try
{
return StringToPtr(BaseDirectory.ConfigHome);
}
catch
{
return IntPtr.Zero;
}
}
public static IntPtr ConfigHome() => StringToPtr(BaseDirectory.ConfigHome);
[UnmanagedCallersOnly(EntryPoint = "xdg_state_home")]
public static IntPtr StateHome()
{
try
{
return StringToPtr(BaseDirectory.StateHome);
}
catch
{
return IntPtr.Zero;
}
}
public static IntPtr StateHome() => StringToPtr(BaseDirectory.StateHome);
[UnmanagedCallersOnly(EntryPoint = "xdg_bin_home")]
public static IntPtr BinHome()
{
try
{
return StringToPtr(BaseDirectory.BinHome);
}
catch
{
return IntPtr.Zero;
}
}
public static IntPtr BinHome() => StringToPtr(BaseDirectory.BinHome);
[UnmanagedCallersOnly(EntryPoint = "xdg_cache_home")]
public static IntPtr CacheHome()
{
try
{
return StringToPtr(BaseDirectory.CacheHome);
}
catch
{
return IntPtr.Zero;
}
}
public static IntPtr CacheHome() => StringToPtr(BaseDirectory.CacheHome);
[UnmanagedCallersOnly(EntryPoint = "xdg_runtime_dir")]
public static IntPtr RuntimeDir()
{
try
{
return StringToPtr(BaseDirectory.RuntimeDir);
}
catch
{
return IntPtr.Zero;
}
}
public static IntPtr RuntimeDir() => StringToPtr(BaseDirectory.RuntimeDir);
}
#endif

View file

@ -6,16 +6,6 @@ namespace Xdg.Directories.FFI;
internal static partial class Exports
{
[UnmanagedCallersOnly(EntryPoint = "xdg_user_home")]
public static IntPtr UserHome()
{
try
{
return StringToPtr(Other.Home);
}
catch
{
return IntPtr.Zero;
}
}
public static IntPtr UserHome() => StringToPtr(Other.Home);
}
#endif

View file

@ -6,107 +6,27 @@ namespace Xdg.Directories.FFI;
internal static partial class Exports
{
[UnmanagedCallersOnly(EntryPoint = "xdg_desktop_dir")]
public static IntPtr Desktop()
{
try
{
return StringToPtr(UserDirectory.DesktopDir);
}
catch
{
return IntPtr.Zero;
}
}
public static IntPtr Desktop() => StringToPtr(UserDirectory.DesktopDir);
[UnmanagedCallersOnly(EntryPoint = "xdg_download_dir")]
public static IntPtr Download()
{
try
{
return StringToPtr(UserDirectory.DownloadDir);
}
catch
{
return IntPtr.Zero;
}
}
public static IntPtr Download() => StringToPtr(UserDirectory.DownloadDir);
[UnmanagedCallersOnly(EntryPoint = "xdg_documents_dir")]
public static IntPtr Documents()
{
try
{
return StringToPtr(UserDirectory.DocumentsDir);
}
catch
{
return IntPtr.Zero;
}
}
public static IntPtr Documents() => StringToPtr(UserDirectory.DocumentsDir);
[UnmanagedCallersOnly(EntryPoint = "xdg_music_dir")]
public static IntPtr Music()
{
try
{
return StringToPtr(UserDirectory.MusicDir);
}
catch
{
return IntPtr.Zero;
}
}
public static IntPtr Music() => StringToPtr(UserDirectory.MusicDir);
[UnmanagedCallersOnly(EntryPoint = "xdg_pictures_dir")]
public static IntPtr Pictures()
{
try
{
return StringToPtr(UserDirectory.PicturesDir);
}
catch
{
return IntPtr.Zero;
}
}
public static IntPtr Pictures() => StringToPtr(UserDirectory.PicturesDir);
[UnmanagedCallersOnly(EntryPoint = "xdg_videos_dir")]
public static IntPtr Videos()
{
try
{
return StringToPtr(UserDirectory.VideosDir);
}
catch
{
return IntPtr.Zero;
}
}
public static IntPtr Videos() => StringToPtr(UserDirectory.VideosDir);
[UnmanagedCallersOnly(EntryPoint = "xdg_templates_dir")]
public static IntPtr Templates()
{
try
{
return StringToPtr(UserDirectory.TemplatesDir);
}
catch
{
return IntPtr.Zero;
}
}
public static IntPtr Templates() => StringToPtr(UserDirectory.TemplatesDir);
[UnmanagedCallersOnly(EntryPoint = "xdg_publicshare_dir")]
public static IntPtr Public()
{
try
{
return StringToPtr(UserDirectory.PublicDir);
}
catch
{
return IntPtr.Zero;
}
}
public static IntPtr Public() => StringToPtr(UserDirectory.PublicDir);
}
#endif

View file

@ -48,10 +48,18 @@ xdg_directory xdg_templates_dir(void);
xdg_directory xdg_publicshare_dir(void);
// A convenience function to get the string representation of a directory.
// You still need to call xdg_free on the original directory object.
static inline char* xdg_to_string(xdg_directory dir) { return (char* dir) };
// Frees the directory object.
//
// This must be called after the directory is no longer needed.
// Note: the CLR will leak about a megabyte of memory by existing.
void xdg_free(xdg_directory);
#endif // !XDG_DOTNET_H
#ifdef __cplusplus
}
#endif
#endif // !XDG_DOTNET_H

View file

@ -15,7 +15,7 @@ static internal class Helpers
/// </summary>
UnixLike,
/// <summary>
/// Anything not supported by .NET
/// Anything not supported by the library (Android/iOS, etc.)
/// </summary>
Other
}

View file

@ -7,7 +7,7 @@
<TargetFrameworks>net7.0;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<IsPackable>true</IsPackable>
<IsTrimmable>true</IsTrimmable>
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<EnableNETAnalyzers>True</EnableNETAnalyzers>