Skip to content

Major package refactor#2

Merged
davidbp merged 3 commits into
davidbp:mainfrom
lfenzo:package-src-restructure
Nov 9, 2025
Merged

Major package refactor#2
davidbp merged 3 commits into
davidbp:mainfrom
lfenzo:package-src-restructure

Conversation

@lfenzo
Copy link
Copy Markdown
Contributor

@lfenzo lfenzo commented Sep 14, 2025

  • Introduced new src/ organization for image hashing implementations
  • Removed inner modules AverageHash, DifferenceHash and PerceptualHash; now all functions are exported under ImageHashes.
  • Moved test dependencies - eg. TestImages, Test - from Project.toml to test/Project.toml
  • Reduced code duplication

This PR does not:

  • edit docs
  • change the public interface

@davidbp
Copy link
Copy Markdown
Owner

davidbp commented Sep 29, 2025

Hi @lfenzo, thanks for taking a look.
Did you check if there are any performance regressions out of the proposed refactoring?

@lfenzo
Copy link
Copy Markdown
Contributor Author

lfenzo commented Oct 3, 2025

Hi @lfenzo, thanks for taking a look. Did you check if there are any performance regressions out of the proposed refactoring?

Hello @davidbp,

I benchmarked it against main. As it turns out, the difference_* functions had some slight slowdown. I'll try to address that shortly!

main:

average_hash  1.173 μs (4 allocations: 416 bytes)
average_mathash  1.124 μs (7 allocations: 528 bytes)
difference_hash  1.268 μs (4 allocations: 432 bytes)   <-----
difference_mathash  1.204 μs (9 allocations: 704 bytes)  <-----
perceptual_hash  30.049 μs (60 allocations: 20.42 KiB)
perceptual_mathash  29.882 μs (65 allocations: 20.86 KiB)

PR:

average_hash  1.169 μs (4 allocations: 416 bytes)
average_mathash  1.123 μs (7 allocations: 528 bytes)
difference_hash  1.360 μs (10 allocations: 688 bytes)  <-----
difference_mathash  1.246 μs (10 allocations: 688 bytes)  <-----
perceptual_hash  30.190 μs (60 allocations: 20.42 KiB)
perceptual_mathash  30.232 μs (65 allocations: 20.86 KiB)
Benchmark Code
using ImageHashes
using TestImages
using BenchmarkTools

function run_benchmark(f, img)
    print("$(f)")
    @btime $f($img)
end

function main()
    functions = [
        average_hash,
        average_mathash,
        difference_hash,
        difference_mathash,
        perceptual_hash,
        perceptual_mathash,
    ]

    img = testimage("lighthouse")

    for f in functions
        run_benchmark(f, img)
    end
end

main()

@lfenzo
Copy link
Copy Markdown
Contributor Author

lfenzo commented Oct 21, 2025

Hello @davidbp!

I was able to reduce allocations only in difference_mathash, the difference_hash still allocates more. Haven't figured out a way to address that yet. Open to suggestions!

Performance difference:

                    PR (c64a155)                                main
difference_hash     1.299 μs (7 allocations: 560 bytes)  # vs.  1.268 μs (4 allocations: 432 bytes)
difference_mathash  1.187 μs (7 allocations: 560 bytes)  # vs.  1.204 μs (9 allocations: 704 bytes)

@davidbp
Copy link
Copy Markdown
Owner

davidbp commented Nov 9, 2025

The difference is negligible, good job!

@davidbp davidbp merged commit 6553e32 into davidbp:main Nov 9, 2025
2 checks passed
@lfenzo lfenzo deleted the package-src-restructure branch November 10, 2025 11:49
@lfenzo
Copy link
Copy Markdown
Contributor Author

lfenzo commented Nov 10, 2025

Thanks, @davidbp!

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