디시인사이드 갤러리

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

갤러리 본문 영역

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

ㅆㅇㅆ(124.216) 2025.08.08 10:17:06
조회 88 추천 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/10/06 - -
AD 프로게이머가 될테야!! 운영자 25/10/01 - -
공지 프로그래밍 갤러리 이용 안내 [96] 운영자 20.09.28 47715 65
2894797 'ㅎㅂ' 윤 공주 실시간 라방킴ㄱㄱ ㅇㅇ(183.96) 02:10 1 0
2894796 'ㅎㅂ) 벗방 시작한 강 인 경 프갤러(175.206) 02:08 1 0
2894795 'ㅎㅂ' 윤 공주 실시간 라방킴ㄱㄱ ㅇㅇ(183.96) 02:05 2 0
2894794 나님 분해 시작합니다. 브레이버갤로그로 이동합니다. 02:05 7 0
2894793 '김종국 신부 인스타 떴네요! 프갤러(175.206) 02:04 3 0
2894792 이런것도 물경력임? 프갤러(218.153) 01:40 27 0
2894782 러스트 담론을 해체하다 책을 객관적으로 업댓 예정 나르시갤로그로 이동합니다. 01:10 12 1
2894781 앞으로 러스트 씨부리는 것들은 ㅇㅇ(49.165) 01:06 17 0
2894780 러스트 담론을 해체하다: 5.6 개발 툴체인의 기술적 과제와 생산성 나르시갤로그로 이동합니다. 01:05 14 0
2894779 Ada 프로그래밍: 3.3.1 숫자 리터럴 나르시갤로그로 이동합니다. 00:56 12 0
2894778 Ada 프로그래밍: 3.3 리터럴 (Literals) 나르시갤로그로 이동합니다. 00:56 8 0
2894777 Ada 프로그래밍: 3.2 예약어 (reserved words) 나르시갤로그로 이동합니다. 00:55 9 0
2894776 Ada 프로그래밍: 3.1 식별자 (identifier) 나르시갤로그로 이동합니다. 00:55 9 0
2894775 Ada 프로그래밍: 3. 어휘 요소 나르시갤로그로 이동합니다. 00:54 13 0
2894772 근데 중소 AI는 자체개발 아니지 ㅇㅇ(106.102) 00:19 18 0
2894771 요즘 러스트 하는새끼들은 서보가 뭔지도 모르고 ㅇㅇ(49.165) 00:13 26 0
2894770 홍대가는중 개멍청한유라갤로그로 이동합니다. 00:04 15 0
2894768 방금 핫한 알고리즘이 추천해주었는데 ㅎㅎㅎ 넥도리아2025(112.170) 10.07 17 0
2894767 외롭다.. ㅇㅇ(14.52) 10.07 15 0
2894766 재밌는 루빅스 큐브의 세계 ㅇㅇ(121.168) 10.07 12 0
2894765 이번 추석 연휴 윤석열 배터지게 먹엇다 [2] 야옹아저씨갤로그로 이동합니다. 10.07 22 0
2894764 왜 러슬람은 까이나? [13] ㅇㅇ(49.165) 10.07 48 0
2894762 러슬람 병신새끼들은 [3] ㅇㅇ(49.165) 10.07 35 2
2894760 자연스러운 야동 추천좀 [11] 프갤러(49.142) 10.07 47 0
2894758 저장용 ♥덩냥이♥갤로그로 이동합니다. 10.07 23 0
2894757 나님 프갤 입장. 윤카는 무슨 생각 하실깡? 넥도리아2025(112.170) 10.07 28 0
2894755 극좌국뽕 영포티는 짱국에서도 ㅂㅅ 취급 당하는거 개웃교 ㅋㅅㅋ ♥덩냥이♥갤로그로 이동합니다. 10.07 23 0
2894754 '러스트 의지만 있으면 충분하다'란 말 정정하겠습니다. [7] 프갤러(110.8) 10.07 46 0
2894753 음기 충전 [1] 발명도둑잡기(39.7) 10.07 34 0
2894752 삼고액션빔 [1] ㅇㅇ(223.39) 10.07 32 2
2894749 한반도에 전쟁이 발생했을 경우… 어떻게 살아남아야 할까? [1] 발명도둑잡기(118.216) 10.07 24 0
2894747 핑모 너무 조아❤+ [1] ♥덩냥이♥갤로그로 이동합니다. 10.07 25 0
2894746 아이유 근황 발명도둑잡기(39.7) 10.07 25 0
2894745 갓본은 또 노벨상 받앗넹 벌써 30개 ㄷㅅㄷ [3] ♥덩냥이♥갤로그로 이동합니다. 10.07 30 0
2894744 명절연휴에 일하고와서 써보는 39개월차 개발자 여태다닌 회사 일기 [3] 프갤러(211.193) 10.07 91 1
2894743 [애니뉴스] 같은 데이터, 하지만 다른 느낌? 프갤러(121.172) 10.07 20 0
2894742 아패 16인치 나왓으면 조케땅 폴더블은 싫어 ♥덩냥이♥갤로그로 이동합니다. 10.07 25 0
2894741 "수업 중 스마트폰 못 써요?" 학생들 난리인데…효과 있나 봤더니 [1] 발명도둑잡기(39.7) 10.07 23 0
2894740 속보!! 아패 26 슬라이드오버 부활!!!!!!! [1] ♥덩냥이♥갤로그로 이동합니다. 10.07 24 0
2894739 아침 점심 저녁 발명도둑잡기(39.7) 10.07 11 0
2894738 투어 첫날인데도 기빨리네 [7] 류류(122.196) 10.07 40 0
2894737 우크라이나 다녀온 기자의 고백 "총성 없는 평범한 일상 소중함 느껴" 발명도둑잡기(39.7) 10.07 15 0
2894736 팀쿡 나이가 60세 넘어서 은퇴준비중이레 [2] ♥덩냥이♥갤로그로 이동합니다. 10.07 34 0
2894735 엄밀히는 일정 연기 ♥덩냥이♥갤로그로 이동합니다. 10.07 21 0
2894734 조만간 포기할건 포기할거 정해야할듯;; ♥덩냥이♥갤로그로 이동합니다. 10.07 24 0
2894733 러스트 빠돌이 정신차리는 듯 ㅋㅋ 나르시갤로그로 이동합니다. 10.07 26 0
2894732 슬램덩크 함 봐볼깡 ♥덩냥이♥갤로그로 이동합니다. 10.07 29 0
2894731 미국 주방위군 투입 내홍, 한국 주한미군 평택기지 시위에 투입 발명도둑잡기(39.7) 10.07 18 0
2894730 러스트 안써보고 깝쳐서 죄송합니다. [2] 프갤러(110.8) 10.07 52 2
뉴스 김수현, 김나영 재혼 등 연예계 희비 디시트렌드 10.06
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2