디시인사이드 갤러리

갤러리 이슈박스, 최근방문 갤러리

갤러리 본문 영역

지피티 5로 코드 뱉어봤는데 여전히 좀 아쉽

ㅆㅇㅆ(124.216) 2025.08.08 10:17:06
조회 71 추천 0 댓글 0

using System;

using System.Buffers;

using System.Collections.Concurrent;

using System.Collections.Generic;

using System.Linq;

using System.Threading;

using System.Threading.Tasks;


/// <summary>

/// 데이터 처리 파이프라인 예제

/// - OOP: 클래스/인터페이스로 모듈화

/// - FP: 불변 데이터, 순수 함수 처리

/// - DOP: 캐시 친화적 배열 처리

/// - Thread-safe 이벤트 시스템

/// </summary>

namespace HighPerformancePipeline

{

    #region Interfaces

    public interface IDataProcessor<TInput, TOutput>

    {

        Task ProcessAsync(IEnumerable<TInput> inputData, CancellationToken token = default);

        event Action<IReadOnlyList<TOutput>> OnProcessingCompleted;

    }

    #endregion


    #region Immutable Data

    /// <summary>

    /// 불변 데이터 레코드 (FP 스타일)

    /// </summary>

    public readonly record struct ProcessedResult(int Id, double Value);

    #endregion


    #region Implementation

    public class ParallelDataProcessor : IDataProcessor<int, ProcessedResult>

    {

        public event Action<IReadOnlyList<ProcessedResult>> OnProcessingCompleted;


        private readonly int batchSize;

        private readonly Func<int, double> transformation;


        public ParallelDataProcessor(int batchSize, Func<int, double> transformation)

        {

            if (batchSize <= 0) throw new ArgumentOutOfRangeException(nameof(batchSize));

            this.batchSize = batchSize;

            this.transformation = transformation ?? throw new ArgumentNullException(nameof(transformation));

        }


        public async Task ProcessAsync(IEnumerable<int> inputData, CancellationToken token = default)

        {

            if (inputData == null) throw new ArgumentNullException(nameof(inputData));


            // Thread-safe 컬렉션

            ConcurrentBag<ProcessedResult> results = new ConcurrentBag<ProcessedResult>();


            // 데이터 분할 (DOP - 캐시 친화적 배치)

            int[][] batches = inputData

                .Select((value, index) => new { value, index })

                .GroupBy(x => x.index / batchSize)

                .Select(g => g.Select(x => x.value).ToArray())

                .ToArray();


            await Task.Run(() =>

            {

                Parallel.ForEach(batches, new ParallelOptions { CancellationToken = token }, batch =>

                {

                    // ArrayPool 사용 (GC 압박 감소)

                    double[] tempBuffer = ArrayPool<double>.Shared.Rent(batch.Length);


                    try

                    {

                        for (int i = 0; i < batch.Length; i++)

                        {

                            tempBuffer[i] = transformation(batch[i]); // 순수 함수 처리

                        }


                        for (int i = 0; i < batch.Length; i++)

                        {

                            results.Add(new ProcessedResult(batch[i], tempBuffer[i]));

                        }

                    }

                    finally

                    {

                        ArrayPool<double>.Shared.Return(tempBuffer);

                    }

                });

            }, token);


            OnProcessingCompleted?.Invoke(results.OrderBy(r => r.Id).ToList());

        }

    }

    #endregion


    #region Example Usage

    public static class Program

    {

        public static async Task Main()

        {

            // 변환 로직: sqrt 연산 + 부동소수점 연산 예시

            ParallelDataProcessor processor = new ParallelDataProcessor(

                batchSize: 1024,

                transformation: value => Math.Sqrt(value) * 1.2345

            );


            processor.OnProcessingCompleted += results =>

            {

                Console.WriteLine($"[Completed] {results.Count} items processed.");

                Console.WriteLine($"First 5 results: {string.Join(", ", results.Take(5))}");

            };


            int[] sampleData = Enumerable.Range(1, 100_000).ToArray();

            await processor.ProcessAsync(sampleData);

        }

    }

    #endregion

}



테스트 밴치 이렇게 뱉어냈는데

