Generics Flashcards

1
Q

What is Monomorphization

A

The process of turning generic code into specific code by filling in the concrete types that are used when compiled

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A Good Example of Monomorphization use case

A

on Monomorphizing Hashmap Rust is able to create a seperate hashing function for each type so its more optimized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Downside of Monomorphization

A
  1. The downsides is that your binary ends up being larger because u need to generate a copy of the type or the function for every type that is used with.
  2. the code can be little less efficient because if u have 2 different functions making the computer have to jump to different seperate regions of memory
  3. could be expensive if u generate ridiciolus amount of types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a Static Dispatch

A

Static dispatch is when the concrete implementation of a method is determined at compile time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Advantages of Static Dispatch

A

Maximum performance

No runtime overhead

Enables inline optimizations

Type checking at compile time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Disadvantages of Static Dispatch

A

Increases binary size (code bloat)

Can’t handle cases where types are only known at runtime

All possible types must be available at compile time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When to use Static Dispatch

A
  1. Perfomance is critical
  2. Types are known at compile time
  3. Working with generic code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When to use Dynamic Dispatch

A
  1. Runtime Flexibility
  2. Hetergonus Collections meaning that the collection has different types
  3. if u want to use external Code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why do u need to know size at compile time

A

it needs to know how much to allocate stack memory for the variables, to generate efficient the compiler must know the size

how to layout the structs in the memory

Track ownership and lifetime

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

example of things that doens’t have any size on compile time if u make as argument for a functions

A

str,
[u8]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is a Vtable

A

virtual table is a little data structure that has a pointer to each methods for the trait for the type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a Fat pointer

A

a pointer that contains more than 1 pointer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Generic Types Vs Associated Types

A

Generic Types vs Associated Types:

Generic types (like trait Drive<T>) allow multiple implementations of the same trait for a single type, each with different type parameters.</T>

Associated types (like trait Drive { type DriveType; }) restrict you to only one implementation per type, as you can only specify one associated type per implementation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly