awslabs / aws-lambda-rust-runtime

A Rust runtime for AWS Lambda
Apache License 2.0
3.35k stars 342 forks source link

Lambda http/optimize deserialization #795

Closed bassmanitram closed 9 months ago

bassmanitram commented 9 months ago

Issue #, if available:

792

Description of changes: Identifying different Lambda events that pertain to HTTP requests is notoriously hard since they don't contain a simple kind property to help out. This is generally true - not just a problem in Rust.

In lambda-http a full deserialization of each enabled request type is attempted in turn, trying the next on failure of the previous, which has the possibility of being a bit of a performance hog when multiple features are enabled.

Up to 0.8 of lambda-http, the deserialization was made in two stages - firstly using a private deserializer available in the serde crate to deserialize to an interim value. That value was then passed to each enabled types's own deserialize associated function.

In 0.9 this was modified to be far cleaner, deserializing into a serde_json::RawValue to check the general structure of the request JSON, then deserializing the contained string slice into the type using standard serde_json functions..

However, this latter implementation entails two more downsides:

This PR attempts to solve all these issues by:

The one possible drawback I can see in this PR is that if Passthru is enabled, rather than passing through the original JSON string, that string is reconstructed from the parsed Value. You still get a JSON string but it won't be exactly the same as the input string. Others who know the project better than I can chime in on that.

By submitting this pull request

bassmanitram commented 9 months ago

Closing because the (naively) expected performance gains were not realized - v0.9 is, in fact, faster than v0.8 - by a long way.