Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Ruby - Hashe pod maską
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
CucumisSativus
November 18, 2015
Programming
46
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Ruby - Hashe pod maską
18.11.2015 at Łódź Ruby user Group
CucumisSativus
November 18, 2015
Transcript
20 slides
Hashe pod maską Michał Gutowski
Oparte na
Jaki jest hash każdy widzi
Jak trzymane są dane w hashu? 0 1 7 8
4 6 5 3 2 9 10 1 => 'el1' '2' => 'el2' :trzy => 1 struct st_table { /* troche niewaznych rzeczy */ int num_bins; // 11 int num_entries; // 3 struct st_table_entry **bins; };
Odczytywanie danych z hasha 0 1 7 8 4 6
5 3 2 9 10 1 => 'el1' '2' => 'el2' :trzy => 1 hash[1] funkcja_hashujaca(1) % 11 => 1 hash[:trzy] funkcja_hashujaca(:trzy) % 11 => ?
Dodawanie danych do hasha 0 1 7 8 4 6
5 3 2 9 10 1 => 'el1' '2' => 'el2' :trzy => 1 :cztery => 4 hash[:cztery] = 4 funkcja_hashujaca(4) % 11 => 8
Funkcja hashująca krótko irb #1 1.hash => 3709253595230655525 '2'.hash =>
1262910778843821320 :trzy.hash => -1450000291807366770 irb #2 1.hash => -3056501098034584631 '2'.hash => -3487312348025589315 :trzy.hash => -1489272169862565918
Kolizje :nowy => 6 ? funkcja_hashujaca(1) % 11 => 1
funkcja_hashujaca(:nowy) % 11 => 1 0 1 7 8 4 6 5 3 2 9 10 1 => 'el1' '2' => 'el2' :trzy => 1 :cztery => 4
0 1 7 8 4 6 5 3 2 9
10 1 => 'el1' '2' => 'el2' :trzy => 1 :cztery => 4 :nowy => 6 hash[:nowy] funkcja_hashujaca(:nowy) % 11 => 1 :nowy.eql? 1 => false :nowy.eql? :nowy => true
Pat Shaughnessy - Ruby Under a Microscope: An Illustrated Guide
to Ruby Internals Co najmniej 3 porównania przy każdym odwołaniu Tylko 44 elementy!
Co robić jak żyć?
Rehash • density = num_entries / num_bins • jeżeli większe
bądź równe 5 (ST_DEFAULT_MAX_DENSITY) • zwiększ liczbę binów do 19 (magiczny numer!) • dla każdego elementu odpal • funkcja_hashujaca(klucz) % 19
Rehash Pat Shaughnessy - Ruby Under a Microscope: An Illustrated
Guide to Ruby Internals
Rehash i czas dodawania elementów
Krótkie podsumowanie jeszcze przed końcem • Struktura klucz/wartość • Dane
są trzymane w „koszyczkach” • wrzucane są do odpowiednich koszyczków zależnie od funkcj hashującej • Kiedy robi się ich za dużo następuje rehash
Rehash i magiczne numerki ruby < 2.2 liczby pierwsze -
2^n+a Tabela Peter Moore Maksymalna liczba binów według tej tablicy 1 073 741 909
Rehash i magiczne numerki ruby 2.2
Funkcja hashująca trochę dłużej
Funkcja hashująca tl;dr • Oparta na algorytmie MurmurHash • funkcja
bazuje na przesunięciach bitowych • przy każdym odpaleniu ruby jest seedowana innymi danymi • funkcja bierze pod uwagę miejsce w pamięci danego obiektu • używa dwóch magicznych liczb 24 i 0x5bd1e995
They're not really 'magic', they just happen to work well
More Decks by CucumisSativus
See All by CucumisSativus
Cool things in dry-rb
cucumissativus
0
69
Neural networks basis
cucumissativus
0
87
How does the unicorn work
cucumissativus
0
74
Rubinius vs. CRuby
cucumissativus
0
62
Garbage Collector w ruby
cucumissativus
0
95
Other Decks in Programming
See All in Programming
[Kotlin Fest 2026] eBPF Tracing for Kotlin
takehaya
1
410
Kawaiicon: Kubernetes, it's no longer cool, but let's secure it anyway!
barnbarn
0
150
マルチプロダクトSaaSの アプリ一覧を速くする——複数プロダクトにまたがる「権限行脚」を減らすまで #techramen26conf
mktakuya
1
260
祈るテストから、探索するテストへ 〜Lincheckではじめる並行処理テスト〜
yyh_gl
0
430
AIが生成したコードは読むべきか?抽象化設計とコードの品質保証 / abstraction-design-and-quality
minodriven
17
9.6k
imagewebp()の向こう側: WebPはなぜ JPEG・PNGより軽いのか
yuzneri
0
160
Debian JP Projectのサーバー構成
uwabami
1
190
眠らない開発チームの作り方
biwakonbu
0
170
SONY CISC-NEWS NWS-1750 + NWB-225 ハードウェアメンテとフレームバッファカラー対応 / KOF2026
tsutsui
0
150
手の動かし方がわからなかった私が個人開発者になるまで
usagi0920_pochi
0
350
Compiling Your Language to WasmGC & Friends
tanishiking
0
770
Canvas 2D Context Next
petamoriken
0
750
Featured
See All Featured
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
77k
Faster Mobile Websites
deanohume
310
32k
The Language of Interfaces
destraynor
163
27k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
690
Marketing to machines
jonoalderson
1
6k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
First, design no harm
axbom
PRO
2
1.4k
Test your architecture with Archunit
thirion
2
2.5k
Technical Leadership for Architectural Decision Making
baasie
4
660
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
530
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
How GitHub (no longer) Works
holman
316
150k