eli
eli
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
Alright thanks very much for the info, and yes the semaphores were silly, in my production code I put those in as a bandaid cause I was running into resources limits due to high concurrency on compression, but obviously not needed in the benchmark 🤦
23 replies
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
I just tried setting the Method to WebpEncodingMethod.Fastest and now it takes an average about 500ms but the CPU/ram usage is still high
23 replies
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
@JBSP Hi, I am wondering if imagesharp has a significantly worse performance than imageskia for resizing/converting images to webp or I am doing something wrong here?
23 replies
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
ty
23 replies
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
of skia or sharp?
23 replies
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
no worries, I'm just gonna drop in imageskia in prod and see if that fixes things lol
23 replies
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
I ran the test on m2 macbook air with 16gb of ram btw, the node prod is running on has 4vcpus and 8gb ram
23 replies
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
does imagesharp just suck for this kind of thing? lol
23 replies
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
@Waffle Whisperer if you could take a look I would appreciate it
23 replies
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
No description
23 replies
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
No description
23 replies
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
No description
23 replies
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
No description
23 replies
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
async Task Worker(string imagePath, SemaphoreSlim semaphore)
{
while (true)
{
var sw = Stopwatch.StartNew();
var stream = new FileStream(imagePath, FileMode.Open);
await CompressSkiaSharp(stream, semaphore);
sw.Stop();
Console.WriteLine($"Elapsed: {sw.ElapsedMilliseconds}ms");
}

}

async Task Main()
{
var semaphore = new SemaphoreSlim(50);
var tasks = new List<Task>();
var imagePath =
@"/Users/eli/RiderProjects/ImageSharpProfilingFr/ImageSharpProfilingFr/qa3w10439tyq47tt0w144te9fi247hiyfer56poy7r3ae134h19EwpQs625GEhO38AGUyrU54yEwfG5w6hE34OE5Q9DOQ0hO1EhD.jpg";

for (var i = 0; i < 50; i++)
{
tasks.Add(Worker(imagePath, semaphore));
}

await Task.WhenAll(tasks);
}

Main().Wait();
async Task Worker(string imagePath, SemaphoreSlim semaphore)
{
while (true)
{
var sw = Stopwatch.StartNew();
var stream = new FileStream(imagePath, FileMode.Open);
await CompressSkiaSharp(stream, semaphore);
sw.Stop();
Console.WriteLine($"Elapsed: {sw.ElapsedMilliseconds}ms");
}

}

async Task Main()
{
var semaphore = new SemaphoreSlim(50);
var tasks = new List<Task>();
var imagePath =
@"/Users/eli/RiderProjects/ImageSharpProfilingFr/ImageSharpProfilingFr/qa3w10439tyq47tt0w144te9fi247hiyfer56poy7r3ae134h19EwpQs625GEhO38AGUyrU54yEwfG5w6hE34OE5Q9DOQ0hO1EhD.jpg";

for (var i = 0; i < 50; i++)
{
tasks.Add(Worker(imagePath, semaphore));
}

await Task.WhenAll(tasks);
}

Main().Wait();
23 replies
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
async Task<Stream> CompressSkiaSharp(Stream imageStream, SemaphoreSlim semaphore, int maxWidth = 1000, int maxHeight = 1000)
{
try
{
await semaphore.WaitAsync();
imageStream.Position = 0;

var outputStream = new MemoryStream();

// Load the image data
using var original = SKBitmap.Decode(imageStream);
if (original == null) throw new InvalidOperationException("Failed to load image");

var currentWidth = original.Width;
var currentHeight = original.Height;

// Calculate new dimensions while maintaining aspect ratio
var newMaxWidth = Math.Min(currentWidth, maxWidth);
var newMaxHeight = Math.Min(currentHeight, maxHeight);

var ratioX = (double)newMaxWidth / currentWidth;
var ratioY = (double)newMaxHeight / currentHeight;
var ratio = Math.Min(ratioX, ratioY);

var newWidth = (int)(currentWidth * ratio);
var newHeight = (int)(currentHeight * ratio);

// Create resized bitmap
using var resized = original.Resize(new SKImageInfo(newWidth, newHeight), new SKSamplingOptions());
if (resized == null) throw new InvalidOperationException("Failed to resize image");

// Convert bitmap to image for encoding
using var image = SKImage.FromBitmap(resized);

// Encode as WebP
using var data = image.Encode(SKEncodedImageFormat.Webp, 100);

// Write to output stream
data.SaveTo(outputStream);
outputStream.Position = 0;

return outputStream;
}
finally
{
semaphore.Release();
}
}
async Task<Stream> CompressSkiaSharp(Stream imageStream, SemaphoreSlim semaphore, int maxWidth = 1000, int maxHeight = 1000)
{
try
{
await semaphore.WaitAsync();
imageStream.Position = 0;

var outputStream = new MemoryStream();

// Load the image data
using var original = SKBitmap.Decode(imageStream);
if (original == null) throw new InvalidOperationException("Failed to load image");

var currentWidth = original.Width;
var currentHeight = original.Height;

// Calculate new dimensions while maintaining aspect ratio
var newMaxWidth = Math.Min(currentWidth, maxWidth);
var newMaxHeight = Math.Min(currentHeight, maxHeight);

var ratioX = (double)newMaxWidth / currentWidth;
var ratioY = (double)newMaxHeight / currentHeight;
var ratio = Math.Min(ratioX, ratioY);

var newWidth = (int)(currentWidth * ratio);
var newHeight = (int)(currentHeight * ratio);

// Create resized bitmap
using var resized = original.Resize(new SKImageInfo(newWidth, newHeight), new SKSamplingOptions());
if (resized == null) throw new InvalidOperationException("Failed to resize image");

// Convert bitmap to image for encoding
using var image = SKImage.FromBitmap(resized);

// Encode as WebP
using var data = image.Encode(SKEncodedImageFormat.Webp, 100);

// Write to output stream
data.SaveTo(outputStream);
outputStream.Position = 0;

return outputStream;
}
finally
{
semaphore.Release();
}
}
23 replies
CC#
Created by eli on 1/25/2025 in #help
ImageSharp slow asf for some reason
async Task<Stream> CompressImageSharp(Stream imageStream, SemaphoreSlim semaphore, int maxWidth=1000, int maxHeight=1000)
{
try
{
await semaphore.WaitAsync();
imageStream.Position = 0; // Reset the stream position in case the stream has already been ready before.

var outputStream = new MemoryStream();
using var image = await Image.LoadAsync(imageStream);

var currentWidth = image.Width;
var currentHeight = image.Height;

var newMaxWidth = Math.Min(currentWidth, maxWidth);
var newMaxHeight = Math.Min(currentHeight, maxHeight);

var resizeOptions = new ResizeOptions
{
Size = new Size(newMaxWidth, newMaxHeight),
};

image.Mutate(x => x.Resize(resizeOptions));

var encoder = new WebpEncoder
{
Quality = 100,
FileFormat = WebpFileFormatType.Lossy
};

await image.SaveAsync(outputStream, encoder);

outputStream.Position = 0;

return outputStream;
}
finally
{
semaphore.Release();
}
}
async Task<Stream> CompressImageSharp(Stream imageStream, SemaphoreSlim semaphore, int maxWidth=1000, int maxHeight=1000)
{
try
{
await semaphore.WaitAsync();
imageStream.Position = 0; // Reset the stream position in case the stream has already been ready before.

var outputStream = new MemoryStream();
using var image = await Image.LoadAsync(imageStream);

var currentWidth = image.Width;
var currentHeight = image.Height;

var newMaxWidth = Math.Min(currentWidth, maxWidth);
var newMaxHeight = Math.Min(currentHeight, maxHeight);

var resizeOptions = new ResizeOptions
{
Size = new Size(newMaxWidth, newMaxHeight),
};

image.Mutate(x => x.Resize(resizeOptions));

var encoder = new WebpEncoder
{
Quality = 100,
FileFormat = WebpFileFormatType.Lossy
};

await image.SaveAsync(outputStream, encoder);

outputStream.Position = 0;

return outputStream;
}
finally
{
semaphore.Release();
}
}
23 replies
CC#
Created by eli on 12/17/2024 in #help
Contains returning False despite same hashcode and Equals returning true
Just gonna change my implementation to use a List and remember to always check Contains because there isn't really anything meaningful I can generate the hashcode from that won't cause this issue
80 replies
CC#
Created by eli on 12/17/2024 in #help
Contains returning False despite same hashcode and Equals returning true
Ah I understand the issue now EF Gets a product EF then gets the Qc Photo Sets (but not the Qc Photos within them) and adds them to the products set based off the computed hash currently Then EF adds the Qc Photos within the Qc Photo sets and the hashcode is changed but their slot in hashset is based off old hashcode
80 replies
CC#
Created by eli on 12/17/2024 in #help
Contains returning False despite same hashcode and Equals returning true
ty for the help guys
80 replies
CC#
Created by eli on 12/17/2024 in #help
Contains returning False despite same hashcode and Equals returning true
Now to figure out what I should base my hashcodes of off since evverything is properties lol
80 replies