The configuration of the "esp32" platform causes the -Og flag to be used in the compilation commands when Sketch > Optimize for Debugging is enabled. This flag disables the types of optimizations that would make it difficult for a human to get any useful understanding of a program by stepping with the debugger, but it doesn't disable optimization altogether. The compiler will still optimize out unused variables.