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
45
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
More Decks by CucumisSativus
See All by CucumisSativus
Cool things in dry-rb
cucumissativus
0
68
Neural networks basis
cucumissativus
0
82
How does the unicorn work
cucumissativus
0
74
Rubinius vs. CRuby
cucumissativus
0
61
Garbage Collector w ruby
cucumissativus
0
94
Other Decks in Programming
See All in Programming
Agents on Rails - Rails at Scale 2026
irinanazarova
0
560
Optunaを作ったときにやりたかったこと、その後 (2026/10/16, Optuna Meetup #3)
iwiwi
1
430
Workers Cache を知る
syumai
0
360
3回つくってわかった、 定期実行ジョブ基盤の本質情報
takai
0
450
Jetpack Compose Mechanisms
skydoves
3
820
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
180
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
220
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
220
Domain-Driven Transformation
hschwentner
2
2.3k
JIS keyboard support in Omarchy
lef237
0
110
自サークルのIdPからログアウトできるようになるまで
taiseiue
1
140
TechTown - Creating Composable Callables in Contemporary C++
rollbear
0
130
Featured
See All Featured
AI: The stuff that nobody shows you
jnunemaker
PRO
10
1.2k
Google's AI Overviews - The New Search
badams
0
1.6k
Ethics towards AI in product and experience design
skipperchong
2
430
How to train your dragon (web standard)
notwaldorf
96
6.9k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
550
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Darren the Foodie - Storyboard
khoart
PRO
4
4k
Everyday Curiosity
cassininazir
0
350
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.3k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
380
Technical Leadership for Architectural Decision Making
baasie
3
600
Fireside Chat
paigeccino
43
4.1k
Transcript
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