SlideShare a Scribd company logo
Efficient Erlang
Performance and memory efficiency of your data
About me and this talk
7 years of Erlang
20 years of C and C++
3 years experience with BEAM VM source and writing my own implementations
Github, twitter: kvakvs
Literal Values
● Literals are values mentioned in the module code
○ Loaded with module, stored in a separate const heap
○ Cheap to access
○ Literal tuples make fast lookup tables
● Size in process heap 1W until module reload, then copied by the GC
○ Each reload potentially creates a copy
Immediates
● Fit into a machine word
○ Small integers
○ Local pids and ports
○ Atoms
○ NIL [ ] and invisible special values (the non-value)
Size is always 1W
Immediates+
● Cheap and fast
● 2 bits are reserved to indicate immediate value
● 2-4 bits more are used to determine the type
○ Hence why small integer can only be 28/60 bits long
Size is always 1W
Immediate: Numbers
● Small integers: 28 or 60 bits
○ Caveat Convert to big automatically! (3+ words), watch your counters!
● Floats are NOT immediate, an 8-byte IEEE double stored on heap
○ Expensive and inefficient
○ Consider: Fixed point or N÷M fraction
Smallint size is 1W
Float size is 1+(2 or 3) W
Immediate: Atoms
● No GC for atoms
● Comparing two atoms
○ Equality is O(1)
○ Comparing to another type is O(1)
○ Order comparisons are O(N)
● Inefficient in external term format
Size is always 1W
+String in the atom tab (once)
SIZE, TYPE
Boxed Values
● A pointer to some object on heap
● Always at least 1 header word is
used (lists have no header)
● Can point to:
○ Lists (cons) cells, tuples, maps
○ Remote pids, remote refs
○ Float numbers and bignums
○ Binary fragments or heap binaries
A box pointer is always 1W
Size of a boxed value varies
<<”Hello”>>
Lists: Memory
● A list element is a cell of 2 terms
○ Stores 1 value and a tail — [1 | [2 | []]]
○ Improper lists can store 2 values in 1 cell — [1 | 2]
Box pointer is 1W
Size on heap is 2W
1 [2|[]]
2 NIL []1 2
[1|2]
[1,2]
Lists: Performance
● A list can only be traversed forward
● N-th element O(N), prepend O(1), append O(N)
○ Can express via IOList at O(1) — [L1, L2]
● – – operation is O(N*M), use ordsets or gb_sets for
the right arg
○ It is not too bad if the right argument is short
● ++ operation is O(N) because: finding last element
● lists:flatten, lists:reverse build a new list
1 [2|[]]
2 NIL []
[1,2]
List tricks
● Store another value instead of a trailing NIL
○ Improper list
○ [X | Y] takes 1+2 words
which is smaller, than [X, Y | []] — 1+4 words,
also smaller, than {X, Y} — 1+3 words
● Reversing is (relatively) cheap
● Reusing any tail of any list cell is cheap
● The compiler can optimize a LC if the result is not used
● Cells have potentially random locations
● Cheap to build
● When you build a list in a loop, cells will be contiguous
● Will potentially preserve contiguity after GC
Lists: Memory Locality
IO Lists
● Allows to join different types of string data at O(1)
● Accepted by most IO functions
● Have little memory overhead (some list cells at 2W each)
● Fast, memory efficient
○ Similar to: Rope (data structure)
● Do more IO lists, it is good for you
Image courtesy of Wikipedia/Rope
Tuples
● A tuple is an immutable array of Terms
● Prefixed by length (26 bit limit)
Box pointer is 1W
Size on heap is 1+arity
3 1
{1,a,b}
‘a’ ‘b’
Tuples: Another example
Box pointer is 1W
Size on heap is 1+arity
2 ‘error’
{error,{undef,start}}
{undef,start}
2 ‘undef’ ‘start’
Memory locality can be
poor unless created at
the same time
Tuples: Copying
● Modification = making a copy
○ Not copied: descending literal integer indices are modified without other calls in between
● Avoid copying: Store mutable fields at the top level of your records
● Copying nested unchanged structures is cheap
Box pointer is 1W
Size on heap is 1+arity
● Tuples are really fast to build and read
● Tuples are slow to modify O(N)
○ Also a full tuple copy most often is performed
● Consider converting to a list temporarily
● Memory locality — tuples are contiguous arrays
● GC will attempt to group tuple members in memory
Tuples: Performance
Maps
● Represented differently with breaking point at 32 elements
○ [<32] Compact key/value array
○ [>32] HAMT (Hash array mapped trie)
● Slower than records for obvious reasons
● Updating a large map would rebuild only path to the modified pair
● Using maps to store state: easy to add keys
Data is copied, when it
● leaves the process as a message to
another process or port
● is used as args when spawning
● is stored/retrieved from ETS
Exception:
● Large binaries > 64 bytes
Process Value
ETS Ports
Other
Processes
Binary
Heap Value
Binaries
A simple heap binary example
32-bit word size
2, binary 5 H
<<”Hello”>>
e l l o
Binary Heap
Process
Binaries
● Boxed values, exist on the process or the
binary heap
● A large binary is made of:
○ a refc bin on the binary heap
○ a proc bin which points to a refc bin
● A heap bin is a local small binary with a 2
word header and < 64b data
● A sub binary and match context are two
special cases
refc bin
proc bin
Process
sub bin
heap bin
proc bin
Binaries: The good news
● Chain of binary append operations will be optimized if there
was ONLY ONE use of that binary throughout the operation.
● Unused variables in a binary match can be optimized away
○ Unused part of binary in all fun clauses:
f(<<_,X/binary>>) -> …
● How to see binary optimizations:
○ erlc +bin_opt_info zzz.erl
○ export ERL_COMPILER_OPTIONS=bin_opt_info
Binaries: Please avoid
● Exposing large bin to multiple processes increases refcount and
holds the binary alive until the GC runs on all these processes!
● Pay attention when growing a binary — it will copy:
○ When the binary is sent as a message in the middle of manipulation
○ When the binary is inserted into ETS, sent to a port or to a NIF
○ When matching a binary (match context creates a pointer to the binary data)
● binary:match can produce lots of matches/sub binaries
ETS: Performance
● Storing/retrieving data implies copying
● Reading/modifying large terms is expensive
○ Consider having a second table with the mutable field
● Atoms are efficient in ETS but not in DETS
External Term Format (term_to_binary)
● Used over the network or to store terms on disk (database)
● Atoms: encoded as strings
○ Network, DETS/Mnesia, binary blobs in database
● Encoding large terms is CPU intensive
Functions
● Local function calls are fast
○ External function calls are slower (x3)
○ Apply calls are the slowest (x6)
● Lambdas are local function pointers (M:F/Arity)
○ List, binary comprehensions, QLC are lambdas
○ Lambdas are normal functions, but
○ Extra invisible args are added sometimes
○ Binary references may be held forever
Fin
Thanks for watching

