-
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performance Improvement when writing #26
Conversation
That's great, i'll merge and perform a release tomorrow. Thank you so much for your contribution! |
No worries! If I can find something else to improve I will give a look, also here's the performances on C: Tested with the following code: #include <gpiod.h>
#include <error.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
struct gpiod_chip *chip;
struct gpiod_line_request_config config;
struct gpiod_line_bulk lines;
int main(int argc, char *argv[]) {
unsigned int offsets[1];
int values[1];
int err;
chip = gpiod_chip_open("/dev/gpiochip4");
if (!chip) {
perror("gpiod_chip_open");
goto cleanup;
}
// set pin 21 to 1 (logic high)
offsets[0] = 17;
values[0] = 0;
err = gpiod_chip_get_lines(chip, offsets, 1, &lines);
if (err) {
perror("gpiod_chip_get_lines");
goto cleanup;
}
memset(&config, 0, sizeof(config));
config.consumer = "blink";
config.request_type = GPIOD_LINE_REQUEST_DIRECTION_OUTPUT;
config.flags = 0;
// get the bulk lines setting default value to 0
err = gpiod_line_request_bulk(&lines, &config, values);
if (err) {
perror("gpiod_line_request_bulk");
goto cleanup;
}
// Loop continuously to toggle the LED on and off
while (1) {
// output value 1 to turn on the LED
values[0] = 1;
err = gpiod_line_set_value_bulk(&lines, values);
if (err) {
perror("gpiod_line_set_value_bulk");
goto cleanup;
}
// output value 0 to turn off the LED
values[0] = 0;
err = gpiod_line_set_value_bulk(&lines, values);
if (err) {
perror("gpiod_line_set_value_bulk");
goto cleanup;
}
}
cleanup:
gpiod_line_release_bulk(&lines);
gpiod_chip_close(chip);
return EXIT_SUCCESS;
} I guess NaN is also giving some overhead... |
are you sure? obj->line can be null |
I am well aware of that, didn't test the case tho. |
Thats not too bad at all really. But I still think its worth throwing a check for null line in before calling setValue, it shouldnt cost much. I havent checked the compiler output but it should be a single instruction on most architectures (some variation of
Unless of course you have verified the safety of calling that function with NULL. |
@alessandromrc the performance gains are remarkable,. the null check however would be valuable for people with different purposes. would be nice to restore the null check before the merge, or i can merge now and restore the check before making a release so everyone else can benefit from the improvements you wrote! |
I will check right now with the value being null and see what happens, otherwise I will think of something to check it being null without adding much overhead. |
Just tested with null and it's the same behaviour as before, null in C++ is just 0... can be tested with this code here: const { version, Chip, Line } = require("node-libgpiod");
global.chip = new Chip(4);
global.line = new Line(chip, 17); // led on GPIO17
let count = 10;
console.log(version());
line.requestOutputMode();
let value_out = false;
while (true)
{
value_out = 1
line.setValue(null);
value_out = 0
line.setValue(null);
} NAN_METHOD(Line::setValue) {
Line *obj = ObjectWrap::Unwrap<Line>(info.Holder());
v8::Local<v8::Context> context = Nan::GetCurrentContext();
uint32_t value = info[0]->Uint32Value(context).FromJust();
std::cout << value << std::endl;
if (gpiod_line_set_value(obj->line, value) == -1) {
return Nan::ThrowError("setValue() failed.");
}
} Result: 0 Running tests:
|
Like mentioned before C++ compilers generally see null as a nullptr or as 0, in either cases it will be handled as 0, can also be seen here: https://en.cppreference.com/w/cpp/types/NULL |
@alessandromrc is it ok if i merge now? |
To me everything seems to work fine, if anyone else wants to test out the changes (maybe even outside the Raspberry world, that's up to them / you). |
people can test in v0.4.1 release 😆 |
Let's hope for the best 😃 |
Before changes:
After changes:
By adding compiler optimisations and changing a little the way the library writes the GPIO state we achieved some performances easily
Measurements done on an Analog Discovery 3.