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
2
1k
NaN BoxingによるJSONパーサーの高速化
DynaJsonの高速化で使っているNaN Boxingという技術を紹介する。
Kazuhiro Fujieda
February 14, 2020
Tweet
Share
More Decks by Kazuhiro Fujieda
See All by Kazuhiro Fujieda
静的クラスは遅いことがあるよ
kfujieda
3
2.1k
ftp.jaist.ac.jpの低レイヤーの話
kfujieda
0
85
Other Decks in Programming
See All in Programming
CSC307 Lecture 08
javiergs
PRO
0
660
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
230
KIKI_MBSD Cybersecurity Challenges 2025
ikema
0
1.3k
生成AIを使ったコードレビューで定性的に品質カバー
chiilog
1
230
高速開発のためのコード整理術
sutetotanuki
1
380
CSC307 Lecture 01
javiergs
PRO
0
680
MUSUBIXとは
nahisaho
0
130
AtCoder Conference 2025
shindannin
0
1k
CSC307 Lecture 06
javiergs
PRO
0
680
プロダクトオーナーから見たSOC2 _SOC2ゆるミートアップ#2
kekekenta
0
200
Fluid Templating in TYPO3 14
s2b
0
130
IFSによる形状設計/デモシーンの魅力 @ 慶應大学SFC
gam0022
1
290
Featured
See All Featured
Designing for humans not robots
tammielis
254
26k
GraphQLとの向き合い方2022年版
quramy
50
14k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
117
110k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.8k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
0
190
Measuring & Analyzing Core Web Vitals
bluesmoon
9
750
The agentic SEO stack - context over prompts
schlessera
0
630
Utilizing Notion as your number one productivity tool
mfonobong
2
210
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Being A Developer After 40
akosma
91
590k
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