Skip to content

Commit 2df486c

Browse files
committed
- be more specific about native types on different platforms
- handle bad image exception - draft benchmarks
1 parent e2ff671 commit 2df486c

File tree

8 files changed

+108
-29
lines changed

8 files changed

+108
-29
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.13.4" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\IronCompress\IronCompress.csproj" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.IO.Compression;
2+
using System.Text;
3+
using BenchmarkDotNet.Attributes;
4+
using BenchmarkDotNet.Running;
5+
using IronCompress;
6+
7+
BenchmarkRunner.Run<SpeedBenchmark>();
8+
9+
[ShortRunJob]
10+
[MarkdownExporter]
11+
[MemoryDiagnoser]
12+
public class SpeedBenchmark {
13+
private byte[]? _inputBuffer;
14+
15+
[GlobalSetup]
16+
public void Setup() {
17+
_inputBuffer = Encoding.UTF8.GetBytes(string.Join("", Enumerable.Repeat("great compression", 1000)));
18+
}
19+
20+
[Benchmark]
21+
public void SnappyNative() {
22+
var iron = new Iron();
23+
iron.ForcePlatform = Platform.Native;
24+
25+
iron.Compress(Codec.Snappy, _inputBuffer.AsSpan(), null);
26+
}
27+
28+
[Benchmark]
29+
public void SnappyManaged() {
30+
var iron = new Iron();
31+
iron.ForcePlatform = Platform.Managed;
32+
33+
iron.Compress(Codec.Snappy, _inputBuffer.AsSpan(), null);
34+
}
35+
}

managed/IronCompress.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1313
..\docs\README.md = ..\docs\README.md
1414
EndProjectSection
1515
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IronCompress.Benchmarks", "IronCompress.Benchmarks\IronCompress.Benchmarks.csproj", "{FB951E3A-3B24-4BB3-89D0-23B4E6013C86}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
2729
{24CE97A1-3502-481C-AFF9-891D45B6699D}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{24CE97A1-3502-481C-AFF9-891D45B6699D}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{24CE97A1-3502-481C-AFF9-891D45B6699D}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{FB951E3A-3B24-4BB3-89D0-23B4E6013C86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{FB951E3A-3B24-4BB3-89D0-23B4E6013C86}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{FB951E3A-3B24-4BB3-89D0-23B4E6013C86}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{FB951E3A-3B24-4BB3-89D0-23B4E6013C86}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

managed/IronCompress/Iron.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public static bool IsNativeLibraryAvailable {
2525

2626
} catch(DllNotFoundException) {
2727
_isNativeLibraryAvailable = false;
28+
} catch(BadImageFormatException) {
29+
_isNativeLibraryAvailable = false;
2830
}
2931
}
3032

@@ -44,6 +46,10 @@ static bool SupportsNative(Codec c) {
4446
return IsNativeLibraryAvailable && Native.is_supported((int)c);
4547
}
4648

49+
/// <summary>
50+
/// Set to force specific platform. Used mostly in benchmarking tests, prefer not to set.
51+
/// </summary>
52+
public Platform? ForcePlatform { get; set; }
4753

