-
Notifications
You must be signed in to change notification settings - Fork 27
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
Optimize dispose logic #48
Conversation
Benchmark is bogus (ran a minute ago and not optimized was fastest):
|
{ | ||
public void Dispose(T value) | ||
{ | ||
if (typeof(T) is IDisposable) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stupid :) - this is checking if Type is IDisposable, because the result of typeof(T) is an instance of Type.
Wrapping the standard if value is IDisposable check into a generic is equal speed/code size:
And it looks like it does decrease the size of the code by about 4% (from running the LRU bench with DisassemblyDiagnoser): With Disposer:
Original:
The actual runtime is pretty much identical - within the variance from running the same thing over and over again. |
Generated assembly is the same as hand written code: ; BitFaster.Caching.Benchmarks.DisposerBench.HandWritten()
push rsi
sub rsp,20
xor esi,esi
M00_L00:
mov rcx,offset MT_BitFaster.Caching.Benchmarks.Disposable
call CORINFO_HELP_NEWSFAST
cmp byte ptr [rax+8],0
jne short M00_L01
mov byte ptr [rax+8],1
M00_L01:
inc esi
cmp esi,3E8
jl short M00_L00
add rsp,20
pop rsi
ret
; Total bytes of code 48 ; BitFaster.Caching.Benchmarks.DisposerBench.GenericDisposerStdCheck()
push rsi
sub rsp,20
xor esi,esi
M00_L00:
mov rcx,offset MT_BitFaster.Caching.Benchmarks.Disposable
call CORINFO_HELP_NEWSFAST
cmp byte ptr [rax+8],0
jne short M00_L01
mov byte ptr [rax+8],1
M00_L01:
inc esi
cmp esi,3E8
jl short M00_L00
add rsp,20
pop rsi
ret
; Total bytes of code 48 |
Experiment - can JIT elide code like: