Re: EuSQLite

new topic     » goto parent     » topic index » view thread      » older message » newer message

Ray Smith wrote:

> Juergen Luethje wrote:
> > Below there are some other functions which might be faster.
> > I don't know whether that really matter, though.
> > 
> > Regards,
> >    Juergen
> 
> Hi Juergen,
> 
> The "len" variable in the original peek_sequence really was meant to be
> "max_len".
> It didn't need to return a sequence of len, only not > len.
> 
> So peek_sequence1 is not a valid substitute.
> 
> Your peek_sequence2 and 3 look much better though.
> 
> I'd "assume" (I know we should never do that) but peek_sequence3 looks the
> faster
> ... 
> the only problem being it isn't portable to Linux or FreeBSD.

<snip>

Hi Ray and Jesus,

I solved that problem by writing a small cross-platform length()
function for null-terminated strings using ASM. Like you Ray,
currently I only assume that this function is the fastest
(but I think it's an educated guess). :)

-- * Euphoria 2.4 Official Release or later required *
--   (because parameters are passed to machine code)

include machine.e
include dll.e

------------------------------------------------------------------------
constant SL_CODE = allocate(26)

poke(SL_CODE, {
     -- After the routine has pushed 4 bytes on the stack, first int
     -- argument is at stack offset +8.
     #57,                        -- push   edi
     #8B, #7C, #24, #08,         -- mov    edi, [esp+8]
     #B9, #FF, #FF, #FF, #FF,    -- mov    ecx, #FFFFFFFF
     #B0, #00,                   -- mov    al, 0
     #FC,                        -- cld
     #F2, #AE,                   -- repne  scasb
     #B8, #FE, #FF, #FF, #FF,    -- mov    eax, #FFFFFFFE
     #29, #C8,                   -- sub    eax, ecx
     #5F,                        -- pop    edi
     #C2, #04, #00               -- ret    4    -- pop 4 bytes off the stack
   })

global constant
   STRLEN = define_c_func("", SL_CODE, {C_POINTER}, C_ULONG)

-- Usage:
-- len = c_func(STRLEN, {addr})
--
-- 'addr' is a pointer to the first character of a null-terminated
-- string in memory;
-- returns the length of the string in bytes (not including the
-- terminating null character) 

------------------------------------------------------------------------

global function peek_string (atom addr)
   -- read a null-terminated string, starting at address 'addr' in memory
   return peek({addr, c_func(STRLEN,{addr})})   
end function

------------------------------------------------------------------------


-- Demo
sequence s
atom addr

s = "Hello"
-- s = repeat('x', 90000)

addr = allocate_string(s)
printf(1, "len = %d", {c_func(STRLEN,{addr})}) 
free(addr)


Regards,
   Juergen

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu