How to optimize this code?

# REPL
using VideoIO
using Images
using LSHFunctions
frms = Vector{UInt64}(undef, 25000);
hashfn = LSHFunction(cossim, 64);
function one_pixes(x::Matrix{RGB{Float32}})::Bool
               pix = x[1,1]
               for x_el in x
                       if x_el != pix return false; end
               end
               return true
       end
function frmkeyb(hfn::SimHash{Float32}, im::PermutedDimsArray{RGB{N0f8}, 2, (2, 1), (2, 1), Matrix{RGB{N0f8}}})::UInt64
               # @show typeof(im)
               h,w = size(im)
               im = im[h÷8:h*7÷8, w÷8:w*7÷8]
               h, w = size(im)
               h ÷= 20
               w ÷= 20
               resized = [RGB{Float32}(sum(im[i:i+h-1, j:j+w-1])/(h*w))
                       for (i,j) in Iterators.product(1:h:size(im)[1]-h+1, 1:w:size(im)[2]-w+1)]
               one_pixes(resized) ? zero(UInt64) : reduce((x, y)->(UInt64(x) <<1 ) | UInt64(y), hfn(reshape(rawview(channelview(resized)), :)))
       end
res(hashfn, video_path::String) = VideoIO.openvideo(video_path, thread_count=1) do vr
                                       img = read(vr);
                                       
                                       frms[1] = frmkeyb(hashfn, img)
                                       cnt = 1
                                       while !eof(vr)
                                               cnt += 1
                                               read!(vr, img);
                                               frms[cnt] = frmkeyb(hashfn, img);
                                       end
                                       (0x0032, copy(frms[1:cnt-25]))
                               end;
@time res(hashfn, "https://adxvideo.dataeye.com/6961/1f7a48d541dd71c5025f119a9fe7ddd9.mp4");

Now, In my pc, it output as follows
5.338815 seconds (3.15 M allocations: 2.664 GiB, 1.62% gc time, 0.84% compilation time)

you may take views on your array slices (otherwise it creates a new array)

With view function, as following

function frmkeyb(hfn::SimHash{Float32}, im::PermutedDimsArray{RGB{N0f8}, 2, (2, 1), (2, 1), Matrix{RGB{N0f8}}})::UInt64
                      # @show typeof(im)
                      h,w = size(im)
                      im = view(im, h÷8:h*7÷8, w÷8:w*7÷8)
                      h, w = size(im)
                      h ÷= 20
                      w ÷= 20
                      resized = [RGB{Float32}(sum(view(im, i:i+h-1, j:j+w-1))/(h*w))
                              for (i,j) in Iterators.product(1:h:size(im)[1]-h+1, 1:w:size(im)[2]-w+1)]
                      one_pixes(resized) ? zero(UInt64) : reduce((x, y)->(UInt64(x) <<1 ) | UInt64(y), hfn(reshape(rawview(channelview(resized)), :)))
              end

output is:
5.123041 seconds (3.46 M allocations: 120.359 MiB, 0.42% gc time, 0.50% compilation time)

Thank you!

5.123041 seconds (3.46 M allocations: 120.359 MiB, 0.42% gc time, 0.50% compilation time)

Maybe you are measuring download time?

Yes, download time is not important.
Actually, the video is a local file in my project.

@code_warntype res(hashfn, “https://adxvideo.dataeye.com/6961/1f7a48d541dd71c5025f119a9fe7ddd9.mp4”);
MethodInstance for res(::SimHash{Float32}, ::String)
from res(hashfn, video_path::String) in Main at REPL[33]:1
Arguments
#self#::Core.Const(res)
hashfn::SimHash{Float32}
video_path::String
Locals
#13::var"#13#14"{SimHash{Float32}}
Body::Tuple{UInt16, Any}
1 ─ %1 = Main.:(var"#13#14")::Core.Const(var"#13#14")
│ %2 = Core.typeof(hashfn)::Core.Const(SimHash{Float32})
│ %3 = Core.apply_type(%1, %2)::Core.Const(var"#13#14"{SimHash{Float32}})
│ (#13 = %new(%3, hashfn))
│ %5 = #13::var"#13#14"{SimHash{Float32}}
│ %6 = (:thread_count,)::Core.Const((:thread_count,))
│ %7 = Core.apply_type(Core.NamedTuple, %6)::Core.Const(NamedTuple{(:thread_count,)})
│ %8 = Core.tuple(1)::Core.Const((1,))
│ %9 = (%7)(%8)::Core.Const((thread_count = 1,))
│ %10 = VideoIO.openvideo::Core.Const(VideoIO.openvideo)
│ %11 = Core.kwfunc(%10)::Core.Const(VideoIO.var"#openvideo##kw"())
│ %12 = VideoIO.openvideo::Core.Const(VideoIO.openvideo)
│ %13 = (%11)(%9, %12, %5, video_path)::Core.PartialStruct(Tuple{UInt16, Any}, Any[Core.Const(0x0032), Any])
└── return %13

Why is it type unstable? (Body::Tuple{UInt16, Any})