More Related Content

What's hot

Lisa12 methodologies
Lisa12 methodologiesLisa12 methodologies
Lisa12 methodologies
Brendan Gregg
 
Cau hinh nat tren router cisco
Cau hinh nat tren router ciscoCau hinh nat tren router cisco
Cau hinh nat tren router cisco
tuanla79vn
 
Linux Profiling at Netflix
Linux Profiling at NetflixLinux Profiling at Netflix
Linux Profiling at Netflix
Brendan Gregg
 
Mạng neural nhân tạo và ứng dụng trong xử lý ngôn ngữ tự nhiên
Mạng neural nhân tạo và ứng dụng trong xử lý ngôn ngữ tự nhiênMạng neural nhân tạo và ứng dụng trong xử lý ngôn ngữ tự nhiên
Mạng neural nhân tạo và ứng dụng trong xử lý ngôn ngữ tự nhiên
Minh Pham
 
FreeSWITCH Modules for Asterisk Developers
FreeSWITCH Modules for Asterisk DevelopersFreeSWITCH Modules for Asterisk Developers
FreeSWITCH Modules for Asterisk Developers
Moises Silva
 
Linux và mã nguồn mở
Linux và mã nguồn mởLinux và mã nguồn mở
Linux và mã nguồn mở
Nguyễn Anh
 
