Skip to content
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

improvement: Update interface{} casting to avoid runtime panic #5

Merged
merged 4 commits into from
Sep 5, 2024

Conversation

GokselKUCUKSAHIN
Copy link
Member

@GokselKUCUKSAHIN GokselKUCUKSAHIN commented Sep 4, 2024

Description

This PR enhances type safety when accessing the map fields.

Before:

if matchAll, exists := m["match_all"]; exists {
	matchAll.(Object)[key] = value
}

In this example, if match_all exists but isn’t castable to Object, a runtime panic occurs.

After:

if matchAll, ok := m["match_all"].(Object); ok {
	matchAll[key] = value
}

This change ensures that the key exists and is safely cast to Object before modifying it, preventing potential panics.

… ok := possibleMap[key].(type)` to avoid run time panics
Copy link

codecov bot commented Sep 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (7c0ee14) to head (7a143dc).
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main        #5   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           18        18           
  Lines          403       401    -2     
=========================================
- Hits           403       401    -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@GokselKUCUKSAHIN GokselKUCUKSAHIN changed the title Improvement/ Update inteface{} casting to avoid run time panic Improvement/ Update inteface{} casting to avoid runtime panic Sep 4, 2024
@GokselKUCUKSAHIN GokselKUCUKSAHIN changed the title Improvement/ Update inteface{} casting to avoid runtime panic Improvement/ Update inteface{} casting to avoid runtime panic Sep 4, 2024
@GokselKUCUKSAHIN GokselKUCUKSAHIN changed the title Improvement/ Update inteface{} casting to avoid runtime panic Improvement/ Update interface{} casting to avoid runtime panic Sep 4, 2024
@GokselKUCUKSAHIN GokselKUCUKSAHIN changed the title Improvement/ Update interface{} casting to avoid runtime panic improvement: Update interface{} casting to avoid runtime panic Sep 4, 2024
@GokselKUCUKSAHIN
Copy link
Member Author

GokselKUCUKSAHIN commented Sep 4, 2024

it also improved performance

previous benchmark scores.
image

with this update

Running benchmark for ./benchmarks/simple_example_test.go
goos: darwin
goarch: arm64
Benchmark_Simple_Builder-10    	12731811	       466.7 ns/op
Benchmark_Simple_Vanilla-10    	16040830	       374.3 ns/op
Benchmark_Simple_esquery-10    	 1948323	      3082 ns/op
PASS
ok  	command-line-arguments	21.939s
Waiting for 30 seconds before the next test...
Running benchmark for ./benchmarks/intermediate_example_test.go
goos: darwin
goarch: arm64
Benchmark_Intermediate_Builder-10    	 3747408	      1598 ns/op
Benchmark_Intermediate_Vanilla-10    	 4454670	      1347 ns/op
Benchmark_Intermediate_esquery-10    	  854839	      6974 ns/op
PASS
ok  	command-line-arguments	21.023s
Waiting for 30 seconds before the next test...
Running benchmark for ./benchmarks/complex_example_test.go
goos: darwin
goarch: arm64
Benchmark_Complex_Builder-10    	 1776082	      3381 ns/op
Benchmark_Complex_Vanilla-10    	 2312140	      2596 ns/op
Benchmark_Complex_esquery-10    	  470833	     12724 ns/op
PASS
ok  	command-line-arguments	24.182s
Waiting for 30 seconds before the next test...
Running benchmark for ./benchmarks/mixed_test.go
goos: darwin
goarch: arm64
Benchmark_Mixed_Example_Builder-10    	 2866080	      2173 ns/op
Benchmark_Mixed_Example_Vanilla-10    	 3294189	      1755 ns/op
Benchmark_Mixed_Example_esquery-10    	 1000000	      5200 ns/op
PASS
ok  	command-line-arguments	21.241s
Waiting for 30 seconds before the next test...
Running benchmark for ./benchmarks/conditional_example_test.go
goos: darwin
goarch: arm64
Benchmark_Conditional_Builder-10    	 2990798	      2005 ns/op
Benchmark_Conditional_Vanilla-10    	 3991286	      1502 ns/op
Benchmark_Conditional_esquery-10    	  824540	      7150 ns/op
PASS
ok  	command-line-arguments	21.523s
Waiting for 30 seconds before the next test...
Running benchmark for ./benchmarks/aggs_example_test.go
goos: darwin
goarch: arm64
Benchmark_Aggs_Example_Builder-10    	 2013816	      2965 ns/op
Benchmark_Aggs_Example_Vanilla-10    	 2584550	      2317 ns/op
Benchmark_Aggs_Example_esquery-10    	  612189	      9784 ns/op
PASS
ok  	command-line-arguments	23.425s
Waiting for 30 seconds before the next test...
All benchmarks completed.

@GokselKUCUKSAHIN GokselKUCUKSAHIN merged commit 0f11389 into main Sep 5, 2024
8 checks passed
@GokselKUCUKSAHIN GokselKUCUKSAHIN deleted the feature/panic-safe-casting branch September 5, 2024 18:04
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