디시인사이드 갤러리

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

갤러리 본문 영역

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

ㅆㅇㅆ(124.216) 2025.08.08 10:17:06
조회 66 추천 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 - -
2880377 애널 좀 축축하넹 ♥냥덩이♥갤로그로 이동합니다. 08.11 45 0
2880376 노동해방시대 오면 좋은점 ㅋ 뒷통수한방(1.213) 08.11 47 0
2880375 금융권 가고싶은데 어떤거 준비해야돼? [2] 프갤러(220.88) 08.11 321 0
2880374 냥덩이 제조법❤+ ♥냥덩이♥갤로그로 이동합니다. 08.11 40 0
2880373 논란중인 일본 숏폼 드라마 발명도둑잡기갤로그로 이동합니다. 08.11 60 0
2880372 출산드라 발명도둑잡기갤로그로 이동합니다. 08.11 49 0
2880371 몸에 힘이.. 빠진다냥.. ♥냥덩이♥갤로그로 이동합니다. 08.11 43 0
2880370 일이 없다. 최근 nest.js쪽은 실력부족으로 그만뒀고 [4] ㅆㅇㅆ찡갤로그로 이동합니다. 08.11 96 0
2880368 ❤✨☀⭐⚡☘⛩나님 시작합니당⛩☘⚡⭐☀✨❤ ♥냥덩이♥갤로그로 이동합니다. 08.11 49 0
2880367 혹시 악성코드 분석하는사람 있음? [10] ㅇㅇ(112.186) 08.11 133 0
2880366 말씀이란표현 재밋지않냐? [7] 헬마스터갤로그로 이동합니다. 08.11 99 0
2880365 아직 여름이로당 ♥냥덩이♥갤로그로 이동합니다. 08.11 50 0
2880364 대기업들 미국으로 전부 이전하든 니들이 앰생인건 똑같잖아 ㅋㅋㅋ 뒷통수한방(1.213) 08.11 39 0
2880363 비전공 국비 후기 [5] BABIBU갤로그로 이동합니다. 08.11 163 0
2880361 ❤✨☀⭐⚡☘⛩나님 시작합니당⛩☘⚡⭐☀✨❤ ♥냥덩이♥갤로그로 이동합니다. 08.11 49 0
2880360 주한미군 철수보다 타국 군대 철수가 더 낫다 발명도둑잡기갤로그로 이동합니다. 08.11 48 0
2880358 안녕하세요? 안녕하세요?(116.124) 08.11 49 0
2880357 가짜와 위선이 판치는 좌빨들과 그를 추종하는 래밍 배급견들 ♥냥덩이♥갤로그로 이동합니다. 08.11 58 0
2880356 x세대 ㅇㅇ(49.165) 08.11 68 0
2880355 아 존나 의욕이 없다 [4] ㅇㅇ(211.210) 08.11 105 0
2880354 애리조나 텍사스 ♥냥덩이♥갤로그로 이동합니다. 08.11 51 0
2880352 그래도 국민연금은 꼬박꼬박 내라..ㅇㅅㅇ [3] 헤르 미온느갤로그로 이동합니다. 08.11 88 0
2880351 예스24 또안드가짐 ㅋㅋ [2] 밀우갤로그로 이동합니다. 08.11 113 0
2880350 러스트 일자리가 없는건 니 수준이 그정도라 그런 것이니라 [1] 프갤러(218.154) 08.11 76 0
2880347 은행문열어!!!!! [10] 개멍청한유라갤로그로 이동합니다. 08.11 117 0
2880345 근데 토스는 이전회사 네임벨류로 연봉 제시한다는데 [1] 밀우갤로그로 이동합니다. 08.11 107 0
2880344 노을 ㅇㅅㅇ [2] 헤르 미온느갤로그로 이동합니다. 08.11 64 0
2880341 아 가방안들고 옴 [2] 밀우갤로그로 이동합니다. 08.11 59 0
2880338 일본 취업들에 환상이 많네 [4] 루도그담당(58.239) 08.11 132 0
2880337 태연 ㅇㅅㅇ 헤르 미온느갤로그로 이동합니다. 08.11 48 0
2880335 하루 한 번 헤르미온느 찬양 헤르 미온느갤로그로 이동합니다. 08.11 54 0
2880331 Ada 러스트 할 필요 없는 이유. 프갤러(59.16) 08.11 64 0
2880330 일본 기업들보면 좆소여도 성과급은 거의무조건주던데 [5] ㅇㅇ(223.38) 08.11 98 0
2880328 다들 화나있음 발명도둑잡기갤로그로 이동합니다. 08.11 55 0
2880323 루비가 ada하는 이유 프갤러(121.139) 08.11 85 1
2880322 ❤✨☀⭐⚡☘⛩나님 시작합니당⛩☘⚡⭐☀✨❤ ♥냥덩이♥갤로그로 이동합니다. 08.11 50 0
2880315 단독) 러스트 빠돌이들이 Ada 언급하지 않는 이유 나르시갤로그로 이동합니다. 08.11 77 0
2880311 러스트 빠돌이들이 제일 싫어하는 글을 소개합니다 [1] 나르시갤로그로 이동합니다. 08.11 69 0
2880309 그래두 사람을 Ada 프로그래밍 글 많이들 봤네 ㅎㅎ 나르시갤로그로 이동합니다. 08.11 69 0
2880308 크롬에 한글 입력 버그 생겼네 ㅎㅎ [3] 나르시갤로그로 이동합니다. 08.11 87 0
2880304 it 프리랜서 도전해보고싶은데 어디서부터 시작함? [1] 프갤러(223.38) 08.11 85 0
2880303 내가 쓴 Ada 프로그래밍 공부 중이다. 4.2 반복문 업뎃 중이다. 나르시갤로그로 이동합니다. 08.11 63 0
2880300 여러 disaggregation 전략을 보는중이에여 PyTorch갤로그로 이동합니다. 08.11 71 0
2880298 요새 폭탄테러가 많은 이유? 프갤러(211.234) 08.11 61 0
2880294 깃헙 코파일럿 같은거 개위험한것 같은데 헬마스터갤로그로 이동합니다. 08.11 91 0
2880293 Ada 프로그래밍: 부록: Clair 코딩 스타일 가이드 나르시갤로그로 이동합니다. 08.11 68 0
2880292 Ada 프로그래밍: 10. SPARK 소개 나르시갤로그로 이동합니다. 08.11 51 0
2880291 Ada 프로그래밍: 9. 계약에 의한 설계(DbC) 나르시갤로그로 이동합니다. 08.11 53 0
2880290 Ada 프로그래밍: 8. 동시성 및 실시간 프로그래밍 나르시갤로그로 이동합니다. 08.11 59 0
2880289 Ada 프로그래밍: 7. 외부 시스템과의 연동 나르시갤로그로 이동합니다. 08.11 64 0
뉴스 '착한 사나이' 강빈, 존경하는 이동욱과 보스 박훈 사이 갈등 연기 폭발! 복잡한 내면+파워 액션으로 ‘시선 강탈’ 디시트렌드 08.16
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2