Microsoft MVP성태의 닷넷 이야기
.NET Framework: 2087. .NET 6부터 SourceGenerator와 통합된 System.Text.Json [링크 복사], [링크+제목 복사]
조회: 5848
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 4개 있습니다.)

.NET 6부터 SourceGenerator와 통합된 System.Text.Json

아래의 질문을 보면,

익명 클래스 말고 익명 구조체는 불가능한걸까요?
; https://www.sysnet.pe.kr/3/0/5786

이렇게 직렬화를 하는데,

string jsonString = JsonSerializer.Serialize(new { Age = 5, Height = 110 });

new로 생성한 익명 타입이 class이기 때문에 GC를 줄이기 위한 방법으로 struct가 사용되도록 하는 방법을 묻고 있습니다. 일단, 위의 코드에서 JsonSerializer.Serialize 메서드는 object를 받는 버전과 제네릭을 받는 버전으로 나뉩니다.

당연히 object를 받는 버전의 경우 설령 struct를 전달했다고 해도 박싱이 되므로 처음부터 클래스를 쓴 것보다 못한 결과를 낳습니다. 재미있는 건, 위의 메서드를 호출하면 제네릭 버전이 호출되긴 하는데요,

public static string Serialize<TValue>(TValue value, JsonSerializerOptions? options = null)
{
    JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
    return WriteString(value, jsonTypeInfo);
}

결국 내부 코드에서 호출되는 ObjectDefaultConverter.OnTryWrite 메서드가 박싱을 하기 때문에,

internal sealed override bool OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, ref WriteStack state)
{
    JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo;

    object obj = value; // box once
    // ...[생략]...
}

제네릭 지원 버전이라고 해서 GC Heap 사용이 없는 것은 아닙니다. 그렇긴 해도 System.Text.Json이 내부적으로 어떡해서든 GC Heap의 사용을 줄이려고 최대한 노력했다는 점이 코드 곳곳에서 눈에 띕니다. 가령 직렬화하는 buffer도 PooledByteBufferWriter를 이용해 풀링을 시켜놓고 있으며 Span<T>의 사용과 함께 GC Heap 할당을 억제하고 있습니다.




그나저나 .NET 6부터 System.Text.Json에는 JsonTypeInfo을 지원하는 제네릭 버전이 추가되었는데요,

public static string Serialize<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo)
{
    if (jsonTypeInfo is null)
    {
        ThrowHelper.ThrowArgumentNullException(nameof(jsonTypeInfo));
    }

    jsonTypeInfo.EnsureConfigured();
    return WriteString(value, jsonTypeInfo);
}

이건 Source Generator와 연동하므로, 컴파일 시점에 미리 생성해 둔 직렬화/역직렬화 클래스의 도움을 받아 성능을 높이게 됩니다. 게다가 사용 방법이 매우 쉬워서 그래도 나름 쓸만한데요, 예를 들어, 다음과 같이 직렬화할 타입이 있으면,

public struct MyPerson
{
    public int Age { get; set; }
    public int Height { get; set; }
}

그 타입을 JsonSerializable 특성으로 연결한 JsonSerializerContext의 상속 타입을 "partial"로 새롭게 만들어 주기만 하면 됩니다.

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(MyPerson))]
internal partial class SourceGenerationContext : JsonSerializerContext
{
}

끝입니다. 그럼 C# 컴파일러는 (%LOCALAPPDATA%\Temp\VSGeneratedDocuments 하위에) "SourceGenerationContext.MyPerson.g.cs" 파일을 컴파일 시점에 생성하고 "SourceGenerationContext.Default.MyPerson" 속성이 제공되므로 Json 직렬화 시 이것을 활용해,

MyPerson t = new MyPerson { Age = 5, Height = 120 };
string jsonString = JsonSerializer.Serialize(t, SourceGenerationContext.Default.MyPerson);

JsonSerializer.Serialize 메서드의 2번째 인자에 해당하는 JsonTypeInfo로 전달하면 됩니다. 실제로 이러한 2개의 코드를 BenchmarkDotNet으로 확인해 보면 "Source Generator"를 이용한 버전이 더 빠른 것을 확인할 수 있습니다.

|          Method |      Mean |     Error |   StdDev |
|---------------- |----------:|----------:|---------:|
| SerializeSrcGen |  80.98 ns | 25.378 ns | 1.391 ns | // Source Generator를 이용한 버전
| SerializeObject | 104.31 ns | 12.104 ns | 0.663 ns |

그런데 여기서도 재미있는 점이 하나 있습니다. Source Generator를 이용한 JsonTypeInfo를 전달한 경우에는 당연히 ObjectDefaultConverter.OnTryWrite를 사용하지 않으므로 (구조체를 전달한 경우) 박싱은 발생하지 않습니다. 하지만, 오히려 GC Heap 사용은 class를 전달한 경우보다 더 나옵니다.

