lua-vips icon indicating copy to clipboard operation
lua-vips copied to clipboard

new_from_buffer() VS new_from_file()

Open uasan opened this issue 8 years ago • 15 comments

Hello. The lua worker, read bytes from stream pipe, we know length buffer, but not know image type.

How best to do, create an image object, new_from_buffer(buff) or file.write(buff) and new_from_file(name)?

Thank.

uasan avatar Jun 07 '17 16:06 uasan

I would read the stream into a huge Lua string, then call new_from_buffer. It'll guess the file type for you.

jcupitt avatar Jun 07 '17 16:06 jcupitt

Ok, can know the type of the image, by the first bytes from the buffer, so as not to read the entire buffer, if the first bytes are not correct?

uasan avatar Jun 07 '17 16:06 uasan

It varies with the image format, but the first 1000 bytes should be enough, and not take too long.

jcupitt avatar Jun 07 '17 16:06 jcupitt

Thank!

Through systemd, we are very easy from Lua script, make pipe server, new process luajit for each connection.

vips-server.socket https://www.freedesktop.org/software/systemd/man/systemd.socket.html

[Unit]
Description = Vips socket

[Socket]
Accept       = yes
KeepAlive    = yes
ListenStream = /run/vips.sock

[Install]
WantedBy = sockets.target

vips-server@ .service https://www.freedesktop.org/software/systemd/man/systemd.service.html

[Unit]
Description = Vips server

[Service]
Type           = simple
NonBlocking    = yes
StandardInput  = socket
StandardOutput = socket
StandardError  = journal
ExecStart      = /usr/bin/luajit /var/www/vips-server.lua

[Install]
WantedBy = multi-user.target

vips-server.lua

while true do

	local buff = io.read()
	print('ECHO: ' .. buff)
	io.flush()

end

If this is interesting, then we can put an example.

uasan avatar Jun 07 '17 17:06 uasan

Sure, it sounds fun. Open a pull request with your example and I'll add it.

jcupitt avatar Jun 08 '17 08:06 jcupitt

Oк, later when I'm free I will make an example.

Maybe more interesting inotify server https://www.freedesktop.org/software/systemd/man/systemd.path.html

uasan avatar Jun 09 '17 09:06 uasan

I have one more question )

We receive traffic in chunks of 9 kb, we can process images also in chunks?

Traffic speed 10 Mb = 1 sec

Process image speed 10MB = 0.7 sec

If we parallels in the process of obtaining chunks and processing chunks, then will finish the work in 1 second, now in successive work take 1.7 seconds.

Is it possible to process images with chunks?

uasan avatar Jun 09 '17 09:06 uasan

Unfortunately not. This has been discussed quite a bit, but for now you need to read the entire image to a string, then start vips on it.

There is a branch that adds true streaming, but it's quite old now. Maybe we should look at this again.

jcupitt avatar Jun 09 '17 10:06 jcupitt

The most recent discussion around this is here, fwiw:

https://github.com/lovell/sharp/issues/179

jcupitt avatar Jun 09 '17 10:06 jcupitt

You can still overlap read and process, you just need to buffer a couple of images in memory.

You can have one thread reading into buffer1, and another thread processing from buffer2. When an image has been completely read, check that the processing thread has finished on buffer2, then swap the buffers and start reading and processing again.

I've not tried threading in Lua, does it support this kind of thing?

jcupitt avatar Jun 09 '17 10:06 jcupitt

I have not tried threads in Lau, but i will try. This all the calculations are theoretical, only experiment in practice get real result, for calculate benefits.

I thought, there is an excellent and popular web server Nginx - 80% all sites, it has a thread pool and the ability to load dynamic modules: https://www.nginx.com/blog/thread-pools-boost-performance-9x/ https://www.nginx.com/blog/compiling-dynamic-modules-nginx-plus/

It has a built-in very popular module for image processing by libgd (slow) http://nginx.org/en/docs/http/ngx_http_image_filter_module.html

If you make Vips-Nginx module, then many web developers, do not need binding to programming languages anymore, most developers will use lazy image processing in the Vips-Nginx module threads pool.

If you're interested in here is a guide http://nginx.org/en/docs/dev/development_guide.html

Thank, i will try threads in Lau.

uasan avatar Jun 09 '17 10:06 uasan

Yes, a nginx module sounds fun. I don't have time though :(

jcupitt avatar Jun 09 '17 11:06 jcupitt

Nginx module can be as HTTP middleware, that gets REST requests from browsers or web backend to image processing, then node-sharp, php-vips, and others, will not be much needed, Nginx is 80% servers.

I hope you will have lot free time :)

uasan avatar Jun 09 '17 11:06 uasan

Well, you could have a go, if you like. It sounds like it could be an easy way to get at least a little famous :)

jcupitt avatar Jun 09 '17 11:06 jcupitt

I do not know C lang :)

I also know how to use and customize many functional in Nginx to cover many to use-case.

In this I can help, if development begins.

uasan avatar Jun 09 '17 11:06 uasan