swfbridge和大文件 [英] swfbridge and large files

查看:94
本文介绍了swfbridge和大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在与 Alchemy 一起使用的C项目.该项目有一些我想使用swfbridge运行的构建后命令行测试.

I have a C project that I'm using with Alchemy. The project has some post-build command-line tests that I'd like to run using the swfbridge.

这些测试可以运行,但是它们非常慢.问题在于他们将一些中等大小的文件(〜3MB)读入内存.通过常规的Alchemy使用相同的文件运行这些相同的测试(例如,不使用swfbridge而是使用来自AS的supplyFile)非常快.

These tests run, but they're extremely slow. The problem is that they read some moderately large files (~3MB) into memory. Running these same tests with the same files via regular Alchemy (e.g., not using swfbridge but using supplyFile from AS) is very fast.

我认为瓶颈是swfbridge.更具体地说,以swfbridge加载文件的方式.它读取它们并将它们以1024字节的块大小通过localhost连接传输到主炼金术swf. (您可以在swfbridge.log中看到这种情况.)

I think the bottleneck is the swfbridge. More specicially, in the way that swfbridge loads files. It reads them in and transmits them in 1024 byte chunks across the localhost connection to the main alchemy swf. (You can see this happening in the swfbridge.log.)

我的问题是:有没有办法使swfbridge更有效率?例如,我可以使用不同的块大小吗?

My question is: is there a way to make swfbridge more efficient? Can I make it use a different chunk size, for example?

这是文件读取代码的示例.如果您给此代码提供一个〜3MB的文件,它将非常运行缓慢.

Here is an example of the file-reading code. If you give this code a ~3MB file it will run very slowly.

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

size_t filesize(const char filename[]) {
    struct stat stbuf;
    if (stat(filename, &stbuf) == -1) {
        fprintf(stdout, "file_size: can't find %s\n...\n", filename);
        return (-1);
    }
    return stbuf.st_size;
}

int main(int argc, char **argv) {
    const char *filename= argv[1];
    size_t size= filesize(filename);

    printf("allocating %d bytes \n", size); fflush(stdout);
    char *data= (char*)malloc(size);

    printf("reading %d bytes \n", size); fflush(stdout);
    FILE *file= fopen(filename, "r");
    fread(data, size, 1, file);

    printf("done \n"); fflush(stdout);
    free(data);
    fclose(file);

    return 0;
}

推荐答案

我想出了解决此问题的方法.

I figured out a workaround to this problem.

从命令行运行swf时,文件i/o确实很慢,因为swf进程正在通过localhost套接字从swfbridge进程获取文件.对此未进行优化(炼金术士们可能没想到有人会认真使用它).使用swfbridge以便stdin和stdout可以工作.但是我尚不完全清楚为什么它用于文件I/O(毕竟swf在adl(Air!)中运行)可以访问文件系统.

When running a swf from the command line, the file i/o is really slow because the swf process is getting the file from the swfbridge process via localhost sockets. Something about this is unoptimized (the Alchemy guys probably didn't expect anyone to use this in a serious way). swfbridge is used so that stdin and stdout will work. But I'm not completely clear on why its used for file i/o-- the swf is running in adl (Air!) after all-- it has file system access.

无论如何,可以使用swf在Air中运行的事实.我们可以使用出色的funopen通过Air方法(而不是通过swfbridge)路由fread/fwrite.它实际上是很多代码,但这是一个想法:

Anyway, the fact that the swf is running in Air can be used. We can route fread/fwrite through Air methods (rather than through swfbridge) by using the wonderful funopen. Its actually a good bit of code, but here's an idea of it:

FILE* air_fopen(const char filename[], const char mode[]) {
    AS3_Val file= AS3_FileFromPath(filename);

    AS3_Val FileModeClass= AS3_GetClass("flash.filesystem", "FileMode");
    AS3_Val fileMode     = AS3_GetS(FileModeClass, fopenModeToAirMode(mode));

    AS3_Val fileStream   = AS3_NewObject("flash.filesystem", "FileStream"); 
    AS3_CallTS("open", fileStream, "AS3ValType, AS3ValType", file, fileMode);

    AS3_Release(FileModeClass);
    AS3_Release(fileMode);  
    AS3_Release(file);

    return funopen(fileStream,
                   (funopen_read_t)air_fread, (funopen_write_t)air_fwrite,
                   (funopen_seek_t)air_fseek, (funopen_close)air_fclose);
} 

air_read是这样的:

where air_read is like this:

int air_fread(AS3_Val fileStream, char *dest, int size) {
    int bytesAvailable= AS3_GetIntProperty(fileStream, "bytesAvailable");
    if (bytesAvailable <= 0) {
        return 0;
    } else if (size > bytesAvailable) {
        size= bytesAvailable;
    }

    AS3_CallTS("readBytes", fileStream, "AS3ValType, IntType, IntType", AS3_Ram(), dest, size);

    return size;
}

另一个air_fwrite/air_fweek/air_fclose相似.请注意,其中一些功能(例如AS3_FileFromPath,AS3_GetInProperty,AS3_NewObject等)是我自己围绕AS3 api的简单包装.

The other air_fwrite/air_fweek/air_fclose are similar. Note that some of these functions (like AS3_FileFromPath, AS3_GetInProperty, AS3_NewObject, etc) are my own simple wrappers around the AS3 api.

这种方法消除了swfbridge瓶颈,并使命令行swfs的速度与普通swfs一样快.

This approach removes the swfbridge bottleneck and makes command-line swfs just as fast as normal ones.

这篇关于swfbridge和大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