Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
静的クラスは遅いことがあるよ
Search
Kazuhiro Fujieda
July 29, 2020
Technology
2.1k
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
静的クラスは遅いことがあるよ
Kazuhiro Fujieda
July 29, 2020
More Decks by Kazuhiro Fujieda
See All by Kazuhiro Fujieda
NaN BoxingによるJSONパーサーの高速化
kfujieda
2
1k
ftp.jaist.ac.jpの低レイヤーの話
kfujieda
0
100
Other Decks in Technology
See All in Technology
Text-to-SQLをAgentCoreで実現し、生成されるSQLの精度を定量的に評価する
yakumo
2
660
デジタル・デザイン:次の50年を描く「進化する青写真」
y150saya
0
820
記録をかんたんに、提案をパーソナルに ── AIであすけんが目指すもの
oprstchn
0
170
ポストモーテム! DDoSからサイトは守れた。 でもビジネスは守れなかった。
bengo4com
0
1.6k
AI時代のエンジニアキャリアについて今一度考える
sakamoto_582
2
1.3k
ローカルLLMとLINE Botの組み合わせ その3 / LINE DC Generative AI Meetup #8
you
PRO
0
120
グローバルチームと挑むプロダクト開発
sansantech
PRO
1
160
どうして今サーバーサイドKotlinを選択したのか
nealle
0
210
依頼文化をやめる日 EM視点で語るPlatform EngineeringとInclusive SRE / Discussing Platform Engineering and Inclusive SRE from an EM's Perspective
shin1988
4
4k
事業価値を⽣み出すSREへ SREが担うべき意思決定の5層
kenta_hi
1
1.9k
デジタル・デザイン構想 by Sayaka Ishizuka
y150saya
0
200
オブザーバビリティ、本当に活用できてる? 〜API連携×生成AIで成熟度を自動評価〜
dmmsre
0
1.9k
Featured
See All Featured
Thoughts on Productivity
jonyablonski
76
5.2k
Raft: Consensus for Rubyists
vanstee
141
7.6k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
310
Building the Perfect Custom Keyboard
takai
2
810
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.1k
Being A Developer After 40
akosma
91
590k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
630
Agile that works and the tools we love
rasmusluckow
331
22k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
How Software Deployment tools have changed in the past 20 years
geshan
0
34k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.4k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
340
Transcript
静的クラスは遅いことが あるよ 藤枝 和宏 twitter: kfujieda
[email protected]
DynaJson • https://github.com/fujieda/DynaJson/ • 高速なJSONパーザー • DynamicJson互換 dynamic json =
JsonObject.Parse(@"{ ""foo"": ""json"", ""nest"": {""foobar"": true} }"); string a1 = json.foo; // "json" bool a2 = json.nest.foobar; // true
速い citm_catalog.json (1.7MB)をパーズ .NET Core 3.1 Ubuntu 18.04 on Azure
D2ds_v4 0 5 10 15 20 25 30 35 40 DynaJson Utf8Json Jil Newtonsoft.Json DynamicJson Time (ms) ←lower is better
静的クラスを避ける public class JsonParser { private static readonly JsonParser Instance
= new JsonParser(); // インスタンスは一つ public static object Parse(TextReader reader, int maxDepth) { return Instance.ParseInternal(reader, maxDepth); // インスタンスメソッドを呼ぶ } private object ParseInternal(TextReader reader, int maxDepth) { // パーザーの本体 } }
静的クラスは遅い なぜか遅い 0 2 4 6 8 10 12 14
Static Normal Time (ms) ←lower is better .NET Core 3.1 Ubuntu 18.04 on Azure D2ds_v4
ディスアセンブルしてみる .NET Core 3.1.6 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.31603), X64 RyuJIT
JsonParser.ParseJsonInternal() ... mov r9d,[rbp+24] inc r9d mov [rbp+24],r9d inc dword ptr [rbp+28] cmp [rbp+20],r9d jne short M04_L01 ... Total bytes of code 7845 JsonParseStatic.Parse() ... mov r9d,[7FFE540EB864] inc r9d mov [7FFE540EB864],r9d inc dword ptr [7FFE540EB868] cmp [7FFE540EB860],r9d jne short M02_L01 ... Total bytes of code 9701
中身の大きなループは遅い • DynaJsonのJsonParserは大きなループ • 中身が全部L1キャッシュに乗らないと遅い 1 2 3 4 5
6 7 8 プログラム L1キャッシュ 1 2 3 4 5 6 7 8 9 9
なぜコードが大きいのか インライン展開の多用 case 'n': CheckToken("ull"); value.Type = JsonType.Null; break; private
void CheckToken(string s) { Consume(); foreach (var ch in s) { if (ch != _nextChar) throw JsonParserException.ExpectingError($"'{ch}'", _position); Consume(); } } private void Consume() { _bufferIndex++; _position++; if (_available == _bufferIndex) { _bufferIndex = 0; _available = _reader.ReadBlock(_buffer, 0, _buffer.Length); if (_available == 0) { _isEnd = true; _nextChar = '¥0'; return; } } _nextChar = _buffer[_bufferIndex]; }
なぜコードが大きいのか インライン展開の多用 case 'n': CheckToken("ull"); value.Type = JsonType.Null; break; cmp
eax,6E ← 'n' jne near ptr M04_L104 mov r10d,[rbp+24] inc r10d mov [rbp+24],r10d inc dword ptr [rbp+28] cmp [rbp+20],r10d jne near ptr M04_L38 xor r9d,r9d mov [rbp+24],r9d mov rdx,[rbp+10] mov r9d,[rdx+8] mov rcx,[rbp+8] xor r8d,r8d mov rax,[rcx] mov rax,[rax+48] call qword ptr [rax+28] mov [rbp+20],eax cmp dword ptr [rbp+20],0 jne near ptr M04_L38 mov byte ptr [rbp+2E],1 mov word ptr [rbp+2C],0 M04_L08: mov r9,23E10009B48 mov rsi,[r9] xor edi,edi mov r12d,[rsi+8] test r12d,r12d jle short M04_L11 M04_L11: mov dword ptr [rsp+0B4],0FFF80001 jmp near ptr M04_L05 127 bytes
インライン展開をやめてみる • 差が縮まった • 静的クラスのキャッシュミスが減ったかな 0 2 4 6 8
10 12 14 16 Static (inlined) Normal (inlined) Static Normal Time (ms) ←lower is better
まとめ • 静的クラスはコードが大きくなることがある • 中身の大きすぎるループは遅くなることがある