내가 품을 들여서

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace HighPerformancePipeline
{
    /// <summary>
    /// 제네릭 데이터 처리기
    /// </summary>
    public interface IDataProcessor<TInput, TOutput>
    {
        Task ProcessAsync(IEnumerable<TInput> inputData, CancellationToken token = default);
        event Action<IReadOnlyList<TOutput>> OnProcessingCompleted;
    }

    /// <summary>
    /// 고성능 제네릭 병렬 처리기
    /// </summary>
    public class ParallelDataProcessor<TInput, TOutput> : IDataProcessor<TInput, TOutput>
    {
        public event Action<IReadOnlyList<TOutput>> OnProcessingCompleted;

        private readonly int batchSize;
        private readonly Func<TInput, TOutput> transformation;

        public ParallelDataProcessor(int batchSize, Func<TInput, TOutput> transformation)
        {
            if (batchSize <= 0) throw new ArgumentOutOfRangeException(nameof(batchSize));
            this.batchSize = batchSize;
            this.transformation = transformation ?? throw new ArgumentNullException(nameof(transformation));
        }

        public async Task ProcessAsync(IEnumerable<TInput> inputData, CancellationToken token = default)
        {
            if (inputData == null) throw new ArgumentNullException(nameof(inputData));

            ConcurrentBag<TOutput> results = new ConcurrentBag<TOutput>();

            // 배치 분할 (DOP)
            TInput[][] batches = inputData
                .Select((value, index) => new { value, index })
                .GroupBy(x => x.index / batchSize)
                .Select(g => g.Select(x => x.value).ToArray())
                .ToArray();

            await Task.Run(() =>
            {
                Parallel.ForEach(batches, new ParallelOptions { CancellationToken = token }, batch =>
                {
                    // ArrayPool은 value type일 때만 유의미
                    TOutput[] tempBuffer = ArrayPool<TOutput>.Shared.Rent(batch.Length);

                    try
                    {
                        for (int i = 0; i < batch.Length; i++)
                        {
                            tempBuffer[i] = transformation(batch[i]);
                        }

                        for (int i = 0; i < batch.Length; i++)
                        {
                            results.Add(tempBuffer[i]);
                        }
                    }
                    finally
                    {
                        ArrayPool<TOutput>.Shared.Return(tempBuffer);
                    }
                });
            }, token);

            OnProcessingCompleted?.Invoke(results.ToList());
        }
    }

    /// <summary>
    /// 사용 예시
    /// </summary>
    public static class Program
    {
        public static async Task Main()
        {
            // 예: int -> string 변환
            var stringProcessor = new ParallelDataProcessor<int, string>(
                batchSize: 512,
                transformation: num => $"Value={num}, Sqrt={Math.Sqrt(num):F3}"
            );

            stringProcessor.OnProcessingCompleted += results =>
            {
                Console.WriteLine($"[Completed] {results.Count} strings generated.");
                Console.WriteLine($"First 3: {string.Join(", ", results.Take(3))}");
            };

            await stringProcessor.ProcessAsync(Enumerable.Range(1, 5000));
        }
    }
}


이렇게 제네릭 타입으로 했는데

파이프 라인을 좀 더 범용화했을텐데

애초에 입출력 타입이 완전 제네릭화가 아니면 매핑 로직이 까다로운데 생각보다 별로인듯

이벤트 기반 처리도 그렇고

그리고 

이벤트 호출 쓰레드가 UI 쓰레드 일 경우 던져야할 디스패칭 로직이 빠져있음.

생각보다 여전히 문맥 문제가 심각한듯.


추천 비추천

0

고정닉 0

0

댓글 영역

