Generics Everywhere CONTACT@ADAMFURMANEK.PL - - PowerPoint PPT Presentation

generics everywhere
SMART_READER_LITE
LIVE PREVIEW

Generics Everywhere CONTACT@ADAMFURMANEK.PL - - PowerPoint PPT Presentation

Generics Everywhere CONTACT@ADAMFURMANEK.PL HTTP://BLOG.ADAMFURMANEK.PL FURMANEKADAM 1 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK About me Experienced with backend, frontend, mobile, desktop, ML, databases. Blogger, public speaker.


slide-1
SLIDE 1

Generics Everywhere

CONTACT@ADAMFURMANEK.PL HTTP://BLOG.ADAMFURMANEK.PL FURMANEKADAM

GENERICS EVERYWHERE - ADAM FURMANEK 25.10.2020

1

slide-2
SLIDE 2

About me

Experienced with backend, frontend, mobile, desktop, ML, databases. Blogger, public speaker. Author of .NET Internals Cookbook. http://blog.adamfurmanek.pl contact@adamfurmanek.pl furmanekadam

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

2

slide-3
SLIDE 3

Agenda

Why do we need reusability. Templates in C++. Erasure in Java. Reification in .NET.

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

3

slide-4
SLIDE 4

Reusability

Use of existing assets in some form within the software product development process.

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

4

slide-5
SLIDE 5

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

5

slide-6
SLIDE 6

Why do we need reusability

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

6

slide-7
SLIDE 7

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

7

slide-8
SLIDE 8

Why do we need reusability

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

8

slide-9
SLIDE 9

Generic programming

Style of computer programming in which algorithms are written in terms of types to- be-specified-later that are then instantiated when needed for specific types provided as parameters.

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

9

slide-10
SLIDE 10

Generic programming

Not new – started in ML in 1973. Most popular on function and class level. Multiple flavours – templates, generics, type classes, polytypic functions. Used for code reuse, static-time reflection, metaprogramming.

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

10

slide-11
SLIDE 11

Templates in C++

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

11

slide-12
SLIDE 12

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

12

slide-13
SLIDE 13

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

13

slide-14
SLIDE 14

C++ templates C++ „copies” each template and replaces type placeholder with actual type. Then it tries to compile it. If it works – great!

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

14

slide-15
SLIDE 15

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

15

slide-16
SLIDE 16

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

16

slide-17
SLIDE 17

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

17

slide-18
SLIDE 18

How to see the code?

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

18

slide-19
SLIDE 19

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

19

slide-20
SLIDE 20

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

20

slide-21
SLIDE 21

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

21

slide-22
SLIDE 22

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

22

slide-23
SLIDE 23

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

23

slide-24
SLIDE 24

What is T?

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

24

template<typename T> template<typename T = void> template<typename... Ts> template<int I> template<typename K, typename V, template<typename> typename C = my_array>

slide-25
SLIDE 25

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

25

slide-26
SLIDE 26

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

26

slide-27
SLIDE 27

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

27

slide-28
SLIDE 28

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

28

slide-29
SLIDE 29

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

29

slide-30
SLIDE 30

Raytracing

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

30

https://github.com/phresnel/metatrace

slide-31
SLIDE 31

C++ templates

PROS

Relatively easy to understand. Doesn’t require sophisticated runtime support. Works without any notion of „supertype” or „base type” for all types. Doesn’t require extensive constraints for passed types. Can be easily specialized for specific types. Works nice with const expressions. Turing complete – perfect for compile time calculations.

CONS „Something” needs to instantiate the type for given parameter. A lot of machine code duplication. We cannot instantiate the code in runtime. Compiler messages are often less than ideal. We cannot constrain input type declaratively – we can only use metaprogramming tricks.

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

31

slide-32
SLIDE 32

JVM Erasure

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

32

slide-33
SLIDE 33

Java type system

OBJECTS java.lang.Object is the root of the class

  • hierarchy. Every class has Object as a
  • superclass. All objects, including arrays,

implement the methods of this class. Provides method for equality checking (equals), calculating hashcode (hashCode), checking runtime type (getClass), serializing to string literal (toString). String is an Object. PRIMITIVE VALUES Primitive types correspond to primitive values. These include int, short, long, byte, char, float, double, boolean. We can implicitly convert between them (most

  • f the times).

There is a reference type for each primitive type respectively (Integer, Short, Long, Byte, Character, Float, Double, Boolean).

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

33

slide-34
SLIDE 34

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

34

slide-35
SLIDE 35

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

35

slide-36
SLIDE 36

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

36

slide-37
SLIDE 37

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

37

slide-38
SLIDE 38

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

38

slide-39
SLIDE 39

JVM erasure

