디시인사이드 갤러리

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

갤러리 본문 영역

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

ㅆㅇㅆ(124.216) 2025.08.08 10:17:06
조회 87 추천 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 - -
2879927 1377년 금속활자로 인쇄한 『직지』가 현대 우리에게 전하는 것은 [1] 발명도둑잡기갤로그로 이동합니다. 08.10 89 0
2879926 오늘의 발명 실마리: 비밀 [1] 발명도둑잡기갤로그로 이동합니다. 08.10 66 0
2879921 섹스가 시작되자 죽음이 탄생했다? l 이정모 발명도둑잡기갤로그로 이동합니다. 08.10 49 0
2879920 반복돼 온 대멸종 법칙! "인류는 반드시 멸종" l 이정모 발명도둑잡기갤로그로 이동합니다. 08.10 72 0
2879919 ‘인공지능이 인류를 말살한다?’ 큰 파장을 낳은 AI2027 보고서 발명도둑잡기갤로그로 이동합니다. 08.10 191 0
2879913 기억 속 한 장의 필름④ — 0.75평, 양심을 가둔 방 발명도둑잡기갤로그로 이동합니다. 08.10 67 0
2879912 중국 오케스트라와 유럽 가는 거장 "나의 유일한 바람은…" 발명도둑잡기갤로그로 이동합니다. 08.10 78 0
2879910 gcc15, ruby에 버그 발견했는데 보고할까? 나르시갤로그로 이동합니다. 08.10 73 0
2879909 스텔라장-워크맨 발명도둑잡기갤로그로 이동합니다. 08.10 68 0
2879907 수학을 생각보다 조금 열심히 공부해야될거같아요 [2] PyTorch갤로그로 이동합니다. 08.10 130 0
2879902 노조 이야기 한 래퍼 발명도둑잡기갤로그로 이동합니다. 08.10 123 0
2879893 한국 힙합이 가사로 이야기하는 범위와 이야기 하지 못하고 침묵하는 범위 발명도둑잡기갤로그로 이동합니다. 08.10 254 0
2879892 유럽 일깨운 조선활자술의 '금속길' 상상기행 발명도둑잡기갤로그로 이동합니다. 08.10 106 0
2879891 역사적으로 노래가 상품이 된 시점 이전과 이후 [1] 발명도둑잡기갤로그로 이동합니다. 08.10 195 0
2879883 한국에서 20년 동안 우분투 커뮤니티를 지키는 사람들 발명도둑잡기갤로그로 이동합니다. 08.10 100 0
2879882 Gpt 세계 최고 AI 기업인데 [1] 프갤러(61.79) 08.10 106 0
2879876 전범선과 양반들 - 아래로부터의 혁명 발명도둑잡기갤로그로 이동합니다. 08.10 93 0
2879872 이 기회에 도커 삭제하고 Rancher로 갈아타야겠다 ㅆㅇㅆ(124.216) 08.10 96 0
2879867 ㅆㅇㅆ 이병신은 제너럴 리스트도아님 ㅋㅋ 프갤러(121.139) 08.10 135 4
2879864 Rancher데스크탑으로 바꾸는 중인데 자꾸 도커랑 충돌하네 ㅆㅇㅆ(124.216) 08.10 62 0
2879860 살인의 추억 배우들 최근 모습 발명도둑잡기갤로그로 이동합니다. 08.10 77 0
2879856 영카트 코드 보고가라 - ㅋㅋ ㅇㅇ(183.101) 08.10 77 0
2879855 C++ 인생 40 년 갈아 넣었습니다. [1] 프갤러(59.16) 08.10 140 0
2879854 저수준도 생각보다 자주바뀜 메모리캐싱도 그렇고 [2] ㅆㅇㅆ(124.216) 08.10 114 0
2879853 근데 솔직히 저수준 공부가 더 재밌음 [1] 밀우갤로그로 이동합니다. 08.10 129 0
2879852 영카트가 재밌을거 같다고..? ㅇㅇ(183.101) 08.10 77 0
2879851 저수준은 셰프고 고수준 언어는 동네 식당 주방ㅇㅇ ㅇㅇ(183.101) 08.10 93 0
2879850 미국의 양당정치 스펙트럼 발명도둑잡기갤로그로 이동합니다. 08.10 62 0
2879849 이 씨발 씨플플이 쉽다는 개새끼들 진짜 이해가안가네 [5] 프갤러(210.99) 08.09 137 0
2879845 가진것도, 배운것도 없는 23살 인생에 연봉 2600받는것이 꿈입니다.. [1] ㅇㅇ(223.39) 08.09 162 0
2879839 멍퀴를 본 슬기 표정.. [4] ♥냥덩이♥갤로그로 이동합니다. 08.09 147 0
2879837 에구구.. [2] ♥냥덩이♥갤로그로 이동합니다. 08.09 109 0
2879834 1 시갼 남ㅇ음~!!!!!!! ㅇㅇ(106.101) 08.09 90 0
2879833 애들한테 저수준 강요하는 강사들보면 혐오감 들수밖에 없는게 뭐냐면 [3] ㅆㅇㅆ(124.216) 08.09 219 0
2879832 저수준은 잘하면 좋은데 먹고 사는길이 너무 좁음 [4] ㅆㅇㅆ(124.216) 08.09 159 0
2879831 ‘쎈캐’ 홍기준 맞아? 세상 물정 모르는 ‘어수룩’ 완벽 변신 발명도둑잡기갤로그로 이동합니다. 08.09 123 0
2879830 근래 저수준 해보면서 느끼는건데 [2] 루도그담당(58.239) 08.09 140 0
2879829 <파인> 홍기 발명도둑잡기갤로그로 이동합니다. 08.09 127 0
2879828 이상한 회사 존나많네.. [4] 프갤러(222.96) 08.09 144 0
2879827 물론 언어마다 동시성 모델 차이나 내부적인 구현 좀 다르긴한 ㅆㅇㅆ(124.216) 08.09 101 0
2879825 깊게 따지고보면 세부구현은 다른데 막상 표현식이 비슷함 [2] ㅆㅇㅆ(124.216) 08.09 123 0
2879824 내가 공부해둘려고 언어 공통 매핑표 만들어놨는데 ㅆㅇㅆ(124.216) 08.09 94 0
2879823 근데 나이가 드니까 언어 바꿔 끼는데 거리낌이 없어짐. 이유가 [2] ㅆㅇㅆ(124.216) 08.09 166 0
2879822 ai시대인데 혁신적인것들이 없음 뒷통수한방(1.213) 08.09 84 0
2879821 Swagger 작성 제대로 해야하는데 영 쉽지 않다 [2] ㅆㅇㅆ(124.216) 08.09 118 0
2879815 TEMPEST 전자파 도청 글도 여러번 썼는데 검열삭제 당했다 발명도둑잡기갤로그로 이동합니다. 08.09 113 0
2879812 예전에 올렸던.북극성 노래 발명도둑잡기갤로그로 이동합니다. 08.09 87 0
2879810 고1 심심해서 만들어본거 [9] 프갤러(116.121) 08.09 354 5
2879809 구글에 돈을 갖다 바치는 중 [2] 뉴진파갤로그로 이동합니다. 08.09 125 1
2879807 Dis어샘bly 곧 저승에서 보자. 넥도리아(220.74) 08.09 100 0
뉴스 ‘조용필, 이 순간을 영원히-프리퀄' 박정현X이수연, 트리뷰트 공연! '감동 폭발' 디시트렌드 10.04
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2