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

contrib/go-redis/redis.v8: optimize BeforeProcess and BeforeProcessPipeline #920

Merged
merged 1 commit into from
May 11, 2021

Conversation

johejo
Copy link
Contributor

@johejo johejo commented May 7, 2021

This change includes the following three optimizations.

  1. The previous implementation split the command with strings.Split, but only
    the 0th element is actually needed as a tag for span.
    Therefore, use strings.IndexByte to find the first space and slice the raw
    command string.

  2. Replace the length derivation of args with the counting of the number of
    spaces.

  3. The number of ddtrace.StartSpanOptions is often pre-determined,
    so make it in advance to reduce allocation.

Benchmark

func BenchmarkBeforeProcess(b *testing.B) {
	opts := &redis.Options{Addr: "127.0.0.1:6379"}
	client := redis.NewClient(opts)
	ctx := context.Background()
	cmd := redis.NewStringCmd(ctx, "set", "key", "value", 99)
	cfg := new(clientConfig)
	defaults(cfg)
	ddh := &datadogHook{params: &params{additionalTags: additionalTagOptions(client), config: cfg}}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, err := ddh.BeforeProcess(ctx, cmd)
		if err != nil {
			b.Fatal(err)
		}
	}
}

Result

name             old time/op    new time/op    delta
BeforeProcess-4     568ns ± 1%     447ns ± 1%  -21.30%  (p=0.000 n=9+10)

name             old alloc/op   new alloc/op   delta
BeforeProcess-4      640B ± 0%      512B ± 0%  -20.00%  (p=0.000 n=10+10)

name             old allocs/op  new allocs/op  delta
BeforeProcess-4      15.0 ± 0%      13.0 ± 0%  -13.33%  (p=0.000 n=10+10)

@johejo johejo force-pushed the optimize_contrib_go_redis_v8 branch from 700248c to 6e87961 Compare May 7, 2021 23:42
Copy link
Contributor

@gbbr gbbr left a comment

Choose a reason for hiding this comment

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

This is nice, thanks! Just one nit, otherwise all good by me.

contrib/go-redis/redis.v8/redis.go Outdated Show resolved Hide resolved
@gbbr gbbr added this to the 1.31.0 milestone May 10, 2021
@johejo johejo force-pushed the optimize_contrib_go_redis_v8 branch from 6e87961 to 8f73a07 Compare May 10, 2021 11:29
@johejo johejo requested a review from gbbr May 10, 2021 11:37
…peline

This change includes the following three optimizations.

1. The previous implementation split the command with strings.Split, but only
   the 0th element is actually needed as a tag for span.
   Therefore, use strings.IndexByte to find the first space and slice the raw
   command string.

2. Replace the length derivation of args with the counting of the number of
   spaces.

3. The number of ddtrace.StartSpanOptions is often pre-determined, so make it
   in advance to reduce allocation.
@johejo johejo force-pushed the optimize_contrib_go_redis_v8 branch from 8f73a07 to 5966af7 Compare May 10, 2021 17:48
Copy link
Contributor

@gbbr gbbr left a comment

Choose a reason for hiding this comment

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

Real nice! Thanks for posting the benchmarks.

@gbbr gbbr merged commit 4ba66f6 into DataDog:v1 May 11, 2021
stlimtat pushed a commit to stlimtat/dd-trace-go that referenced this pull request May 17, 2021
* 'v1' of https://github.com/DataDog/dd-trace-go:
  Implement DD_PROFILING_OUTPUT_DIR for dev/debug (DataDog#924)
  contrib/go-pg/pg.v10: add INTEGRATION flag check for tests (DataDog#921)
  contrib/go-redis/redis.v8: optimize BeforeProcess and BeforeProcessPipeline (DataDog#920)
  CI: check go.sum is up-to-date (DataDog#918)
  README: Fix go get instructions (DataDog#913)
  internal/log: Improve API for testing (DataDog#916)
  ddtrace/tracer: add support for agent discovery endpoint, feature flags, stats & drops (DataDog#859)
  internal/version: bump to v1.31.0 (DataDog#910)
  CONTRIBUTING.md: add link to contrib/README.md (DataDog#802)
  profiler: Disable agentless mode for WithAPIKey (DataDog#906)
  contrib/Shopify/sarama: fix possible deadlock in WrapAsyncProducer (DataDog#907)
  contrib/database/sql.tracedConn implement driver.SessionResetter (DataDog#908)
  ddtrace/opentracer: consider FollowsFrom references as children (DataDog#905)
  ddtrace/opentracer: add support for opentracing.TracerContextWithSpanExtension (DataDog#855)
  ddtrace/tracer: follow noDebugStack setting when using SetTag with an error (DataDog#900)
  contrib/net/http: add a getter to retrieve original round tripper (DataDog#903)
  ddtrace/tracer: ensure Flush call is synchronous (DataDog#901)
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.

2 participants