SYCL 2020 Specification
SYCL 2020 SpecificationSYCL 2020 Specification
SYCL 2020 Specification
The Khronos Group Inc.
 
Slideshow - Tạo và quản lý người dùng, phân quyền trong ubuntu - 10b4 Fithou
Slideshow - Tạo và quản lý người dùng, phân quyền trong ubuntu - 10b4 FithouSlideshow - Tạo và quản lý người dùng, phân quyền trong ubuntu - 10b4 Fithou
Slideshow - Tạo và quản lý người dùng, phân quyền trong ubuntu - 10b4 Fithou
Tú Cao
 
PCI Passthrough and ITS Support in Xen / ARM :Xen Dev Summit 2015 Presentation
PCI Passthrough and ITS Support in Xen / ARM :Xen Dev Summit 2015 Presentation PCI Passthrough and ITS Support in Xen / ARM :Xen Dev Summit 2015 Presentation
PCI Passthrough and ITS Support in Xen / ARM :Xen Dev Summit 2015 Presentation
Manish Jaggi
 
Linux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old SecretsLinux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old Secrets
Brendan Gregg
 
Brief introduction to kselftest
Brief introduction to kselftestBrief introduction to kselftest
Brief introduction to kselftest
SeongJae Park
 
Tóm tắt lệnh Ubuntu
Tóm tắt lệnh UbuntuTóm tắt lệnh Ubuntu
Tóm tắt lệnh Ubuntu
Quang Ngoc
 
Project ACRN expose and pass through platform hidden PCIe devices to SOS
Project ACRN expose and pass through platform hidden PCIe devices to SOSProject ACRN expose and pass through platform hidden PCIe devices to SOS
Project ACRN expose and pass through platform hidden PCIe devices to SOS
Project ACRN
 
Advanced Json
Advanced JsonAdvanced Json
Advanced Json
guestfd7d7c
 
High-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uringHigh-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uring
ScyllaDB
 
Language Models N-gram
Language Models N-gramLanguage Models N-gram
Language Models N-gram
VTC Intecom
 
Troubleshooting containerized triple o deployment
Troubleshooting containerized triple o deploymentTroubleshooting containerized triple o deployment
Troubleshooting containerized triple o deployment
Sadique Puthen
 
[MeetUp][2nd] 오리뎅이의_쿠버네티스_네트워킹_v1.2
[MeetUp][2nd] 오리뎅이의_쿠버네티스_네트워킹_v1.2[MeetUp][2nd] 오리뎅이의_쿠버네티스_네트워킹_v1.2
[MeetUp][2nd] 오리뎅이의_쿠버네티스_네트워킹_v1.2
InfraEngineer
 
eBPF/XDP
eBPF/XDP eBPF/XDP
eBPF/XDP
Netronome
 
Các bài toán xử lý ngôn ngữ tự nhiên trong phát triển hệ thống chatbot
Các bài toán xử lý ngôn ngữ tự nhiên trong phát triển hệ thống chatbotCác bài toán xử lý ngôn ngữ tự nhiên trong phát triển hệ thống chatbot
Các bài toán xử lý ngôn ngữ tự nhiên trong phát triển hệ thống chatbot
Minh Pham
 

What's hot (20)

Lisa12 methodologies
Lisa12 methodologiesLisa12 methodologies
Lisa12 methodologies
 
Cau hinh nat tren router cisco
Cau hinh nat tren router ciscoCau hinh nat tren router cisco
Cau hinh nat tren router cisco
 
Linux Profiling at Netflix
Linux Profiling at NetflixLinux Profiling at Netflix
Linux Profiling at Netflix
 
Mạng neural nhân tạo và ứng dụng trong xử lý ngôn ngữ tự nhiên
Mạng neural nhân tạo và ứng dụng trong xử lý ngôn ngữ tự nhiênMạng neural nhân tạo và ứng dụng trong xử lý ngôn ngữ tự nhiên
Mạng neural nhân tạo và ứng dụng trong xử lý ngôn ngữ tự nhiên
 
FreeSWITCH Modules for Asterisk Developers
FreeSWITCH Modules for Asterisk DevelopersFreeSWITCH Modules for Asterisk Developers
FreeSWITCH Modules for Asterisk Developers
 
Linux và mã nguồn mở
Linux và mã nguồn mởLinux và mã nguồn mở
Linux và mã nguồn mở
 
SYCL 2020 Specification
SYCL 2020 SpecificationSYCL 2020 Specification
SYCL 2020 Specification
 
Slideshow - Tạo và quản lý người dùng, phân quyền trong ubuntu - 10b4 Fithou
Slideshow - Tạo và quản lý người dùng, phân quyền trong ubuntu - 10b4 FithouSlideshow - Tạo và quản lý người dùng, phân quyền trong ubuntu - 10b4 Fithou
Slideshow - Tạo và quản lý người dùng, phân quyền trong ubuntu - 10b4 Fithou
 
PCI Passthrough and ITS Support in Xen / ARM :Xen Dev Summit 2015 Presentation
PCI Passthrough and ITS Support in Xen / ARM :Xen Dev Summit 2015 Presentation PCI Passthrough and ITS Support in Xen / ARM :Xen Dev Summit 2015 Presentation
PCI Passthrough and ITS Support in Xen / ARM :Xen Dev Summit 2015 Presentation
 
Linux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old SecretsLinux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old Secrets
 
Brief introduction to kselftest
Brief introduction to kselftestBrief introduction to kselftest
Brief introduction to kselftest
 
Tóm tắt lệnh Ubuntu
Tóm tắt lệnh UbuntuTóm tắt lệnh Ubuntu
Tóm tắt lệnh Ubuntu
 
Project ACRN expose and pass through platform hidden PCIe devices to SOS
Project ACRN expose and pass through platform hidden PCIe devices to SOSProject ACRN expose and pass through platform hidden PCIe devices to SOS
Project ACRN expose and pass through platform hidden PCIe devices to SOS
 
Advanced Json
Advanced JsonAdvanced Json
Advanced Json
 
High-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uringHigh-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uring
 
Language Models N-gram
Language Models N-gramLanguage Models N-gram
Language Models N-gram
 
Troubleshooting containerized triple o deployment
Troubleshooting containerized triple o deploymentTroubleshooting containerized triple o deployment
Troubleshooting containerized triple o deployment
 
[MeetUp][2nd] 오리뎅이의_쿠버네티스_네트워킹_v1.2
[MeetUp][2nd] 오리뎅이의_쿠버네티스_네트워킹_v1.2[MeetUp][2nd] 오리뎅이의_쿠버네티스_네트워킹_v1.2
[MeetUp][2nd] 오리뎅이의_쿠버네티스_네트워킹_v1.2
 
eBPF/XDP
eBPF/XDP eBPF/XDP
eBPF/XDP
 
Các bài toán xử lý ngôn ngữ tự nhiên trong phát triển hệ thống chatbot
Các bài toán xử lý ngôn ngữ tự nhiên trong phát triển hệ thống chatbotCác bài toán xử lý ngôn ngữ tự nhiên trong phát triển hệ thống chatbot
Các bài toán xử lý ngôn ngữ tự nhiên trong phát triển hệ thống chatbot
 

Similar to Efficient Erlang - Performance and memory efficiency of your data by Dmytro Lytovchenko

Data efficiency on BEAM - Choose the right data representation by Dmytro Lyto...
Data efficiency on BEAM - Choose the right data representation by Dmytro Lyto...Data efficiency on BEAM - Choose the right data representation by Dmytro Lyto...
Data efficiency on BEAM - Choose the right data representation by Dmytro Lyto...
Magnus Sedlacek
 
TokuDB vs RocksDB
TokuDB vs RocksDBTokuDB vs RocksDB
TokuDB vs RocksDB
Vlad Lesin
 
Introduction to redis - version 2
Introduction to redis - version 2Introduction to redis - version 2
Introduction to redis - version 2
Dvir Volk
 
Iceberg: a fast table format for S3
Iceberg: a fast table format for S3Iceberg: a fast table format for S3
Iceberg: a fast table format for S3
DataWorks Summit
 
Advance computer architecture
Advance computer architectureAdvance computer architecture
Advance computer architecture
suma1991
 
Iceberg: A modern table format for big data (Strata NY 2018)
Iceberg: A modern table format for big data (Strata NY 2018)Iceberg: A modern table format for big data (Strata NY 2018)
Iceberg: A modern table format for big data (Strata NY 2018)
Ryan Blue
 
Advanced memory allocation
Advanced memory allocationAdvanced memory allocation
Advanced memory allocation
Joris Bonnefoy
 
CISSP Week 18
CISSP Week 18CISSP Week 18
CISSP Week 18
jemtallon
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
Saeid Zebardast
 
Database Systems Assignment Help
Database Systems Assignment HelpDatabase Systems Assignment Help
Database Systems Assignment Help
Database Homework Help
 
Ledingkart Meetup #2: Scaling Search @Lendingkart
Ledingkart Meetup #2: Scaling Search @LendingkartLedingkart Meetup #2: Scaling Search @Lendingkart
Ledingkart Meetup #2: Scaling Search @Lendingkart
Mukesh Singh
 
Save Java memory
Save Java memorySave Java memory
Save Java memory
JavaDayUA
 
Memory Optimization
Memory OptimizationMemory Optimization
Memory Optimization
Wei Lin
 
Memory Optimization
Memory OptimizationMemory Optimization
Memory Optimization
guest3eed30
 
Chromatic Sparse Learning
Chromatic Sparse LearningChromatic Sparse Learning
Chromatic Sparse Learning
Databricks
 
Apache Iceberg - A Table Format for Hige Analytic Datasets
Apache Iceberg - A Table Format for Hige Analytic DatasetsApache Iceberg - A Table Format for Hige Analytic Datasets
Apache Iceberg - A Table Format for Hige Analytic Datasets
Alluxio, Inc.
 
Chord DHT
Chord DHTChord DHT
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
Kuppusamy P
 
Cache recap
Cache recapCache recap
Cache recap
Tony Nguyen
 
Cache recap
Cache recapCache recap
Cache recap
Luis Goldster
 

Similar to Efficient Erlang - Performance and memory efficiency of your data by Dmytro Lytovchenko (20)

Data efficiency on BEAM - Choose the right data representation by Dmytro Lyto...
Data efficiency on BEAM - Choose the right data representation by Dmytro Lyto...Data efficiency on BEAM - Choose the right data representation by Dmytro Lyto...
Data efficiency on BEAM - Choose the right data representation by Dmytro Lyto...
 
TokuDB vs RocksDB
TokuDB vs RocksDBTokuDB vs RocksDB
TokuDB vs RocksDB
 
Introduction to redis - version 2
Introduction to redis - version 2Introduction to redis - version 2
Introduction to redis - version 2
 
Iceberg: a fast table format for S3
Iceberg: a fast table format for S3Iceberg: a fast table format for S3
Iceberg: a fast table format for S3
 
Advance computer architecture
Advance computer architectureAdvance computer architecture
Advance computer architecture
 
Iceberg: A modern table format for big data (Strata NY 2018)
Iceberg: A modern table format for big data (Strata NY 2018)Iceberg: A modern table format for big data (Strata NY 2018)
Iceberg: A modern table format for big data (Strata NY 2018)
 
Advanced memory allocation
Advanced memory allocationAdvanced memory allocation
Advanced memory allocation
 
CISSP Week 18
CISSP Week 18CISSP Week 18
CISSP Week 18
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
Database Systems Assignment Help
Database Systems Assignment HelpDatabase Systems Assignment Help
Database Systems Assignment Help
 
Ledingkart Meetup #2: Scaling Search @Lendingkart
Ledingkart Meetup #2: Scaling Search @LendingkartLedingkart Meetup #2: Scaling Search @Lendingkart
Ledingkart Meetup #2: Scaling Search @Lendingkart
 
Save Java memory
Save Java memorySave Java memory
Save Java memory
 
Memory Optimization
Memory OptimizationMemory Optimization
Memory Optimization
 
Memory Optimization
Memory OptimizationMemory Optimization
Memory Optimization
 
Chromatic Sparse Learning
Chromatic Sparse LearningChromatic Sparse Learning
Chromatic Sparse Learning
 
Apache Iceberg - A Table Format for Hige Analytic Datasets
Apache Iceberg - A Table Format for Hige Analytic DatasetsApache Iceberg - A Table Format for Hige Analytic Datasets
Apache Iceberg - A Table Format for Hige Analytic Datasets
 
Chord DHT
Chord DHTChord DHT
Chord DHT
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
 
Cache recap
Cache recapCache recap
Cache recap
 
Cache recap
Cache recapCache recap
Cache recap
 

More from Erlang Solutions

Fintech_Trends_for_2022_report_by_Erlang_Solutions.pdf
Fintech_Trends_for_2022_report_by_Erlang_Solutions.pdfFintech_Trends_for_2022_report_by_Erlang_Solutions.pdf
Fintech_Trends_for_2022_report_by_Erlang_Solutions.pdf
Erlang Solutions
 
Datadog and Elixir with Erlang Solutions
Datadog and Elixir with Erlang SolutionsDatadog and Elixir with Erlang Solutions
Datadog and Elixir with Erlang Solutions
Erlang Solutions
 
Strategies for successfully adopting Elixir
Strategies for successfully adopting ElixirStrategies for successfully adopting Elixir
Strategies for successfully adopting Elixir
Erlang Solutions
 
RabbitMQ vs Apache Kafka Part II Webinar
RabbitMQ vs Apache Kafka Part II WebinarRabbitMQ vs Apache Kafka Part II Webinar
RabbitMQ vs Apache Kafka Part II Webinar
Erlang Solutions
 
RabbitMQ vs Apache Kafka - Part 1
RabbitMQ vs Apache Kafka - Part 1RabbitMQ vs Apache Kafka - Part 1
RabbitMQ vs Apache Kafka - Part 1
Erlang Solutions
 
Designing & architecting RabbitMQ engineered systems - Ayanda Dube @ London R...
Designing & architecting RabbitMQ engineered systems - Ayanda Dube @ London R...Designing & architecting RabbitMQ engineered systems - Ayanda Dube @ London R...
Designing & architecting RabbitMQ engineered systems - Ayanda Dube @ London R...
Erlang Solutions
 
Building the ideal betting stack | London Erlang User Group presentation
Building the ideal betting stack | London Erlang User Group presentationBuilding the ideal betting stack | London Erlang User Group presentation
Building the ideal betting stack | London Erlang User Group presentation
Erlang Solutions
 
Empowering mobile first workers in emerging-markets using messaging
Empowering mobile first workers in emerging-markets using messagingEmpowering mobile first workers in emerging-markets using messaging
Empowering mobile first workers in emerging-markets using messaging
Erlang Solutions
 

More from Erlang Solutions (8)

Fintech_Trends_for_2022_report_by_Erlang_Solutions.pdf
Fintech_Trends_for_2022_report_by_Erlang_Solutions.pdfFintech_Trends_for_2022_report_by_Erlang_Solutions.pdf
Fintech_Trends_for_2022_report_by_Erlang_Solutions.pdf
 
Datadog and Elixir with Erlang Solutions
Datadog and Elixir with Erlang SolutionsDatadog and Elixir with Erlang Solutions
Datadog and Elixir with Erlang Solutions
 
Strategies for successfully adopting Elixir
Strategies for successfully adopting ElixirStrategies for successfully adopting Elixir
Strategies for successfully adopting Elixir
 
RabbitMQ vs Apache Kafka Part II Webinar
RabbitMQ vs Apache Kafka Part II WebinarRabbitMQ vs Apache Kafka Part II Webinar
RabbitMQ vs Apache Kafka Part II Webinar
 
RabbitMQ vs Apache Kafka - Part 1
RabbitMQ vs Apache Kafka - Part 1RabbitMQ vs Apache Kafka - Part 1
RabbitMQ vs Apache Kafka - Part 1
 
Designing & architecting RabbitMQ engineered systems - Ayanda Dube @ London R...
Designing & architecting RabbitMQ engineered systems - Ayanda Dube @ London R...Designing & architecting RabbitMQ engineered systems - Ayanda Dube @ London R...
Designing & architecting RabbitMQ engineered systems - Ayanda Dube @ London R...
 
Building the ideal betting stack | London Erlang User Group presentation
Building the ideal betting stack | London Erlang User Group presentationBuilding the ideal betting stack | London Erlang User Group presentation
Building the ideal betting stack | London Erlang User Group presentation
 
Empowering mobile first workers in emerging-markets using messaging
Empowering mobile first workers in emerging-markets using messagingEmpowering mobile first workers in emerging-markets using messaging
Empowering mobile first workers in emerging-markets using messaging
 

Recently uploaded

Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Zilliz
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 

Recently uploaded (20)

Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 

Efficient Erlang - Performance and memory efficiency of your data by Dmytro Lytovchenko

  • 1. Efficient Erlang Performance and memory efficiency of your data
  • 2. About me and this talk 7 years of Erlang 20 years of C and C++ 3 years experience with BEAM VM source and writing my own implementations Github, twitter: kvakvs
  • 3. Literal Values ● Literals are values mentioned in the module code ○ Loaded with module, stored in a separate const heap ○ Cheap to access ○ Literal tuples make fast lookup tables ● Size in process heap 1W until module reload, then copied by the GC ○ Each reload potentially creates a copy
  • 4. Immediates ● Fit into a machine word ○ Small integers ○ Local pids and ports ○ Atoms ○ NIL [ ] and invisible special values (the non-value) Size is always 1W
  • 5. Immediates+ ● Cheap and fast ● 2 bits are reserved to indicate immediate value ● 2-4 bits more are used to determine the type ○ Hence why small integer can only be 28/60 bits long Size is always 1W
  • 6. Immediate: Numbers ● Small integers: 28 or 60 bits ○ Caveat Convert to big automatically! (3+ words), watch your counters! ● Floats are NOT immediate, an 8-byte IEEE double stored on heap ○ Expensive and inefficient ○ Consider: Fixed point or N÷M fraction Smallint size is 1W Float size is 1+(2 or 3) W
  • 7. Immediate: Atoms ● No GC for atoms ● Comparing two atoms ○ Equality is O(1) ○ Comparing to another type is O(1) ○ Order comparisons are O(N) ● Inefficient in external term format Size is always 1W +String in the atom tab (once)
  • 8. SIZE, TYPE Boxed Values ● A pointer to some object on heap ● Always at least 1 header word is used (lists have no header) ● Can point to: ○ Lists (cons) cells, tuples, maps ○ Remote pids, remote refs ○ Float numbers and bignums ○ Binary fragments or heap binaries A box pointer is always 1W Size of a boxed value varies <<”Hello”>>
  • 9. Lists: Memory ● A list element is a cell of 2 terms ○ Stores 1 value and a tail — [1 | [2 | []]] ○ Improper lists can store 2 values in 1 cell — [1 | 2] Box pointer is 1W Size on heap is 2W 1 [2|[]] 2 NIL []1 2 [1|2] [1,2]
  • 10. Lists: Performance ● A list can only be traversed forward ● N-th element O(N), prepend O(1), append O(N) ○ Can express via IOList at O(1) — [L1, L2] ● – – operation is O(N*M), use ordsets or gb_sets for the right arg ○ It is not too bad if the right argument is short ● ++ operation is O(N) because: finding last element ● lists:flatten, lists:reverse build a new list 1 [2|[]] 2 NIL [] [1,2]
  • 11. List tricks ● Store another value instead of a trailing NIL ○ Improper list ○ [X | Y] takes 1+2 words which is smaller, than [X, Y | []] — 1+4 words, also smaller, than {X, Y} — 1+3 words ● Reversing is (relatively) cheap ● Reusing any tail of any list cell is cheap ● The compiler can optimize a LC if the result is not used
  • 12. ● Cells have potentially random locations ● Cheap to build ● When you build a list in a loop, cells will be contiguous ● Will potentially preserve contiguity after GC Lists: Memory Locality
  • 13. IO Lists ● Allows to join different types of string data at O(1) ● Accepted by most IO functions ● Have little memory overhead (some list cells at 2W each) ● Fast, memory efficient ○ Similar to: Rope (data structure) ● Do more IO lists, it is good for you Image courtesy of Wikipedia/Rope
  • 14. Tuples ● A tuple is an immutable array of Terms ● Prefixed by length (26 bit limit) Box pointer is 1W Size on heap is 1+arity 3 1 {1,a,b} ‘a’ ‘b’
  • 15. Tuples: Another example Box pointer is 1W Size on heap is 1+arity 2 ‘error’ {error,{undef,start}} {undef,start} 2 ‘undef’ ‘start’ Memory locality can be poor unless created at the same time
  • 16. Tuples: Copying ● Modification = making a copy ○ Not copied: descending literal integer indices are modified without other calls in between ● Avoid copying: Store mutable fields at the top level of your records ● Copying nested unchanged structures is cheap Box pointer is 1W Size on heap is 1+arity
  • 17. ● Tuples are really fast to build and read ● Tuples are slow to modify O(N) ○ Also a full tuple copy most often is performed ● Consider converting to a list temporarily ● Memory locality — tuples are contiguous arrays ● GC will attempt to group tuple members in memory Tuples: Performance
  • 18. Maps ● Represented differently with breaking point at 32 elements ○ [<32] Compact key/value array ○ [>32] HAMT (Hash array mapped trie) ● Slower than records for obvious reasons ● Updating a large map would rebuild only path to the modified pair ● Using maps to store state: easy to add keys
  • 19. Data is copied, when it ● leaves the process as a message to another process or port ● is used as args when spawning ● is stored/retrieved from ETS Exception: ● Large binaries > 64 bytes Process Value ETS Ports Other Processes Binary Heap Value
  • 20. Binaries A simple heap binary example 32-bit word size 2, binary 5 H <<”Hello”>> e l l o
  • 21. Binary Heap Process Binaries ● Boxed values, exist on the process or the binary heap ● A large binary is made of: ○ a refc bin on the binary heap ○ a proc bin which points to a refc bin ● A heap bin is a local small binary with a 2 word header and < 64b data ● A sub binary and match context are two special cases refc bin proc bin Process sub bin heap bin proc bin
  • 22. Binaries: The good news ● Chain of binary append operations will be optimized if there was ONLY ONE use of that binary throughout the operation. ● Unused variables in a binary match can be optimized away ○ Unused part of binary in all fun clauses: f(<<_,X/binary>>) -> … ● How to see binary optimizations: ○ erlc +bin_opt_info zzz.erl ○ export ERL_COMPILER_OPTIONS=bin_opt_info
  • 23. Binaries: Please avoid ● Exposing large bin to multiple processes increases refcount and holds the binary alive until the GC runs on all these processes! ● Pay attention when growing a binary — it will copy: ○ When the binary is sent as a message in the middle of manipulation ○ When the binary is inserted into ETS, sent to a port or to a NIF ○ When matching a binary (match context creates a pointer to the binary data) ● binary:match can produce lots of matches/sub binaries
  • 24. ETS: Performance ● Storing/retrieving data implies copying ● Reading/modifying large terms is expensive ○ Consider having a second table with the mutable field ● Atoms are efficient in ETS but not in DETS
  • 25. External Term Format (term_to_binary) ● Used over the network or to store terms on disk (database) ● Atoms: encoded as strings ○ Network, DETS/Mnesia, binary blobs in database ● Encoding large terms is CPU intensive
  • 26. Functions ● Local function calls are fast ○ External function calls are slower (x3) ○ Apply calls are the slowest (x6) ● Lambdas are local function pointers (M:F/Arity) ○ List, binary comprehensions, QLC are lambdas ○ Lambdas are normal functions, but ○ Extra invisible args are added sometimes ○ Binary references may be held forever