Skip to content

⚡ Optimize JSONL validation using json.Valid#1

Open
shantoislamdev wants to merge 2 commits into
mainfrom
optimize-jsonl-validation-7769831163424687852
Open

⚡ Optimize JSONL validation using json.Valid#1
shantoislamdev wants to merge 2 commits into
mainfrom
optimize-jsonl-validation-7769831163424687852

Conversation

@shantoislamdev
Copy link
Copy Markdown
Owner

💡 What:
Modified validateJSONL in internal/cli/commands.go to use json.Valid([]byte(line)) instead of json.Unmarshal([]byte(line), &obj).

🎯 Why:
Unmarshaling JSON into a dynamically sized map[string]any just to validate its syntax is highly inefficient. It involves significant CPU overhead for parsing values and a large number of heap memory allocations. Using json.Valid correctly checks syntax semantics without the memory allocation overhead, establishing a direct performance fix.

📊 Measured Improvement:
Created a benchmark BenchmarkValidateJSONL to test parsing a 1000-line JSONL file.

Baseline (using json.Unmarshal):

BenchmarkValidateJSONL-4   	     178	   6517208 ns/op	 2712798 B/op	   42004 allocs/op

(~6.5ms per operation, 42004 allocations per operation)

After Optimization (using json.Valid):

BenchmarkValidateJSONL-4   	     588	   1876231 ns/op	 1240949 B/op	    1005 allocs/op

(~1.8ms per operation, 1005 allocations per operation)

Speedup:

  • Execution Time: ~3.6x faster
  • Memory Allocations: ~41.7x fewer allocations

PR created automatically by Jules for task 7769831163424687852 started by @shantoislamdev

Refactored `validateJSONL` to use `json.Valid` instead of `json.Unmarshal`.
This avoids allocating a new `map[string]any` and dynamically unmarshaling
fields just to validate JSON syntax, which is much more efficient.

Added a benchmark `BenchmarkValidateJSONL` to measure the improvement.
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented May 21, 2026

Deploying kothaset with  Cloudflare Pages  Cloudflare Pages

Latest commit: 03f4edb
Status: ✅  Deploy successful!
Preview URL: https://d1aa5e94.kothaset.pages.dev
Branch Preview URL: https://optimize-jsonl-validation-77.kothaset.pages.dev

View logs

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f609771dd1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/cli/commands.go
var obj map[string]any
if err := json.Unmarshal([]byte(line), &obj); err != nil {
return count, fmt.Errorf("line %d: invalid JSON: %w", lineNum, err)
if !json.Valid([]byte(line)) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject non-object JSONL records

Switching to json.Valid changes validate dataset semantics from “line is a JSON object” to “line is any JSON value”, so files containing lines like [], "text", or 123 will now be reported as valid rows. KothaSet writes dataset rows as objects (see schema ToJSONL methods marshaling sample.Fields), so this creates false positives where structurally invalid datasets pass validation and are only discovered later in downstream use.

Useful? React with 👍 / 👎.

Refactored `validateJSONL` to use `json.Valid` instead of `json.Unmarshal`.
This avoids allocating a new `map[string]any` and dynamically unmarshaling
fields just to validate JSON syntax, which is much more efficient.

Added a benchmark `BenchmarkValidateJSONL` to measure the improvement.
Updated Go version in go.mod and github actions from 1.25.7 to 1.25.10
to resolve govulncheck vulnerability issues.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant