kwai / blaze

Blazing-fast query execution engine speaks Apache Spark language and has Arrow-DataFusion at its core.
Apache License 2.0
970 stars 90 forks source link

Evaluate Profile-Guided Optimization (PGO) and LLVM BOLT #275

Open zamazan4ik opened 8 months ago

zamazan4ik commented 8 months ago

Hi!

Recently I checked Profile-Guided Optimization (PGO) improvements on multiple projects. The results are here. According to the multiple tests, PGO can help with improving performance in many cases. That's why I think trying to optimize the Blaze (its Rust part) with PGO can be a good idea.

I can suggest the following action points:

Since it's the library, I can suggest the following way to optimize with PGO:

Maybe testing Post-Link Optimization techniques (like LLVM BOLT) would be interesting too (Clang and Rustc already use BOLT as an addition to PGO) but I recommend starting from the usual PGO.

For the Rust projects, I recommend starting experimenting with PGO with cargo-pgo.

Here are some examples of how PGO optimization is integrated in other projects:

richox commented 8 months ago

it sounds attractive, however it's not easy to build a binary for profiling because the native code is built into a lib and dynamically loaded in spark through JNI. we also tried some profiling ways like flamegraphing which does not need a binary, i doubt whether pgo can work like that?

zamazan4ik commented 8 months ago

it sounds attractive, however it's not easy to build a binary for profiling because the native code is built into a lib and dynamically loaded in spark through JNI

You can try to build a "wrapping" binary only for the native part (without JNI stuff), run it on a sample workload, collect the profiles, and then use these profiles during the normal Blaze compilation (PGO-optimize native part + build JNI stuff around it).

we also tried some profiling ways like flamegraphing which does not need a binary, i doubt whether pgo can work like that?

In theory yes, it's possible to do this via Sampling PGO (Clang docs). The same instruction should be valid for Rustc as well but I didn't test such scenario yet - most of my experience is with instrumentation PGO.

UPD: According to the Clang documentation, it's also possible to do instrumentation PGO with shared libraries - the PGO profiles will be dumped for each library. The Pydantic-core project uses this approach for building the PGO-optimized library version.