emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] Add a mechanism for passing unibyte strings from lisp to mod


From: Brennan Vincent
Subject: Re: [PATCH] Add a mechanism for passing unibyte strings from lisp to modules.
Date: Wed, 26 Jun 2024 23:36:16 -0400

Eli Zaretskii <eliz@gnu.org> writes:

>> Date: Wed, 26 Jun 2024 15:33:09 +0200
>> From: tomas@tuxteam.de
>> Cc: brennan@umanwizard.com, acorallo@gnu.org, stefankangas@gmail.com,
>>      emacs-devel@gnu.org
>> 
>> > > > How will it be different from the Lisp vectors we already have?
>> > > 
>> > > The box around every byte.
>> > 
>> > What box?  Please tell more, as I don't think I follow.
>> 
>> Maybe I'm all wrong, but AFAIU, a vector can contain arbitrary Lisp
>> values. That makes 64bits/8bits plus boxing/unboxing (which is, I
>> assume, quick, but nonzero).
>> 
>> Having a specialized "array of bytes" (as there is one for bools)
>> might be beneficial for big arrays, and perhaps avoid big data moving
>> operations over the C/LISP fence.
>
> If you are saying that using 64-bit values there incurs a run-time
> performance penalty, then accessing bytes does that as well.  Someone
> should profile this and present evidence wrt the relative performance
> of these, then we can discuss whether the penalty is real and whether
> it is worth adding yet another data type to Emacs.

Sure, I wrote a quick benchmark that passes a 10MB buffer to a module
which just sums the bytes and returns and integer. It is about 200x
faster using a unibyte string (with my original patch) than a vector.

C code:

// Compile with gcc -O3 -fPIC -shared -o test-module.so test.c

#include <emacs-module.h>
#include <stdlib.h>

int plugin_is_GPL_compatible;

static emacs_value
Fcall_test(emacs_env *env, ptrdiff_t nargs, emacs_value args[], void *) 
EMACS_NOEXCEPT
{
    unsigned char sum = 0;
    emacs_value vec = args[0];
    size_t sz = env->vec_size(env, vec);
    for (int i = 0; i < sz; ++i)
         sum += env->extract_integer(env, env->vec_get(env, vec, i));
    return env->make_integer(env, sum);
}

static emacs_value
Fcall_test2(emacs_env *env, ptrdiff_t nargs, emacs_value args[], void *) 
EMACS_NOEXCEPT
{
    unsigned char sum = 0;
    emacs_value arr = args[0];
    char *buf;
    ptrdiff_t sz = 0;
    env->copy_unibyte_string_contents(env, arr, NULL, &sz);
    buf = malloc(sz);
    env->copy_unibyte_string_contents(env, arr, buf, &sz);
    for (int i = 0; i < sz - 1; ++i)
         sum += buf[i];
    return env->make_integer(env, sum);
}

/* bind c_func (native) to e_func (elisp) */
static void
bind(emacs_env *env, emacs_value (*c_func) (emacs_env *env,
                                            ptrdiff_t nargs,
                                            emacs_value args[],
                                            void *) EMACS_NOEXCEPT,
     const char *e_func,
     ptrdiff_t min_arity,
     ptrdiff_t max_arity,
     const char *doc,
     void *data)
{
    emacs_value fset_args[2];
    
    fset_args[0] = env->intern(env, e_func);
    fset_args[1] = env->make_function(env, min_arity, max_arity, c_func, doc, 
data);
    env->funcall(env, env->intern(env, "fset"), 2, fset_args);
}

int
emacs_module_init(struct emacs_runtime *ert)
{
    emacs_env *env = ert->get_environment(ert); 
    
    bind(env,
         Fcall_test, "btv--test", 1, 1,
         "test using vector",
         NULL);

    bind(env,
         Fcall_test2, "btv--test2", 1, 1,
         "test using byte array",
         NULL);

    emacs_value provide_arg = env->intern(env, "test-module");
    env->funcall(env, env->intern(env, "provide"), 1, &provide_arg);
    return 0;
}


Elisp code:

(require 'test-module)
(require 'benchmark)

(setq v (make-vector 10000001 37))
(setq v2 (make-string 10000001 37))

`(,(benchmark-elapse (btv--test v))
  ,(benchmark-elapse (btv--test2 v2)))


Result of evaluating elisp code:

(0.17861138 0.000805208)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]