-
Notifications
You must be signed in to change notification settings - Fork 520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add protocol for handling Request & Response #372
Comments
Could you explain a little more how you envision this working in practice? Are you saying that the request passed to the handler would both imitate a map and implement this protocol, and the protocol would be for performant access? What about middleware that adds or removes keys? |
Yes, implement both a map and the protocol, example here currently using Potemkin while trying to figure out how to best implement that. Why not
|
Did a spike on a (defrecord Request [uri request-method])
(defn create
"Creates a partial-copy request where the commonly needed
keys are copied to an internal [[Request]] Record, while
rest of the keys are handled via [[ZeroCopyRequest]]."
[^HttpServerExchange exchange]
(->PartialCopyRequest
;; eager copy
(->Request
(.getRequestURI exchange)
(-> exchange .getRequestMethod .toString .toLowerCase keyword))
;; realize on access
(->ZeroCopyRequest exchange))) reading the (def request (create ...))
;; 12ns (clojure machinery)
(cc/quick-bench
(:uri request))
;; 5ns (via protocol)
(cc/quick-bench
(ring/get-uri request)) ... with some extraction, the |
Before this is considered, I'd like to have some good metrics on whether this would significantly improve performance. Given that we'd only be saving 7ns for a key lookup, and there is likely only going to be a few key lookups per request, any performance improvement seems like it would be relatively minor. |
I'll do a comprehensive benchmark. The real win is in the reading single header via a protocol |
Haven't had time to do any comprehensive stuff, but quick study on the current jetty "copy-all" model using Criterium on my macbook: REQ
; => #object[org.eclipse.jetty.server.Request 0x5cb44555 "Request(POST //localhost:8002/)@5cb44555"]
(build-request-map REQ)
;{:ssl-client-cert nil,
; :protocol "HTTP/1.1",
; :remote-addr "0:0:0:0:0:0:0:1",
; :headers {"accept" "application/json, */*",
; "user-agent" "HTTPie/2.0.0",
; "connection" "keep-alive",
; "host" "localhost:8002",
; "accept-encoding" "gzip, deflate",
; "content-length" "19",
; "content-type" "application/json"},
; :server-port 8002,
; :content-length 19,
; :content-type "application/json",
; :character-encoding "UTF-8",
; :uri "/",
; :server-name "localhost",
; :query-string nil,
; :body #object[org.eclipse.jetty.server.HttpInputOverHTTP
; 0x7905558d
; "HttpInputOverHTTP@7905558d[c=0,q=0,[0]=null,s=STREAM]"],
; :scheme :http,
; :request-method :post}
;; 2.4 µs
(cc/quick-bench
(build-request-map REQ)) Same as a flamegraph: ... copying the headers takes 55% of the total time. From the clojure map, getting a single header using ;; 113ns
(cc/quick-bench
(get-in req [:headers "content-type"])) compared to a zero-copy solution: ;; a type that implements all needed map methods + ring/Request
;; all lookups (e.g. `get`) are done first to the internal lookup-map + all changes are written there (a full copy is returned on `assoc`, `dissoc` etc.
;; secondary, lookup is done directly to the underlaying Java Request, potentially cached
;; ... looks like a map, at max 2 lookups to find a value, but zero-copying on creation
(deftype ZeroCopyRequest [_data ^Request _request]
ILookup
(valAt [_ k]
; ... fast dispatch on known keys, normal lookup for _data for others
IPersistentMap
(assoc [_ k v]
; .... copy with modified _data
...
ring/RingRequest
(get-header [_ header]
(or (some-> _data :headers (get (str/lower-case header)))
(str/join "," (enumeration-seq (.getHeaders _request header))))))
;; a zero-copy request
(def zreq (->ZeroCopyRequest {}, REQ));
;; 132ns
(cc/quick-bench
(ring/get-header zreq "content-type")) the I think
As there is Ring2 comping up, this could complement it? I could start/help to do a real jetty-zero-copy version if this is something that could go into ring. |
I've been recently reconsidering this, as it could also be used for compatibility. You've provided an example of a possible request protocol, but did you have any thoughts on the response protocol? |
Many modern Java Web Server (Netty, Undertow, Vert.x) have their own abstractions for Requests and Responses. Current ring-spec states that requests and responses as map-like structures. While this is awesome in abstraction-wise (and the killer feature of Ring), performance-wise it can be bad as data needs to be copied into maps. Copying can be eager (like with the jetty-adapter) for all requests or lazy (like Aleph & Immutant) / on demand, but still things like accessing a single header value forced all headers to be read copied and keys lowercased.
Abstracting the request and response as protocols would allow best of both worlds. A protocol like:
could be implemented for maps so that they do the normal lookup, but the Aleph-style zero-copy-request (map-like) classes could implement those directly.
(get-header request "Content-Type")
would use the already parsed information without any copying.This would allow low-level libraries to be programmed against effective protocols while keeping everything compatible with maps.
Currently working on a new lightweight and zero-fat new wrapper for Undertow and using a custom request protocol (like the one above) with it, but would not like to tie our other libraries to a server-specific protocols.
All the current web servers having their own request & response (map-like) types would need to conform to the new protocols, so would be a breaking change.
The text was updated successfully, but these errors were encountered: