디시인사이드 갤러리

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

갤러리 본문 영역

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

ㅆㅇㅆ(124.216) 2025.08.08 10:17:06
조회 59 추천 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/11 - -
AD 가전디지털, 휴대폰 액세서리 SALE 운영자 25/08/08 - -
2879441 ㅋㅅㅋ ♥냥덩이♥갤로그로 이동합니다. 08.08 46 0
2879440 오늘 내일 레딧에 글 쓰러 갈거다 ㅋㅋ 나르시갤로그로 이동합니다. 08.08 49 0
2879439 나님은 오탁이 아님 ♥냥덩이♥갤로그로 이동합니다. 08.08 48 0
2879438 나는조현병이야 나는내향적이야 손발이시립디다갤로그로 이동합니다. 08.08 51 0
2879437 자기와 의견이 다른 사람들 욕하지 않고는 정치글 못씀? ㅇㅅㅇ 헤르 미온느갤로그로 이동합니다. 08.08 42 0
2879435 외산nac 진짜 강력한데 오만기능 다있음 밀우갤로그로 이동합니다. 08.08 46 0
2879434 근데 극좌 4050세대가 나님 존나 좋아함 ♥냥덩이♥갤로그로 이동합니다. 08.08 71 0
2879433 공부완료후 집으로 귀가하는중입니다 현무E공인(211.234) 08.08 60 0
2879432 타코야끼 ㅇㅅㅇ 헤르 미온느갤로그로 이동합니다. 08.08 36 0
2879431 태연 ㅇㅅㅇ 헤르 미온느갤로그로 이동합니다. 08.08 36 0
2879430 애미창년새끼들 그냥 살짝 바까귀만 하는 줄 되나본데 [1] ㅆㅇㅆ(124.216) 08.08 69 0
2879429 하루 한 번 헤르미온느 찬양 헤르 미온느갤로그로 이동합니다. 08.08 40 0
2879428 사기꾼&도둑년 가짜대텅령 2찢명 ♥냥덩이♥갤로그로 이동합니다. 08.08 50 0
2879427 이상해서 ODD 분해중. 넥도리아(220.74) 08.08 71 0
2879426 레딧같은 웺사이트 만드는 중인데 트래픽 어떻게 핸들함? [5] 프갤러(211.55) 08.08 96 0
2879425 개씨발련들 진짜 프로그램 다 만들면 갑자기 다른 증권사 API써주세요 [13] ㅆㅇㅆ(124.216) 08.08 130 0
2879422 낼 토스코테 드걔재ㅐㅐㅐㅐ 프갤러(211.36) 08.08 54 0
2879420 매이풀 접엇는대 저 이재 머하면 댐요?? 롤 고고요?? ㅇㅇ(223.39) 08.08 41 0
2879419 애널 꿀잠자서 컨디션 ㅅㅌㅊ니 문제점 생김 ♥냥덩이♥갤로그로 이동합니다. 08.08 46 0
2879418 이런 애매한 경우 어케 해야함? [8] 아스카영원히사랑해갤로그로 이동합니다. 08.08 292 2
2879415 무인카페 커피하나시키고 무한공부되나? 현무E공인(211.234) 08.08 68 0
2879413 갑자기 8월부터 우울증 걸렸다 프갤러(61.79) 08.08 51 0
2879412 삶이 지옥같지만 꾹참고 하루하루 살아간다 프갤러(61.79) 08.08 50 0
2879411 애널의달성 2.1/1/2 ♥냥덩이♥갤로그로 이동합니다. 08.08 60 0
2879408 편의점 스팸도시락 3500원 짜리가 가성비최고네 현무E공인(211.234) 08.08 63 0
2879407 리팩토링 16번 순수400갤로그로 이동합니다. 08.08 53 0
2879406 담배도 폭식도 다 허무하다 프갤러(61.79) 08.08 70 0
2879405 냥덩이는 편견도 없고 차별도 하지 않아양..❤ [6] ♥냥덩이♥갤로그로 이동합니다. 08.08 104 0
2879403 젊은사람이하는미용실은들가기가무서움 [6] 현무E공인(211.234) 08.08 135 0
2879402 ‘팔레스타인 국가 인정’ 유엔 회의 개최…미국·이스라엘 불참 발명도둑잡기갤로그로 이동합니다. 08.08 42 0
2879401 거짓말하는 직원이 있음.. [3] 프갤러(14.52) 08.08 121 0
2879400 수십년 묵은 마귀의 악취는 어떨까요? 프갤러(220.84) 08.08 56 0
2879399 비틀즈, 롤링 스톤즈, 그리고 레이디 가가 발명도둑잡기갤로그로 이동합니다. 08.08 41 0
2879398 바이브 코딩 순수400갤로그로 이동합니다. 08.08 58 0
2879397 죄를 많이 지은 것같다. 거짓말한 셈이다. 넥도리아(223.38) 08.08 35 0
2879396 이유나 기자한테 따지다가 얼굴이 약간 일본 할아버지 에서 아저씨 느낌인데 넥도리아(223.38) 08.08 58 0
2879395 김건희가 워너비로 삼은 사람들 4인 발명도둑잡기갤로그로 이동합니다. 08.08 68 0
2879394 성향 검사 사이트 이런거 광고수익 얼마나 나올까 [1] 프갤러(49.142) 08.08 58 0
2879393 그나저나민생지원쿠폰12만으로뭐사지 [2] 현무E공인(211.234) 08.08 59 0
2879391 러스트 담론을 해체하다: 기술 토론에서 관찰되는 논증 오류 사례 분석 나르시갤로그로 이동합니다. 08.08 59 0
2879390 러스트 담론을 해체하다: 머리말 나르시갤로그로 이동합니다. 08.08 42 0
2879389 살아보니 학력이 발목을 많이잡는것같음 [2] ㅇㅇ(118.235) 08.08 100 0
2879387 2020년컴공졸업하고취업후6년시간개빠르네 현무E공인(211.234) 08.08 125 0
2879385 리팩토링 할 때마다 UX가 달라진다 순수400갤로그로 이동합니다. 08.08 59 0
2879384 빨리 퇴근하고싶다 ㅇㅇ(211.36) 08.08 38 0
2879383 방통대 다니는 직장인이 많네? [2] 루도그담당(118.235) 08.08 103 0
2879382 내년이면어느덧나도7년차개발자 현무E공인(211.234) 08.08 70 0
2879381 ❤✨☀⭐⚡☘⛩나님 시작합니당⛩☘⚡⭐☀✨❤ ♥냥덩이♥갤로그로 이동합니다. 08.08 43 0
2879380 러스트 담론 해체하기"로 "새로운 삼대장" 반박하기 나르시갤로그로 이동합니다. 08.08 65 0
2879376 남은 연차 ㅁㅌㅊ? [1] 어린이노무현갤로그로 이동합니다. 08.08 84 0
뉴스 “작은 엉덩이가 이상형”…최홍만, 여친 이야기하며 ‘눈물’ 디시트렌드 08.13
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2