Repository navigation
⚡ perf: optimize json serialization and introduce static fast path #76
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,20 +22,23 @@ | |
| "crystallization_status": "CRYSTALLINE" | ||
| } | ||
|
|
||
| # Pre-serialize static response for maximum root endpoint throughput | ||
| STATIC_ROOT_RESPONSE = json.dumps({ | ||
| "service": "Genesis Seismic Log", | ||
| "version": "1.0.0", | ||
| "status": "operational", | ||
| "protocol": "S-ToT (Seismic Tree-of-Thoughts)", | ||
| "endpoints": { | ||
| "live": "/api/bench/live", | ||
| "health": "/api/health", | ||
| "seismic": "/api/seismic/status" | ||
| } | ||
| }, separators=(',', ':')).encode('utf-8') | ||
|
|
||
| class SeismicHandler(BaseHTTPRequestHandler): | ||
| def do_GET(self): | ||
| if self.path == "/": | ||
| self.send_json({ | ||
| "service": "Genesis Seismic Log", | ||
| "version": "1.0.0", | ||
| "status": "operational", | ||
| "protocol": "S-ToT (Seismic Tree-of-Thoughts)", | ||
| "endpoints": { | ||
| "live": "/api/bench/live", | ||
| "health": "/api/health", | ||
| "seismic": "/api/seismic/status" | ||
| } | ||
| }) | ||
| self.send_json(STATIC_ROOT_RESPONSE) | ||
| elif self.path == "/api/health": | ||
| self.send_json({ | ||
| "status": "healthy", | ||
|
|
@@ -112,7 +115,10 @@ def send_json(self, data): | |
| self.send_header('Content-Type', 'application/json') | ||
| self.send_header('Access-Control-Allow-Origin', '*') | ||
| self.end_headers() | ||
| self.wfile.write(json.dumps(data, separators=(',', ':')).encode()) | ||
| if isinstance(data, bytes): | ||
| self.wfile.write(data) | ||
| else: | ||
| self.wfile.write(json.dumps(data, separators=(',', ':')).encode()) | ||
|
Comment on lines
+118
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For improved performance and HTTP compliance, it's crucial to set the Additionally, for consistency and robustness, it's best to be explicit with character encoding. I recommend refactoring the def send_json(self, data):
if isinstance(data, bytes):
body = data
else:
body = json.dumps(data, separators=(',', ':')).encode('utf-8')
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Content-Length', str(len(body)))
self.end_headers()
self.wfile.write(body)Since this refactoring affects lines outside the current diff, I'm providing the complete method here for you to apply. |
||
|
|
||
| def log_message(self, format, *args): | ||
| """Override to customize logging""" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the fast path for
bytesis a great performance optimization, this method can be further improved for both performance and robustness.elsebranch uses.encode()which relies on the platform's default encoding. This can lead to unexpected behavior on different systems. It's best practice to explicitly specify'utf-8'to matchSTATIC_ROOT_RESPONSE.Content-LengthHeader: For responses with a known size, setting theContent-Lengthheader is more efficient than letting the server use chunked transfer encoding.To add the
Content-Lengthheader, the response body must be prepared before callingend_headers(). This requires restructuring the method. Here is a suggested refactoring that addresses both points:Since a code suggestion can only modify the changed lines, I'm providing a smaller suggestion below to at least fix the explicit encoding. I highly recommend applying the full refactoring above for better performance.