Dynamic Serializer fields in Django

  • In init, we can change the fields that are declared in the serializer.Meta class
  • Main use case for this is, some times you don’t want to send some fields in the response, so the fields in the response are dynamic.
  • A simple solution of this would be to remove the fields from the serializer.data, but consider this what if we are calling the serializer for multiple objects, then we would need to remove the fields from all the serialized objects. And also we would be accessing the columns from the DB which we don’t need. So wasted network cost and low latency from DB as well
  • Solution: Defer the columns in the queryset using defer(), so we only access the columns which we need. Then in the init method of serializer, remove the columns from serialzer.Meta,fields, so that serializer don’t do an extra sql call to get those missing columns

Process Execution Cycle

  • A process is a instance of a program
  • Process data is stored in the memory in below structure
-----------
Stack
-----------
Heap
-----------
Static
-----------
Text (code)
-----------

The Stack stores the variables i.e run time memory. The Text block contains the code of the process to be executed

About CPU

A CPU has this components

  • registers (r1, r2, r3, r4) The fastest accissible storage unit inside CPU
  • PC Program Counter, points to the next or current instruction in memory to be executed
  • IR Current Instruction

Access times

  • register - 1 ns
  • L1 cache - 2 ns
  • L2 cache - 7 ns
  • L3 cache - 15 ns
  • Main Memory - 100 ns
  • SSD - 150 un
  • HDD - 10 ms

L1 and L2 cache are unique to each CPU, where as L3 cache is common between all the CPUs

Execution Cycle

A code is stored and executed in bottom-up manner

Machine code:

str r3, #0xjsfksdjf
add r3, r1, r2
mov r1, #3
mov r0, #1
  1. PC is loaded with the 1st instruction (Memory Call)
    1. Update PC and IR
  2. Decode and execute the command
    1. Store 1 in r0 resister
  3. Increment the PC
  4. Load next command from L cache (No need for memory call here)
    1. The memory fetch is mainly in batch of burst mode, so a single memory fetch will fetch 3-4 instruction and will store the next instructions in the L cache.
    2. Execute the command
  5. For the last instruction we would be storing the string, not the register storage is small, and we need to store this in memory. So a Write-through cache call will be done, so that string will be stored in cache as well as memory
  • For every opr, the PC is incremented by either 4 or 8 depends on 32 bit or 64 bit architecture
  • The memory also stores the PC, but it is not incremented always, as that will be slow and is incremented only in CPU. But if the context switch happens than the PC in memory is updated