As a key component of adaptive optics systems, wavefront sensing technology is an important way to effectively obtain aberrant phases in optical systems for high-capacity optical communications and high-quality imaging in relevant applications ranging from biological imaging to astronomical observation. To enhance the time efficiency of detection, the wavefront sensing with diffraction deep neural network (D2NN) directly calculates the wavefront information in the optical field. However, the compactness of the D2NN structure and the accuracy of wavefront prediction are important bottlenecks, restricting its practical application. Here, we design a multi-layer compact D2NN based on Bayesian optimization, called sparse D2NN (SD2NN), to achieve high-precision, real-time direct wavefront sensing. The experimental results demonstrated a reduction in the root-mean-square error (RMSE) of the SD2NN wavefront sensing of approximately 45.4%, along with a reduction in the axial length of approximately 82% in comparison to the unoptimized fully connected D2NN. This resulted in the attainment of a minimum layer distance of 8.77 mm. In addition, we additionally explored the effects of network depth and neuron size on the wavefront sensing performance of SD2NN and further summarized the general law of diffraction layer distance and neuron size. The proposed method will provide a reliable means of designing miniaturized integrated wavefront sensing chips.