通过JNI有效传递大型C结构 [英] Passing large C structure through JNI efficiently

查看:198
本文介绍了通过JNI有效传递大型C结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的C结构(约40名成员,INT和char []),我有通过JNI传递给Java端进行进一步的处理。这屡禁不绝。 (我已经把的GetMethodID()等成的init()函数的C面,所以我只有他们调用一次。)

I have a large C structure (about 40 members, int and char[]) which I have to pass through JNI to the Java side for further processing. This happens repeatedly. (I already put the GetMethodID() etc. into an init() function on the C side so I only have to call them once.)

什么是做到这一点的有效途径?

What would be an efficient way to do this?

我能想到的:


  1. 的GetMethodID实例化与适当的成员一个Java类(...,<&初始化GT;,...)和传球的C结构的所有成员通过构造;

  2. 指定Java结构与相应的成员,并通过 SetXYZField初始化(公共)成员();

  3. ...

  1. Instantiating a Java class with the appropriate members through GetMethodID( ..., "<init>", ... ) and passing all members of the C structure through the constructor;
  2. Assigning a Java struct with the appropriate members, and initializing the (public) members through SetXYZField();
  3. ...

难道我忽略的东西吗? (这是我的第一次短兵相接用JNI)。难道真的没有有效的方式来同步C结构和Java结构?

Did I overlook something? (This is my first "close combat" with JNI.) Is there really no efficient way to "sync" a C structure and a Java structure?

什么是更有效的传递方法,1或2?在1的情况下,我应该通过 CallXYZMethod通过构造函数的参数()(参数列表)或 CallXYZMethodA()(参数数组)?

What is the more efficient passing method, 1. or 2.? In case of 1., should I pass constructor parameters through CallXYZMethod() (parameter list) or CallXYZMethodA() (argument array)?

谢谢你的投入。

编辑:略重述; 2.并不需要是一个类,当然,我也不需要严格在Java端的结构体,如果有一些其他的解决方案来处理在Java端C数据。

Reworded slightly; 2. doesn't need to be a class of course, and I don't strictly need a struct on the Java side if there is some other solution to handle the C data on the Java side.

编辑2:我做这在C ++,但结构本身从C-联动回调函数,即没有C ++茎 - 具体的问题。把这句改为C,而不是C ++。

Edit 2: I do this in C++, but the struct itself stems from a C-linkage callback function, i.e. nothing C++-specific to the problem. Rephrased to read "C" instead of "C++".

推荐答案

对于纯效率,最好尽量减少通过JNI去调用,因此最好的办法是将所有的数据或者通过选项1,创建一个新对象

For pure efficiency, it's best to minimize calls that go through JNI, so your best bet is sending all the data in either through option 1 and creating a new object.

您也可以有一个recieveUpdate(... 40参数...)方法上的,如果你想避免分配每次更新新对象的Java方面,虽然它的设计cringeworthy

You could also have a "recieveUpdate( ... 40 params ...)" method on the Java side if you wanted to avoid allocating a new object per update, though it's cringeworthy design.

这篇关于通过JNI有效传递大型C结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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