Java replaces generic placeholder with java.lang.Object. In other words, it gets rid of the generic type. There is one version of the code – no copying of generic for different types. It then compiles the code with the java.lang.Object as a generic type.

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

39

slide-40
SLIDE 40

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

40

slide-41
SLIDE 41

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

41

slide-42
SLIDE 42

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

42

slide-43
SLIDE 43

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

43

slide-44
SLIDE 44

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

44

slide-45
SLIDE 45

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

45

slide-46
SLIDE 46

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

46

slide-47
SLIDE 47

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

47

slide-48
SLIDE 48

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

48

slide-49
SLIDE 49

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

49

slide-50
SLIDE 50

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

50

slide-51
SLIDE 51

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

51

slide-52
SLIDE 52

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

52

slide-53
SLIDE 53

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

53

slide-54
SLIDE 54

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

54

slide-55
SLIDE 55

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

55

slide-56
SLIDE 56

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

56

slide-57
SLIDE 57

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

57

slide-58
SLIDE 58

JVM erasure

PROS Maintains backwards comaptibility. Stores one code for all types. Works with higher kinded types (partially). We can constrain generic parameter (bounds). Relatively easy to implement. Works with covariance and contravariance better. CONS

We lose the type information. We have a performance hit for primitive types. We don’t have full compiler support and type safety. It is harder to perform optimizations for particular types. We need to encode „trivial” things with type system. We cannot provide specialization for given type.

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

58

slide-59
SLIDE 59

Reification in .NET

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

59

slide-60
SLIDE 60

.NET Type system

Mostly based on Java type system. System.Object is the root of the hierarchy. Primitive types are called value types. Users can create their

  • wn value types (structures).

Few more things: delegates, events, expression trees, dynamic.

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

60

slide-61
SLIDE 61

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

61

slide-62
SLIDE 62

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

62

slide-63
SLIDE 63

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

63

slide-64
SLIDE 64

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

64

slide-65
SLIDE 65

Reification in .NET

.NET replaces type placeholder with System.__Canon for reference types and actual type for value types. It has one code for all reference types and different code for each value type. It doesn’t lose the generic type. Generics are reified and remember their type.

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

65

slide-66
SLIDE 66

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

66

slide-67
SLIDE 67

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

67

slide-68
SLIDE 68

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

68

slide-69
SLIDE 69

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

69

slide-70
SLIDE 70

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

70

slide-71
SLIDE 71

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

71

slide-72
SLIDE 72

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

72

slide-73
SLIDE 73

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

73

slide-74
SLIDE 74

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

74

slide-75
SLIDE 75

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

75

slide-76
SLIDE 76

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

76

slide-77
SLIDE 77

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

77

slide-78
SLIDE 78

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

78

slide-79
SLIDE 79

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

79

slide-80
SLIDE 80

Reification in .NET

PROS Maintains generic type – no need for passing type information in runtime. Guarantees type safety. Easier optimizations for value types. No overhead for value type (no boxing). CONS No way to do higher kinded types without runtime support. Harder to implement. Duplicates code for value types. Need to encode „trivial” things with type system. No easy way to provide specialization for given type.

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

80

slide-81
SLIDE 81

Summary

3 different approaches for reusability:

  • C++ copying code, changing the type and compiling it.
  • JVM removing generic type and pretending it is still there.
  • .NET storing generic type and generating different code for value types.

Each approach has pros and cons

  • C++ approach is Turing complete but requires a lot of code duplication.
  • JVM approach is easy to implement but doesn’t guarantee type safety.
  • .NET approach is much stronger and guarantees type safety but doesn’t

support higher kinded types = less powerful type system.

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

81

slide-82
SLIDE 82

Q&A

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

82

slide-83
SLIDE 83

References

Jeffrey Richter — „CLR via C#” Mario Hewardt — „Advanced .NET Debugging” Adam Furmanek – „.NET Internals Cookbook” Andrei Alexandrescu — „Modern C++ Design: Generic Programming and Design Patterns Applied: Applied Generic and Design Patterns (C++ in Depth)”

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

83

slide-84
SLIDE 84

References

https://blog.adamfurmanek.pl/2019/08/31/net-inside-out-part-10-using-type-markers-for-low- level-optimizations/ — type markers in .NET http://dreixel.net/research/pdf/gdmh.pdf — generics in Haskell https://alexandrnikitin.github.io/blog/dotnet-generics-under-the-hood/ — .NET generics under the hood http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html — Java generics under the hood

25.10.2020

84

GENERICS EVERYWHERE - ADAM FURMANEK

slide-85
SLIDE 85

Thanks!

CONTACT@ADAMFURMANEK.PL HTTP://BLOG.ADAMFURMANEK.PL FURMANEKADAM

25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

85