Skip to content

Commit

Permalink
use CRC32c hashing directly instead of StableHashTraits (#3667)
Browse files Browse the repository at this point in the history
* use CRC32c hashing directly instead of StableHashTraits

* remove Point2d import

* add changelog
  • Loading branch information
SimonDanisch committed Mar 5, 2024
1 parent 86a66eb commit 9cf688d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,8 @@
## [Unreleased]
- Added supported markers hint to unsupported marker warn message.

- Remove StableHashTraits in favor of calculating hashes directly with CRC32c [#3667](https://github.com/MakieOrg/Makie.jl/pull/3667).

## [0.20.8] - 2024-02-22

- Fixed excessive use of space with HTML image outputs [#3642](https://github.com/MakieOrg/Makie.jl/pull/3642).
Expand Down
4 changes: 1 addition & 3 deletions Project.toml
Expand Up @@ -51,7 +51,6 @@ ShaderAbstractions = "65257c39-d410-5151-9873-9b3e5be5013e"
Showoff = "992d4aef-0814-514b-bc4d-f2e9a6c4116f"
SignedDistanceFields = "73760f76-fbc4-59ce-8f25-708e95d2df96"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StableHashTraits = "c5dd0088-6c3f-4803-b00e-f31a60c170fa"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
Expand All @@ -72,10 +71,10 @@ DelaunayTriangulation = "0.8.7"
Distributions = "0.17, 0.18, 0.19, 0.20, 0.21, 0.22, 0.23, 0.24, 0.25"
DocStringExtensions = "0.8, 0.9"
Downloads = "1, 1.6"
FFMPEG_jll = "4, 6"
FileIO = "1.6"
FilePaths = "0.8"
FixedPointNumbers = "0.6, 0.7, 0.8"
FFMPEG_jll = "4, 6"
Format = "1.3"
FreeType = "3.0, 4.0"
FreeTypeAbstraction = "0.10"
Expand Down Expand Up @@ -107,7 +106,6 @@ ShaderAbstractions = "0.4"
Showoff = "0.3, 1.0.2"
SignedDistanceFields = "0.4"
SparseArrays = "1.0, 1.6"
StableHashTraits = "1.1"
Statistics = "1, 1.6"
StatsBase = "0.31, 0.32, 0.33, 0.34"
StatsFuns = "0.9, 1.0"
Expand Down
1 change: 1 addition & 0 deletions src/Makie.jl
Expand Up @@ -107,6 +107,7 @@ assetpath(files...) = normpath(joinpath(ASSETS_DIR, files...))

include("documentation/docstringextension.jl")
include("utilities/quaternions.jl")
include("utilities/stable-hashing.jl")
include("bezier.jl")
include("types.jl")
include("utilities/texture_atlas.jl")
Expand Down
9 changes: 6 additions & 3 deletions src/bezier.jl
@@ -1,5 +1,3 @@
using StableHashTraits

const Point2d = Point2{Float64}

struct MoveTo
Expand Down Expand Up @@ -39,6 +37,11 @@ EllipticalArc(cx, cy, r1, r2, angle, a1, a2) = EllipticalArc(Point2d(cx, cy),
struct ClosePath end
const PathCommand = Union{MoveTo, LineTo, CurveTo, EllipticalArc, ClosePath}

# For hashing with crc32c
function Base.write(io::IO, command::PathCommand)
write(io, Ref(command))
end

function bbox(commands::Vector{PathCommand})
prev = commands[1]
bb = nothing
Expand Down Expand Up @@ -109,7 +112,7 @@ struct BezierPath
hash::UInt32
function BezierPath(commands::Vector)
c = convert(Vector{PathCommand}, commands)
return new(c, bbox(c), StableHashTraits.stable_hash(c; alg=crc32c, version=2))
return new(c, bbox(c), hash_crc32(c))
end
end
bbox(x::BezierPath) = x.boundingbox
Expand Down
20 changes: 20 additions & 0 deletions src/utilities/stable-hashing.jl
@@ -0,0 +1,20 @@
using CRC32c

# Seems like crc32c is slow enough, that it's worthwhile to memoize the hashes
const MEMOIZED_HASHES = Dict{Any,UInt32}()

function fast_stable_hash(x)
return get!(MEMOIZED_HASHES, x) do
return hash_crc32(x)
end
end

# Anything writeable to IO can be hashed
function hash_crc32(arrays::Union{AbstractVector,Tuple})
io = IOBuffer()
for array in arrays
write(io, array)
end
seekstart(io)
return CRC32c.crc32c(io)
end
12 changes: 1 addition & 11 deletions src/utilities/texture_atlas.jl
@@ -1,4 +1,4 @@
const SERIALIZATION_FORMAT_VERSION = "v6"
const SERIALIZATION_FORMAT_VERSION = "v7"

struct TextureAtlas
rectangle_packer::RectanglePacker{Int32}
Expand Down Expand Up @@ -288,16 +288,6 @@ function glyph_uv_width!(atlas::TextureAtlas, b::BezierPath)
return atlas.uv_rectangles[glyph_index!(atlas, b)]
end


# Seems like StableHashTraits is so slow, that it's worthwhile to memoize the hashes
const MEMOIZED_HASHES = Dict{Any, UInt32}()

function fast_stable_hash(x)
return get!(MEMOIZED_HASHES, x) do
return StableHashTraits.stable_hash(x; alg=crc32c, version=2)
end
end

function insert_glyph!(atlas::TextureAtlas, glyph, font::NativeFont)
glyphindex = FreeTypeAbstraction.glyph_index(font, glyph)
hash = fast_stable_hash((glyphindex, FreeTypeAbstraction.fontname(font)))
Expand Down

0 comments on commit 9cf688d

Please sign in to comment.