everythingio icon indicating copy to clipboard operation
everythingio copied to clipboard

Refactor GetResultFullPathName to use char array

Open NWoodsman opened this issue 1 year ago • 2 comments

https://learn.microsoft.com/en-us/dotnet/framework/interop/default-marshalling-for-strings

https://learn.microsoft.com/en-us/dotnet/standard/native-interop/best-practices#string-parameters

The above Microsoft docs include canonical examples to reduce allocations when marshalling native char arrays into managed code.

This commit should reduce up to 4 allocations per extern call. Which should result in huge savings over the many iterations that Search will generate.

See above commit message. I tested the code and it works, I'm just not familiar enough with C to guarantee that CharSet.Unicode results in native char arrays that are two bytes per char, i.e. proper Utf-16 char that C# expects.

That said, this should be a great reduction in allocations.

NWoodsman avatar Jul 20 '24 07:07 NWoodsman

With more sleep I realized this PR reduces allocations but now we drastically changed the Search() signature to putting the responsibility on the user for determining how many characters to ask for. In a sense, this is an internal implementation detail yet one that has drastic negative effects if the user doesn't predetermine the length...so it makes sense to accept the PR and change the signature but that's just one opinion.

NWoodsman avatar Jul 20 '24 21:07 NWoodsman

Ok, we can assume Windows file path length to be max 260, so we use that as the buffer size. I will need to research how Everything handles large path values, otherwise I think this is a sensible first step.

NWoodsman avatar Jul 20 '24 21:07 NWoodsman