Skip to content

⚡ Optimize gofakeit usage in server handler#87

Merged
sudorandom merged 3 commits intomainfrom
perf-gofakeit-optimization-1036730633027740137
Feb 26, 2026
Merged

⚡ Optimize gofakeit usage in server handler#87
sudorandom merged 3 commits intomainfrom
perf-gofakeit-optimization-1036730633027740137

Conversation

@sudorandom
Copy link
Copy Markdown
Owner

This PR optimizes the NewHandler function in private/server/handler.go by removing the explicit initialization of gofakeit.New(0) for every request.

Changes:

  • Removed Faker: gofakeit.New(0) from GenOptions struct initialization.
  • Removed unused import github.com/brianvoe/gofakeit/v7.

Impact:

  • GenOptions.fake() now defaults to gofakeit.GlobalFaker.
  • This avoids creating a new *rand.Rand and seeding it with time.Now() for every single request.
  • Reduces memory allocations and CPU cycles spent on RNG initialization.

Benchmark Results:

  • Allocations: Reduced by 4 allocations per operation.
  • Throughput (Parallel): Improved by ~6.3% (12525 ns/op vs 13364 ns/op).
  • Throughput (Serial): Slight improvement (45729 ns/op vs 45906 ns/op).

This change improves performance under high concurrency by reducing GC pressure and initialization overhead.


PR created automatically by Jules for task 1036730633027740137 started by @sudorandom

Removed the per-request initialization of `gofakeit.New(0)` in `private/server/handler.go`. By leaving `Faker` field as `nil` in `GenOptions`, it now defaults to `gofakeit.GlobalFaker` which is thread-safe and reused, avoiding unnecessary allocations and seeding overhead.

Benchmarks show ~6% improvement in parallel execution throughput and reduced allocations by ~4 allocs/op.
Serial: ~45729 ns/op vs 45906 ns/op
Parallel: ~12525 ns/op vs 13364 ns/op

Co-authored-by: sudorandom <310004+sudorandom@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 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.

Increased the default `maxLen` in `String` generation from 20 to 255.
Previously, if a well-known type like Email exceeded 20 characters and no explicit `MaxLen` constraint was provided, it would be truncated, causing test failures (e.g., `filomenaheidenreich@...` becoming `filomenaheidenreich@`).

This change ensures that well-known types are generated with their natural length unless specifically constrained by a rule.

Co-authored-by: sudorandom <310004+sudorandom@users.noreply.github.com>
Comment thread gen_strings.go Outdated
Reverted the default `maxLen` change from 255 back to 20.
Instead, we now track if `Len`, `MaxLen`, or `MaxBytes` were explicitly set.
If they were NOT set, and the field is a `WellKnown` type (like Email), we skip the default 20-char truncation.
This ensures that unconstrained well-known types are generated with their natural length, while maintaining the default behavior for other string types.

Co-authored-by: sudorandom <310004+sudorandom@users.noreply.github.com>
@sudorandom sudorandom merged commit e66196c into main Feb 26, 2026
3 checks passed
@sudorandom sudorandom deleted the perf-gofakeit-optimization-1036730633027740137 branch February 26, 2026 06:44
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