shared frozen constant for empty JSON bodies#51
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)❌ Error creating Unit Test PR.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
- Replaces inline Object.freeze({}) with a module-level constant to eliminate redundant allocations.
- Reduces heap usage by ~6MB during core test suites.
- Adds a regression test to verify referential identity (zero-allocation).
c922451 to
528c197
Compare
|
Agent ran but didn't generate any test files. Tests may already exist or changes don't require additional tests. |
|
Hey @VikramAditya33 have a look at the pr , all the checks have passed |
|
❌ Failed to create PR with unit tests: AGENT_CHAT: Failed to open pull request |
I'm wondering the same thing too I just triggered a review lol although everything LGTM. Probably just wait for a while, meanwhile let's just watch what coderabbit comes up with 😆 |
|
@VikramAditya33 coderabbit doesn't seem to do anything lol , ig this can be merged ?? |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/http/core/request.spec.ts (1)
445-461: Test passes incidentally — two lines are misleading no-ops.Two issues that don't break the assertion today but obscure intent and are fragile:
Line 449:
mockUwsReq.getMethod.mockReturnValue('GET')runs after theUwsRequestconstructors at lines 446–447. The constructor synchronously cachesthis.methodfromuwsReq.getMethod()(request.ts line 182), so this mock change has no effect onreq1/req2. The test only passes because the defaultbeforeEachmock already returns'get'(uppercased to'GET').Line 453: With no
setHeaders([...])call, neithercontent-lengthnortransfer-encodingis set, so_initBodyParserreturns early at request.ts line 874–877 and never registersonData. TheonDataCallbackreference here still points to whatever the previous test installed (or the initial throwing stub if this test ran in isolation). The call therefore doesn't feed an empty body intoreq1/req2— they already havedoneReadingData = true, which is what actually drivesjson()down the empty-body branch.Tightening the test makes the optimization being verified unambiguous and removes cross-test coupling on
onDataCallback:♻️ Suggested cleanup
it('should optimize empty bodies by using the exact same memory reference', async () => { + // No content-length / transfer-encoding => _initBodyParser short-circuits, + // doneReadingData stays true, and json() takes the empty-body GET branch. + mockUwsReq.getMethod.mockReturnValue('get'); const req1 = new UwsRequest(mockUwsReq, mockUwsRes); const req2 = new UwsRequest(mockUwsReq, mockUwsRes); - mockUwsReq.getMethod.mockReturnValue('GET'); req1._initBodyParser(1024); req2._initBodyParser(1024); - onDataCallback(toArrayBuffer(Buffer.from('')), true); - const res1 = await req1.json(); const res2 = await req2.json(); // Referential equality proves zero new objects were allocated expect(res1).toBe(res2); expect(Object.isFrozen(res1)).toBe(true); });Optionally also assert
expect(res1).toEqual({})to pin the shape, and add a sibling test forHEAD/DELETEand one negative test thatPOSTwith empty body still throws — that would lock in the full contract documented at request.ts lines 1066–1073.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/http/core/request.spec.ts` around lines 445 - 461, The test relies on incidental state and should explicitly configure method and headers so the body parser is actually wired: construct UwsRequest instances after mocking mockUwsReq.getMethod to the intended verb (e.g., 'GET' or 'HEAD'), call setHeaders(...) to set either content-length: '0' or transfer-encoding to ensure _initBodyParser registers onData (so onDataCallback is the one used by these instances), then call _initBodyParser(1024) and invoke onDataCallback with an empty buffer before calling json(); reference UwsRequest, _initBodyParser, onDataCallback, json, setHeaders, and the header names content-length/transfer-encoding when making these changes.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/http/core/request.spec.ts`:
- Around line 445-461: The test relies on incidental state and should explicitly
configure method and headers so the body parser is actually wired: construct
UwsRequest instances after mocking mockUwsReq.getMethod to the intended verb
(e.g., 'GET' or 'HEAD'), call setHeaders(...) to set either content-length: '0'
or transfer-encoding to ensure _initBodyParser registers onData (so
onDataCallback is the one used by these instances), then call
_initBodyParser(1024) and invoke onDataCallback with an empty buffer before
calling json(); reference UwsRequest, _initBodyParser, onDataCallback, json,
setHeaders, and the header names content-length/transfer-encoding when making
these changes.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 386f3629-ece4-48c6-9875-84238774139a
📒 Files selected for processing (2)
src/http/core/request.spec.tssrc/http/core/request.ts
|
@aryan55254 does the nitpick regarding cleanup makes sense to you? Seems like a cleanup if you feel like it's okay otherwise I'm satisfied with the current implementation |
|
yeah I am satisified with it as well , i don't fully understand what the nitpick is saying tbh , it kinda seems to say the test is only passing because both req are empty but we were optimizing for when the requests are empty anyways so , and the memory reference matches so its solid from my side |
VikramAditya33
left a comment
There was a problem hiding this comment.
Alright it's a nitpick after all we can completely ignore it. LGTM, Thank you.
|
cool |
Fixes [#34]
Overview
This PR introduces a module-level constant
EMPTY_FROZEN_OBJECTdefined once at module load time, eliminating redundant object allocations for empty request bodies.Implementation
Why This Matters
Object.freeze({})===)Performance & Validation
Memory Footprint
--logHeapUsageshowed 6 MB reduction in peak heap sizeIdentity Verification
request.spec.tsusing.toBe()assertionmain)Checklist
mainSummary by CodeRabbit
Tests
Refactor