Skip to content
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

$xavi(...) #2337

Merged
merged 6 commits into from May 29, 2020
Merged

$xavi(...) #2337

merged 6 commits into from May 29, 2020

Conversation

linuxmaniac
Copy link
Member

Pre-Submission Checklist

  • Commit message has the format required by CONTRIBUTING guide
  • Commits are split per component (core, individual modules, libs, utils, ...)
  • Each component has a single commit (if not, squash them into one commit)
  • No commits to README files for modules (changes must be done to docbook files
    in doc/ subfolder, the README file is autogenerated)

Type Of Change

  • Small bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds new functionality)
  • Breaking change (fix or feature that would change existing functionality)

Checklist:

  • PR should be backported to stable branches
  • Tested changes locally

Description

This change introduces a new type of variable $xavi - eXtended Attribute Value Insensitive case.
It's like a $xavp but keys are case insensitive:

$xavi(WhatEver=>fOo) = 1;

if($xavi(whatever[0]=>foo) == 1) {
....
}

@linuxmaniac linuxmaniac force-pushed the vseva/xavi branch 3 times, most recently from 3880cb5 to 078d14d Compare May 27, 2020 09:19
@miconda
Copy link
Member

miconda commented May 29, 2020

I could not spot anything on a quick look over the patch. If nobody else wants to still keep it here for review, I am fine to merge it.

Copy link
Contributor

@henningw henningw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks all fine, but i noticed that several of the functions are more or less identical to the existing xavp code. This is of course to be expected, as the value works differs only in the case handling. I think it is worthwhile to try if it could be generalized.

What do you think about just using a generic function that gets a function pointer to a comparison or hash function? In some cases only the static core list is necessary. E.g. like this (pseudo-code to the relevant parts);

  • static sr_xavp_t *xavi_new_value(str *name, sr_xval_t *val, void * hash_func) { id = hash_func(name->s, name->len); }
  • int xavi_add(sr_xavp_t *xavi, sr_xavp_t *list, sr_avp_t core_list) { *core_list = xavi; }
  • static sr_xavp_t *xavi_get_internal(str *name, sr_xavp_t **list, int idx, sr_xavp_t **prv, void hash_func, void comp_func) { id = hash_func(..); if (.. && comp_func(...)) }

Using void* function pointers is of course not perfect, but this is what C gives us. In e.g. the DB1 API they are used in different functions in a similar way to prevent a lot of code duplication.

@miconda
Copy link
Member

miconda commented May 29, 2020

I prefer to have separate functions, instead of going to some generic void* callbacks. Those are hard to track and prone to introduce bugs in existing functional code. The duplicated code should not be really big in size and having new dedicated functions for comparison, etc ... will result also in extra size of the overall code.

I actually looked at reusing the xavp code when I implementing xavu recently and the code got a lot of messy IF-ELSE conditions everywhere, functions with extra parameters to indicate what type of variable, etc ... so I reverted and just made dedicated functions, everything afterwards being easier to follow.

@henningw
Copy link
Contributor

henningw commented May 29, 2020

I don't think the usage of function pointers is a big problem, but it was just an idea. If you need to pass to many "switch" type of variables, it gets hard to follow, sure.
The two functions xavi_add_value, xavi_add_value_after just operate on a global static list. This could be implemented type safe by just passing the list to it, without the need of void pointers.

@miconda
Copy link
Member

miconda commented May 29, 2020

In the case of the two functions you mention, the _xavi_list_crt is hidden in the c file (being declared static, since it is controlled in very special cases related to transaction states, ...), so if those functions are used outside of the file, then they still need wrappers to call a common function (for xavp and xavi) with the adequate list.

And my remark was targeting exactly this case, because the functions have like 10 effective lines of code. A wrapper function is like 3 lines of code, with a jump on the stack. So overall it is not a significant gain.

It makes sense to have a function for a small number of lines of code, if the function is used in a lot of places, but if it going to be used in 2-3 places, then it has to be a significant number of codes of lines to really worth it.

Of course, it can be seen as a personal preference, but I prefer code easier to follow/search/maintain instead of modularising every snippet of code.

@linuxmaniac linuxmaniac merged commit d61be42 into master May 29, 2020
@linuxmaniac linuxmaniac deleted the vseva/xavi branch May 29, 2020 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants