@inline and @specialized What Do They Do? Should I Be Using Them?
summary
Explains what @inline and @specialized actually do on the JVM, shows bytecode and microbenchmarks: compile-time inlining (requires -optimise flag and emits -Yinline-warnings if it fails) removes invokevirtual overhead and yields 3–4× speedup vs unoptimized runs and Scala 2.12 adds ~10% more; hotspot often already inlines so annotations can be redundant. Specialization eliminates boxing but multiplies classes (risk of code size explosion); results vary — a bloom filter shows no gain, an append-only int buffer shows double the speed when specialized.
AI-written summary · about
chapters (25)
- 0:00Start
- 5:33Removes function call overhead
- 7:03invokevirtual
- 8:15Enables further optimisations
- 9:50Answer: Code is data
- 11:04Inlining in HotSpot Conditions for inlining
- 12:27Inlining in Scala
- 13:30Inlining heuristics (Scala 2.10.0 -2.11.)
- 15:27New optimiser in 2.12
- 19:13Let's benchmark!
- 19:27Fast Fourier Transform
- 22:26Further reading
- 22:52Types in the JVM Primitive types
- 23:36Generic methods in Java
- 25:31Scala types
- 25:55Generic methods in Scala
- 26:59Specialisation
- 28:24How does the caller know? specialized is a static annotation stored in the class's ScalaSignature
- 29:29Space tradeoff
- 30:47Boxing in the Scala stdlib (as of 2.11.8)
- 31:29Alternatives
- 32:35Bloom filter
- 33:27Benchmark results
- 33:57Mutable buffer
- 37:14Summary
related talks
42mAvoiding macros with inline and derived
36mHow does Incremental Compilation Work with Scala 3
21mReasonable Scala Compiler
40mMacros vs Types
35mA Deep Dive into Scala Native Internals
43mBoost Your Productivity with IntelliJ, Scala Plugin and More
24mLet the Scala compiler work for you
44mScala 2.12 & Java 8: More Fun Together!
