디시인사이드 갤러리

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

갤러리 본문 영역

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

ㅆㅇㅆ(124.216) 2025.08.08 10:17:06
조회 43 추천 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 - -
2879882 Gpt 세계 최고 AI 기업인데 [2] 프갤러(61.79) 08.10 48 0
2879876 전범선과 양반들 - 아래로부터의 혁명 발명도둑잡기갤로그로 이동합니다. 08.10 21 0
2879872 이 기회에 도커 삭제하고 Rancher로 갈아타야겠다 ㅆㅇㅆ(124.216) 08.10 40 0
2879867 ㅆㅇㅆ 이병신은 제너럴 리스트도아님 ㅋㅋ 프갤러(121.139) 08.10 57 3
2879864 Rancher데스크탑으로 바꾸는 중인데 자꾸 도커랑 충돌하네 ㅆㅇㅆ(124.216) 08.10 26 0
2879863 2025년 입결 수직 급상승 로켓 떡상 명문!& 프갤러(168.126) 08.10 42 1
2879860 살인의 추억 배우들 최근 모습 발명도둑잡기갤로그로 이동합니다. 08.10 24 0
2879856 영카트 코드 보고가라 - ㅋㅋ ㅇㅇ(183.101) 08.10 28 0
2879855 C++ 인생 40 년 갈아 넣었습니다. [1] 프갤러(59.16) 08.10 76 0
2879854 저수준도 생각보다 자주바뀜 메모리캐싱도 그렇고 [2] ㅆㅇㅆ(124.216) 08.10 58 0
2879853 근데 솔직히 저수준 공부가 더 재밌음 [1] 밀우갤로그로 이동합니다. 08.10 59 0
2879852 영카트가 재밌을거 같다고..? ㅇㅇ(183.101) 08.10 24 0
2879851 저수준은 셰프고 고수준 언어는 동네 식당 주방ㅇㅇ ㅇㅇ(183.101) 08.10 25 0
2879850 미국의 양당정치 스펙트럼 발명도둑잡기갤로그로 이동합니다. 08.10 20 0
2879849 이 씨발 씨플플이 쉽다는 개새끼들 진짜 이해가안가네 [5] 프갤러(210.99) 08.09 80 0
2879845 가진것도, 배운것도 없는 23살 인생에 연봉 2600받는것이 꿈입니다.. [1] ㅇㅇ(223.39) 08.09 71 0
2879839 멍퀴를 본 슬기 표정.. [4] ♥냥덩이♥갤로그로 이동합니다. 08.09 79 0
2879837 에구구.. [2] ♥냥덩이♥갤로그로 이동합니다. 08.09 40 0
2879834 1 시갼 남ㅇ음~!!!!!!! ㅇㅇ(106.101) 08.09 44 0
2879833 애들한테 저수준 강요하는 강사들보면 혐오감 들수밖에 없는게 뭐냐면 [3] ㅆㅇㅆ(124.216) 08.09 100 0
2879832 저수준은 잘하면 좋은데 먹고 사는길이 너무 좁음 [4] ㅆㅇㅆ(124.216) 08.09 77 0
2879831 ‘쎈캐’ 홍기준 맞아? 세상 물정 모르는 ‘어수룩’ 완벽 변신 발명도둑잡기갤로그로 이동합니다. 08.09 40 0
2879830 근래 저수준 해보면서 느끼는건데 [2] 루도그담당(58.239) 08.09 83 0
2879829 <파인> 홍기 발명도둑잡기갤로그로 이동합니다. 08.09 46 0
2879828 이상한 회사 존나많네.. [4] 프갤러(222.96) 08.09 73 0
2879827 물론 언어마다 동시성 모델 차이나 내부적인 구현 좀 다르긴한 ㅆㅇㅆ(124.216) 08.09 41 0
2879825 깊게 따지고보면 세부구현은 다른데 막상 표현식이 비슷함 [2] ㅆㅇㅆ(124.216) 08.09 54 0
2879824 내가 공부해둘려고 언어 공통 매핑표 만들어놨는데 ㅆㅇㅆ(124.216) 08.09 42 0
2879823 근데 나이가 드니까 언어 바꿔 끼는데 거리낌이 없어짐. 이유가 [2] ㅆㅇㅆ(124.216) 08.09 81 0
2879822 ai시대인데 혁신적인것들이 없음 뒷통수한방(1.213) 08.09 42 0
2879821 Swagger 작성 제대로 해야하는데 영 쉽지 않다 [2] ㅆㅇㅆ(124.216) 08.09 67 0
2879815 TEMPEST 전자파 도청 글도 여러번 썼는데 검열삭제 당했다 발명도둑잡기갤로그로 이동합니다. 08.09 23 0
2879814 쭉정이 2마리의 발버둥은 멎어야 합니다. 프갤러(220.84) 08.09 41 0
2879812 예전에 올렸던.북극성 노래 발명도둑잡기갤로그로 이동합니다. 08.09 29 0
2879810 고1 심심해서 만들어본거 [9] 프갤러(116.121) 08.09 291 1
2879809 구글에 돈을 갖다 바치는 중 [2] 뉴진파갤로그로 이동합니다. 08.09 60 1
2879808 마귀소굴의 고통, 고난을 팝니다. 프갤러(220.84) 08.09 32 0
2879807 Dis어샘bly 곧 저승에서 보자. 넥도리아(220.74) 08.09 39 0
2879806 프로그래밍 언어 플레임 [1] 발명도둑잡기갤로그로 이동합니다. 08.09 55 0
2879805 외주의뢰로 nest.js 공부중인데 [3] ㅆㅇㅆ찡갤로그로 이동합니다. 08.09 80 0
2879803 야이 개 ㅆ ㅣ빨썌끼뜰아!!!!!!!!!!!!!!! [1] 프갤러(121.139) 08.09 48 1
2879802 토요일이 끝나간다 [1] 개멍청한유라갤로그로 이동합니다. 08.09 41 0
2879801 xx진의 사형선고만 아니었다면 사회에서 하하호호 만났겠죠. 프갤러(220.84) 08.09 40 0
2879799 안녕히 계세요 여러분-! 핸폰 메인폰 2023년 폰 살리기 위해서 가위 넥도리아(220.74) 08.09 31 0
2879798 여..여자라면.. 조금은 부끄러운줄 알라구우웃!! [4] ♥냥덩이♥갤로그로 이동합니다. 08.09 62 0
2879797 드라마 <북극성> 한대서 생각나는 예전 글 발명도둑잡기갤로그로 이동합니다. 08.09 27 0
2879796 나님 누엇어양❤+ [1] ♥냥덩이♥갤로그로 이동합니다. 08.09 39 0
2879794 흠.. 애널은 디깅 수확물이 없군.. ♥냥덩이♥갤로그로 이동합니다. 08.09 31 0
2879791 오늘의 소설, 영화, 발명 실마리: 인스타그램 만명 동의시 강제 생방 [1] 발명도둑잡기갤로그로 이동합니다. 08.09 34 0
2879788 마귀소굴에 빛이 있었던 적이 있었지요. 프갤러(220.84) 08.09 38 0
뉴스 한혜연, 16kg 감량 이후 44kg 체중 유지하는 특별 비법 공개...“53세 몸매 맞아?” 디시트렌드 08.09
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2