Span<T>, Memory<T> - slog(์™„๋ฃŒ)

์กฐ๊ธˆ ๋” ๋‹ค๋ฅธ ์ ‘๊ทผ์„ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, MemoryMappedFile์„ ์ด์šฉํ•ด Span<byte>์„ ํš๋“ํ•  ์ˆ˜ ๋งŒ ์žˆ๋‹ค๋ฉด, ์ข€ ๋” ํšจ์œจ์ ์ธ ๋Œ€๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์„๊บผ๋ผ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

ํ™•์ธํ•œ ๊ฒฐ๊ณผ, ์–ด์ฉ” ์ˆ˜ ์—†์ด unsafe์˜์—ญ์„ ์‚ฌ์šฉํ•ด์•ผ ํ–ˆ๊ณ , decimal์ด ๊ณ ์ • ๋ฐฐ์—ด์ด ์•ˆ๋œ๋‹ค๋Š” ์‚ฌ์‹ค๋„ ์•Œ๊ฒŒ ๋˜์—ˆ์ง€๋งŒ, ์–ด์จŒ๋“  ์ƒ๋‹นํžˆ ๋น ๋ฅธ ์†๋„๋กœ ๋ชฉ๋ก(Span<์‹ค์‹œ๊ฐ„์‹œ์„ธ2>)์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

void ์ž„์˜์‹œ์„ธ๋ชฉ๋ก_์ฝ๊ธฐ2()
{
    var filename = "์‹ค์‹œ๊ฐ„์‹œ์„ธ.dat";
    var fileLength = new FileInfo(filename).Length;

    using var mmf = MemoryMappedFile.CreateFromFile(filename, FileMode.Open);
    using var accessor = mmf.CreateViewAccessor();

    Span<byte> memory;
    unsafe
    {
        byte* ptr = null;
        accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);
        ptr += accessor.PointerOffset;

        //memory = new Span<byte>(ptr, (int)accessor.SafeMemoryMappedViewHandle.ByteLength);
        memory = new Span<byte>(ptr, (int)fileLength);
    }


    var ์‹ค์‹œ๊ฐ„์‹œ์„ธ๋ชฉ๋ก = MemoryMarshal.Cast<byte, ์‹ค์‹œ๊ฐ„์‹œ์„ธ2>(memory);
    foreach (var item in ์‹ค์‹œ๊ฐ„์‹œ์„ธ๋ชฉ๋ก)
    {
        ;
    }

    accessor.SafeMemoryMappedViewHandle.ReleasePointer();
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct ์‹ค์‹œ๊ฐ„์‹œ์„ธ2
{
    public long ์ฒด๊ฒฐ์‹œ๊ฐ;
    public decimal ์ฒด๊ฒฐ๊ฐ€;
    public decimal ์ฒด๊ฒฐ์ˆ˜๋Ÿ‰;
    public fixed int _๋งค์ˆ˜ํ˜ธ๊ฐ€์ž”๋Ÿ‰[4 * 5];
    public fixed int _๋งค๋„ํ˜ธ๊ฐ€์ž”๋Ÿ‰[4 * 5];
}
1 Like