4854
/// <summary>
4955
/// Create iron instance
@@ -59,6 +65,14 @@ public IronCompressResult Compress(
5965
int? outputLength = null,
6066
CompressionLevel compressionLevel = CompressionLevel.Optimal) {
6167

68+
if(ForcePlatform != null) {
69+
if(ForcePlatform == Platform.Native) {
70+
return NativeCompressOrDecompress(true, codec, input, compressionLevel, outputLength);
71+
} else if(ForcePlatform == Platform.Managed) {
72+
return ManagedCompressOrDecompress(true, codec, input, compressionLevel, outputLength);
73+
}
74+
}
75+
6276
if(SupportsNative(codec))
6377
return NativeCompressOrDecompress(true, codec, input, compressionLevel, outputLength);
6478

managed/IronCompress/Platform.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace IronCompress {
2+
public enum Platform {
3+
Native = 0,
4+
Managed = 1
5+
}
6+
}

native/api.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ bool compress_lz4(
226226
return false;
227227
}
228228

229-
bool compress(bool compress, int codec, char* input_buffer, int input_buffer_size,
230-
char* output_buffer, int* output_buffer_size, int compression_level) {
229+
bool compress(bool compress, int32_t codec, char* input_buffer, int32_t input_buffer_size,
230+
char* output_buffer, int32_t* output_buffer_size, int32_t compression_level) {
231231
switch(codec) {
232232
case 1:
233233
return compress_snappy(compress, input_buffer, input_buffer_size, output_buffer, output_buffer_size);
@@ -245,7 +245,7 @@ bool compress(bool compress, int codec, char* input_buffer, int input_buffer_siz
245245
}
246246
}
247247

248-
bool is_supported(int codec) {
248+
bool is_supported(int32_t codec) {
249249
return codec == 1 ||
250250
codec == 2 ||
251251
// not 3

native/api.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <stdint.h>
4+
35
#ifdef _WIN32
46
# ifdef WIN_EXPORT
57
# define EXPORTED __declspec( dllexport )
@@ -25,19 +27,19 @@ extern "C"
2527
*/
2628
EXPORTED bool compress(
2729
bool compress,
28-
int codec,
30+
int32_t codec,
2931
char* input_buffer,
30-
int input_buffer_size,
32+
int32_t input_buffer_size,
3133
char* output_buffer,
32-
int* output_buffer_size,
33-
int compression_level);
34+
int32_t* output_buffer_size,
35+
int32_t compression_level);
3436

3537
/**
3638
* @brief Checks if particular codec is supported
3739
* @param codec
3840
* @return true if supported, false otherwise
3941
*/
40-
EXPORTED bool is_supported(int codec);
42+
EXPORTED bool is_supported(int32_t codec);
4143

4244
/**
4345
* @brief Used to just ping the library to test it's available at all

native/test.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,31 @@
33

44
using namespace std;
55

6-
bool run(int method, char* buffer, size_t buffer_length)
7-
{
8-
int len{ 0 };
9-
bool ok = compress(true, method, buffer, buffer_length, nullptr, &len, 2);
6+
bool run(int method, char* buffer, size_t buffer_length) {
7+
int32_t len{0};
8+
bool ok = compress(true, method, buffer, buffer_length, nullptr, &len, 2);
109

11-
vector<char> compressed;
12-
compressed.resize(len);
10+
vector<char> compressed;
11+
compressed.resize(len);
1312

14-
ok = compress(true, method, buffer, buffer_length,
15-
&compressed[0], &len, 2);
13+
ok = compress(true, method, buffer, buffer_length,
14+
&compressed[0], &len, 2);
1615

17-
vector<byte> decompressed;
18-
int bl1 = buffer_length;
19-
decompressed.resize(buffer_length);
20-
ok = compress(false, method, &compressed[0], len,
21-
(char*)&decompressed[0], &bl1, 2);
16+
vector<byte> decompressed;
17+
int32_t bl1 = buffer_length;
18+
decompressed.resize(buffer_length);
19+
ok = compress(false, method, &compressed[0], len,
20+
(char*)&decompressed[0], &bl1, 2);
2221

23-
return ok;
22+
return ok;
2423
}
2524

26-
int main()
27-
{
28-
char bytes[] = {'a', 'b', 'c'};
25+
int main() {
26+
char bytes[] = {'a', 'b', 'c'};
2927

30-
//run(1, bytes, 3);
31-
//run(2, bytes, 3);
32-
run(4, bytes, 3);
28+
//run(1, bytes, 3);
29+
//run(2, bytes, 3);
30+
run(4, bytes, 3);
3331

34-
return 0;
32+
return 0;
3533
}

0 commit comments

Comments
 (0)