전체 댓글 0
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 존재만으로도 웃음주는 최고의 '웃수저' 스타는? 운영자 25/08/25 - -
2884484 땡명뉴스 m병신 리짜이밍 외교망신에 영상삭제 ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 22 0
2884482 두달 넘게 케데헌 열풍, 뉴욕타임스 "미국 부모는 12번 아이들은 30번 발명도둑잡기갤로그로 이동합니다. 08.25 23 0
2884478 가짜대통령 짭통령 친중매국 리짜이밍 때문에 관세폭탄 맞은 대한민국의 미래 ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 22 0
2884476 발암물질유통 짱깨불매운동 확산 ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 30 0
2884474 10분 전부터 내 방 노트북 크롬웹브라우저가 갑자기 또 느려졌다 발명도둑잡기갤로그로 이동합니다. 08.25 23 0
2884473 배달원이 발견한 베개에 피로 쓰인 숫자들… 그 의미는? 발명도둑잡기갤로그로 이동합니다. 08.25 21 0
2884471 리재명 안동댐 사건은 또 뭐임? ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 21 0
2884469 윤석열의 심리 분석, 권위주의적 성격자 MBC 발명도둑잡기갤로그로 이동합니다. 08.25 26 0
2884468 속보)북한 휴전선 침범, 국군 하사 사망 ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 84 0
2884467 "트럼프가 도울 것" 윤 어게인, 매번 기대했지만… 발명도둑잡기갤로그로 이동합니다. 08.25 24 0
2884466 미국에서 벌어진 찢재명의 이상행동 ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 28 0
2884465 폴란드 대통령도 쓰는 영빈관 리짜이밍은 못쓴다 국가망신 ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 26 0
2884464 대한민국 최악의 리스크는 리짜이밍의 오럴리스크 ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 18 0
2884463 극좌배급견들 희소식 ㄷㅅㄷ 참가하고 1억 받자~ ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 18 0
2884462 nimf 출시가 2015년, 2016년인데 무슨 ㅎㅎ 나르시갤로그로 이동합니다. 08.25 24 0
2884461 혼자서도 잘노는 왕따 리짜이밍 영접도 셀프? ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 27 0
2884460 이재명 선거운동중 발언했던 엔비디아 국부펀드 유사 트럼프 인텔 지분 발명도둑잡기갤로그로 이동합니다. 08.25 21 0
2884459 최초로 영빈관 못가고 호텔 숙박한 찢째명 또 거짓말 [2] ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 34 0
2884458 꿈을 위해 달려가는 모든분들에게 ㅇㅇ(58.229) 08.25 27 0
2884457 뉴프로 재앙지원금 500포인트 지원안내 헬마스터갤로그로 이동합니다. 08.25 28 0
2884456 의장대 도열 없으니 푸대접?…李 대통령 방미 오해와 진실 발명도둑잡기갤로그로 이동합니다. 08.25 31 0
2884455 한번 시행착오를 거쳤으면 최적화는 당연 ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 24 0
2884454 ❤✨☀⭐⚡☘⛩나님 시작합니당⛩☘⚡⭐☀✨❤ ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 15 0
2884453 이러니 관세 25% 50% ㅉ ㅉ ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 27 0
2884452 친중매국 재명이는 그냥 미국에서 찬밥신세네 [1] ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 29 0
2884451 나님 후각 ㅆㅅㅌㅊ !! ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 24 0
2884450 맨날 정치글 도배하시는 분께서 친히 언급해주시니 감격스럽네요. 프갤러(110.8) 08.25 26 0
2884449 ❤✨☀⭐⚡☘⛩나님 시작합니당⛩☘⚡⭐☀✨❤ ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 17 0
2884448 호동씨가 건강이 매우 안 좋기 때문에 발명도둑잡기갤로그로 이동합니다. 08.25 37 0
2884447 요즘 코딩 존나 현타오지않냐 [1] 프갤러(175.193) 08.25 45 0
2884446 님프 목적은 아무래도 러스트로 분탕쳐서 인지도 쌓은 뒤 프갤러(110.8) 08.25 25 0
2884445 고 망한 이유가 구글임? [2] 프갤러(211.234) 08.25 41 0
2884444 님프 vs 러스트충 아직도 싸우고있노 [2] 헬마스터갤로그로 이동합니다. 08.25 41 0
2884443 "친구 만나는데 한 달에 35만원 나가네"…'우정 모임비' 압박에 美Z세 발명도둑잡기갤로그로 이동합니다. 08.25 32 0
2884442 에이다가 살짝 뜨니까 이슈몰이해서 랜섬웨어 만들려고 한듯 프갤러(27.177) 08.25 28 0
2884441 뉴딜정책은 실패작 ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 23 0
2884440 오늘 더워서 온라인에 싸우는 글이 많아졌다 발명도둑잡기갤로그로 이동합니다. 08.25 17 0
2884437 아하 그래서 님프 오픈소스에서 프리웨어로 돌렸나봅니다. 프갤러(223.63) 08.25 39 0
2884436 이야 잠깐 봤는데 님프는 정말 쓰레기가 맞습니다. [9] 프갤러(27.166) 08.25 71 0
2884434 항암치료제 리브리반트 국민청원 도와드리자. 목도Reason(119.195) 08.25 13 0
2884433 종교 중독이 판단력 저하를 일으키는 기제 발명도둑잡기갤로그로 이동합니다. 08.25 20 0
2884432 적페가 많다. 우리 사회의 적페들.. 오들 광고 땡기셨나? 자본주의 사회 목도Ria(119.195) 08.25 15 0
2884431 역시 이때다 하고 블로그 홍보질 처하네요. 렉카답습니다. 프갤러(27.177) 08.25 26 0
2884430 새끼 길냥이 보호색❤+ ♥지나가던냥덩이♥갤로그로 이동합니다. 08.25 29 0
2884429 nimf 소스코드 ㅎㅎ 나르시갤로그로 이동합니다. 08.25 36 0
2884428 [책 속으로] 세금 아끼려다 혁신 멈춘 애플, 그 뒤에 누가 있나 발명도둑잡기갤로그로 이동합니다. 08.25 22 0
2884427 솔직히 님프같은 쓰레기 만든 지능이면 그냥 딸배나 하는게 프갤러(27.177) 08.25 23 0
2884426 님프 블로그 광고비 먹고 싶으면 차라리 야짤이나 달리세요. 프갤러(27.167) 08.25 27 0
2884425 러스트 팩폭에 허위사실로 대응하는거 보십쇼 나르시갤로그로 이동합니다. 08.25 23 0
2884424 베이퍼웨어? 님프는 그건거 같더군요. 프갤러(27.167) 08.25 24 0
뉴스 '첫, 사랑을 위하여' 염정아X박해준X오나라 첫 정식 삼자대면...오나라 미소의 의미는? 디시트렌드 08.25
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2