예를 들어, 최종적으로 다음과 같이 테스트 코드를 구성하고,

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
using System.Text.Json;
using System.Text.Json.Serialization;

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(MyPerson))]
internal partial class SourceGenerationContext : JsonSerializerContext
{
}

public class Person
{
    public int Age { get; set; }
    public int Height { get; set; }
}

public struct MyPerson
{
    public int Age { get; set; }
    public int Height { get; set; }
}

namespace ConsoleApp1
{
    [ShortRunJob]
    [MemoryDiagnoser]
    public class Program
    {
        static int _count = 0;
        static void Main(string[] args)
        {
            BenchmarkRunner.Run<Program>();
        }

        static Person p1 = new Person { Age = 5, Height = 110 };
        static MyPerson p2 = new MyPerson { Age = 5, Height = 110 };

        [Benchmark]
        public void SerializeSrcGen()
        {
            string jsonString = JsonSerializer.Serialize(p2, SourceGenerationContext.Default.MyPerson);
        }

        [Benchmark]
        public void SerializeObject()
        {
            string jsonString = JsonSerializer.Serialize(p1);
        }
    }
}

실행해 보면 이런 결과가 나옵니다.

|          Method |      Mean |     Error |   StdDev |   Gen0 | Allocated |
|---------------- |----------:|----------:|---------:|-------:|----------:|
| SerializeSrcGen |  80.27 ns |  7.896 ns | 0.433 ns | 0.0061 |      96 B | // 구조체 + Source Generator를 사용한 버전
| SerializeObject | 103.30 ns | 10.666 ns | 0.585 ns | 0.0045 |      72 B | // 클래스를 전달한 버전

보는 바와 같이 Source Generator를 사용한 버전이 역시나 빠르지만, GC Heap은 24바이트 더 사용하고 있습니다. 직렬화된 JSON 텍스트의 길이가 22 + 1, 즉 46바이트이므로 각각 50바이트/26바이트 씩을 더 쓰고 있는 것입니다.




정리해 보면, "익명 클래스 말고 익명 구조체는 불가능한걸까요?" 질문의 익명 구조체는 현재 C#에서 지원하지 않고 있습니다. 단지, 구조체를 직접 정의해 전달하는 것은 가능하지만 그런 경우라면 박싱이 발생하지 않도록 Source Generator를 함께 사용해야 합니다. 그 결과, 속도는 빨라지지만 (개체 풀링 및 Span의 사용에도 불구하고) GC Heap의 사용은 여전히 발생합니다.

그렇긴 한데, 어쨌든 위의 결과를 보면 0세대 GC Heap의 가비지 수집이 꽤나 최적화가 잘 되었다는 느낌은 받게 됩니다.

(첨부 파일은 이 글의 테스트 코드를 포함합니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 5/8/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2023-01-13 08시11분
.Net 7 JSON Performance trap
; https://itnext.io/net-7-json-performance-trap-6811296cf158

C# - JSON 역/직렬화 시 리플렉션 손실을 없애는 JsonSrcGen
; https://www.sysnet.pe.kr/2/0/12688

-------------------------

.NET Conf 2023 (Day 2) - What's new in System.Text.Json
; https://youtu.be/vU-iZcxbDUk?t=9093

[.NET Core 3]
* High performance JSON library
* Focus on security and compliance
* First shipped with .NET Core 3
* NuGet package targeting netstandard2.0

[.NET 6]
* Source Generator
* Mutable JSON DOM (JsonNode)
* IAsyncEnumerable support

[.NET 7]
* Contract Customization
* Type Hierarchies (polymorphism)
* "required" member support

[.NET 8]
Try the new System.Text.Json source generator
; https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-source-generator/

* Native AOT improvements
  - Functional improvements
    . "required" and "init" member support
    . Polymorphism and unspeakable type support
    . String enum support for Native AOT
  - size reduction
    . Internal changes reducing trimmed app size.
    . Minimal NAOT console app using STJ:
       ~ 3.4MB -> 2.6MB (.NET 7 to .NET 8)
       ~ 23% size reduction
    . Demo ASP.NET Core app:
       ~ 21MB -> 8.9M (.NET 7 to .NET 8)
       ~ 57% size reduction
* ASP.NET Core interop improvements
  - Easier to register source generators, size reduction
* Bugfixes and reliability improvements
  - Closing the functional gap with reflection

PublishedTrimmed projects fail reflection-based serialization
; https://learn.microsoft.com/en-us/dotnet/core/compatibility/serialization/8.0/publishtrimmed#recommended-action

csproj JsonSerializerIsReflectionEnabledByDefault 속성 추가

// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Json/Common/JsonSourceGenerationOptionsAttribute.cs
[JsonSourceGenerationOptions(
  defaults: JsonSerializerDefaults.Web,
  Converters = [typeof(MyCustomConverter)],
  DefaultBufferSize = 128,
  DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
  DictionaryKeyPolicy = JsonKnownNamingPolicy.SnakeCaseUpper,
  IgnoreReadOnlyFields = true,
  IgnoreReadOnlyProperties = true,
  IncludeFields = true,
  MaxDepth = 1024,
  NumberHandling = JsonNumberHandling.WriteAsString,
  PreferredObjectCreationHandling = JsonObjectCreationHandling.Replace,
  PropertyNameCaseInsensitive = true,
  PropertyNamingPolicy = JsonKnownNamingPolicy.KebabCaseUpper,
  ReadCommentHandling = JsonCommentHandling.Skip,
  UnknownTypeHandling = JsonUnknownTypeHandling.JsonNode,
  UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow,
  WriteIndented = true)]
[JsonSerializable(typeof(MyPoco))]
public partial class MyContext : JsonSerializerContext {}

PropertyName = "value"
  ==> JsonNamingPolicy.SnakeCaseLower: "property_name": "value"
  ==> JsonNamingPolicy.KebabCaseUpper: "PROPERTY-NAME": "value"
정성태

1  2  3  4  5  6  [7]  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13455정성태11/25/20232454닷넷: 2168. C# - Azure.AI.OpenAI 패키지로 OpenAI 사용파일 다운로드1
13454정성태11/23/20232810닷넷: 2167. C# - Qdrant Vector DB를 이용한 Embedding 벡터 값 보관/조회 (Azure OpenAI) [1]파일 다운로드1
13453정성태11/23/20232314오류 유형: 879. docker desktop 설치 시 "Invalid JSON string. (Exception from HRESULT: 0x83750007)"
13452정성태11/22/20232419닷넷: 2166. C# - Azure OpenAI API를 이용해 사용자가 제공하는 정보를 대상으로 검색하는 방법파일 다운로드1
13451정성태11/21/20232549닷넷: 2165. C# - Azure OpenAI API를 이용해 ChatGPT처럼 동작하는 콘솔 응용 프로그램 제작파일 다운로드1
13450정성태11/21/20232358닷넷: 2164. C# - Octokit을 이용한 GitHub Issue 검색파일 다운로드1
13449정성태11/21/20232424개발 환경 구성: 688. Azure OpenAI 서비스 신청 방법
13448정성태11/20/20232668닷넷: 2163. .NET 8 - Dynamic PGO를 결합한 성능 향상파일 다운로드1
13447정성태11/16/20232552닷넷: 2162. ASP.NET Core 웹 사이트의 SSL 설정을 코드로 하는 방법
13446정성태11/16/20232481닷넷: 2161. .NET Conf 2023 - Day 1 Blazor 개요 정리
13445정성태11/15/20232807Linux: 62. 리눅스/WSL에서 CA 인증서를 저장하는 방법
13444정성태11/15/20232551닷넷: 2160. C# 12 - Experimental 특성 지원
13443정성태11/14/20232603개발 환경 구성: 687. OpenSSL로 생성한 사용자 인증서를 ASP.NET Core 웹 사이트에 적용하는 방법
13442정성태11/13/20232423개발 환경 구성: 686. 비주얼 스튜디오로 실행한 ASP.NET Core 사이트를 WSL 2 인스턴스에서 https로 접속하는 방법
13441정성태11/12/20232729닷넷: 2159. C# - ASP.NET Core 프로젝트에서 서버 Socket을 직접 생성하는 방법파일 다운로드1
13440정성태11/11/20232410Windows: 253. 소켓 Listen 시 방화벽의 Public/Private 제어 기능이 비활성화된 경우
13439정성태11/10/20232915닷넷: 2158. C# - 소켓 포트를 미리 시스템에 등록/예약해 사용하는 방법(Port Exclusion Ranges)파일 다운로드1
13438정성태11/9/20232514닷넷: 2157. C# - WinRT 기능을 이용해 윈도우에서 실행 중인 Media App 제어
13437정성태11/8/20232707닷넷: 2156. .NET 7 이상의 콘솔 프로그램을 (dockerfile 없이) 로컬 docker에 배포하는 방법
13436정성태11/7/20232961닷넷: 2155. C# - .NET 8 런타임부터 (Reflection 없이) 특성을 이용해 public이 아닌 멤버 호출 가능
13435정성태11/6/20232888닷넷: 2154. C# - 네이티브 자원을 포함한 관리 개체(예: 스레드)의 GC 정리
13434정성태11/1/20232656스크립트: 62. 파이썬 - class의 정적 함수를 동적으로 교체
13433정성태11/1/20232370스크립트: 61. 파이썬 - 함수 오버로딩 미지원
13432정성태10/31/20232441오류 유형: 878. 탐색기의 WSL 디렉터리 접근 시 "Attempt to access invalid address." 오류 발생
13431정성태10/31/20232772스크립트: 60. 파이썬 - 비동기 FastAPI 앱을 gunicorn으로 호스팅
13430정성태10/30/20232667닷넷: 2153. C# - 사용자가 빌드한 ICU dll 파일을 사용하는 방법
1  2  3  4  5  6  [7]  8  9  10  11  12  13  14  15  ...