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

Another big improvement to dynamic property access: Fix dynamic dispatch perf #36

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/blob.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ struct Blob{T}
end
end

# OPTIMIZATION: Annoyingly julia dispatches on a *Type Constructor* are very expensive.
# So if you ever have a type unstable field access on a Blob, we will not know the type of
# the child Blob we will return, meaning a dynamic dispatch. Dispatching on Blob{FT}(...)
# for an unknown FT is very expensive. So instead, we dispatch to this function, which will
# be cheap because it has only one method. Then this function calls the constructor.
# This is a silly hack. :')
Comment on lines +15 to +20
Copy link
Contributor

Choose a reason for hiding this comment

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

Love this comment, and looking forward to the pitfall going away! ❤️

make_blob(::Type{BT}, b::Blob, offset) where {BT} = Blob{BT}(b + offset)

function Blob(ref::Base.RefValue{T}) where T
Blob{T}(pointer_from_objref(ref), 0, sizeof(T))
end
Expand Down Expand Up @@ -169,7 +177,7 @@ end
end
i = fieldidx_lookup[field]
FT = fieldtype(T, i)
Blob{FT}(blob + blob_offset(T, i))
make_blob(FT, blob, blob_offset(T, i))
end
@noinline _throw_missing_field_error(T, field) = error("$T has no field $field")

Expand All @@ -180,7 +188,8 @@ end
@boundscheck if i < 1 || i > fieldcount(T)
_throw_getindex_boundserror(blob, i)
end
return Blob{fieldtype(T, i)}(blob + Blobs.blob_offset(T, i))
FT = fieldtype(T, i)
return make_blob(FT, blob, Blobs.blob_offset(T, i))
end

Base.@propagate_inbounds function Base.setindex!(blob::Blob{T}, value::T) where T
Expand Down
Loading