apollographql / apollo-rs

Spec compliant GraphQL Tools in Rust.
Apache License 2.0
566 stars 45 forks source link

chore(parser): reduce intermediate string allocations #820

Closed goto-bus-stop closed 7 months ago

goto-bus-stop commented 7 months ago

By threading the appropriate input lifetime through lexer and parser methods as initially pointed out in https://github.com/apollographql/apollo-rs/pull/814, we can avoid copying strings in a few places.

Especially nice is that we can avoid copying the same strings multiple times when peeking at tokens, and we can avoid an eager copy that is almost always just thrown away in the implementation of .expect().

goto-bus-stop commented 7 months ago

Reverted the change making Token copyable, it was kinda worse to read and on top of that it was actually slower to copy tokens than to use references by about 10% according to the benchmarks in the repository. Now the ownership of tokens is the same as before, only the lifetime of the source text inside the tokens has changed.