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
NaN BoxingによるJSONパーサーの高速化
Search
Kazuhiro Fujieda
February 14, 2020
Programming
1k
2
Share
NaN BoxingによるJSONパーサーの高速化
DynaJsonの高速化で使っているNaN Boxingという技術を紹介する。
Kazuhiro Fujieda
February 14, 2020
More Decks by Kazuhiro Fujieda
See All by Kazuhiro Fujieda
静的クラスは遅いことがあるよ
kfujieda
3
2.1k
ftp.jaist.ac.jpの低レイヤーの話
kfujieda
0
91
Other Decks in Programming
See All in Programming
2026-04-15 Spring IO - I Can See Clearly Now
jonatan_ivanov
1
150
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
330
PHPでバイナリをパースして理解するASN.1
muno92
PRO
0
280
Claude Codeをカスタムして自分だけのClaude Codeを作ろう
terisuke
0
150
Explore CoroutineScope
tomoeng11
0
130
WebAssembly を読み込むベストプラクティス 2026年春版 / Best Practices for Loading WebAssembly (Spring 2026)
petamoriken
4
970
10 Tips of AWS ~Gen AI on AWS~
licux
5
510
SREに優しいTerraform構成 modulesとstateの組み方
hiyanger
2
160
AI時代のエンジニアリングの原則 / Engineering Principles in the AI Era
haru860
0
910
クラウドネイティブなエンジニアに向ける Raycastの魅力と実際の活用事例
nealle
2
230
ハーネスエンジニアリングとは?
kinopeee
13
6.5k
属人化しないコード品質の作り方_2026.04.07.pdf
muraaano
0
280
Featured
See All Featured
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
110k
Context Engineering - Making Every Token Count
addyosmani
9
860
How to Think Like a Performance Engineer
csswizardry
28
2.6k
Crafting Experiences
bethany
1
130
A Soul's Torment
seathinner
6
2.7k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
500
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
320
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
270
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
61
43k
Fireside Chat
paigeccino
42
3.9k
Transcript
NaN BoxingによるJSON パーサーの高速化 藤枝 和宏 twitter: kfujieda
[email protected]
DynaJson • https://github.com/fujieda/DynaJson/ • 高速なJSONパーサー • DynamicJson互換 var json =
JsonObject.Parse(@"{ ""foo"": ""json"", ""nest"": {""foobar"": true} }"); var a1 = json.foo; // "json" var a2 = json.nest.foobar; // true
速い citm_catalog.json (1.7MB)⇒DynamicObject 11.849 21.123 34.711 50.772 58.141 0 10
20 30 40 50 60 70 DynaJson Utf8Json Jil Newtonsoft.Json DynamicJson Time (ms) ←lower is better
なぜ速いか • JSONの値を16バイトの構造体に格納 • 動的メモリ割り当てが不要に • 代入でコピーが発生するが16バイトまでは速い
16バイトに格納する • Explicitレイアウトでフィールドを重ねる • doubleの上4バイトにJsonTypeを重ねる ⇒ NaN Boxing [StructLayout(LayoutKind.Explicit)] internal
struct InternalObject { [FieldOffset(4)] public JsonType Type; [FieldOffset(0)] public double Number; [FieldOffset(8)] public string String; [FieldOffset(8)] public JsonArray Array; [FieldOffset(8)] public JsonDictionary Dictionary; }
NaN Boxing • doubleのNaNの隙間に値を詰める • NaNは0/0などで発生する特別な値 • 表現は0xfff800000000 (qNANの場合) •
下位51ビットは任意の値でいい
NaNに値を詰める enum JsonType : uint { Null = 0xfff80001, True
= 0xfff80002, False = 0xfff80003, String = 0xfff80004, Array = 0xfff80005, Object = 0xfff80006 } static object ToValue(InternalObject obj) { switch (obj.Type) { case JsonType.Null: return null; case JsonType.True: return true; case JsonType.False: return false; case JsonType.String: return obj.String; case JsonType.Array: case JsonType.Object: return new JsonObject(obj); default: return obj.Number; } }
NaN Boxingの効果 11.849 13.418 16.294 0 5 10 15 20
struct 16 struct 24 class Time (ms) ←lower is better