Re: Executing Arbitrary Machine Code in Guile

2015-08-22 Thread tomas
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On Sat, Aug 22, 2015 at 08:46:52AM +0200, Panicz Maciej Godek wrote:
> 2015-08-22 1:31 GMT+02:00 Elzair :
> 
> > The other day I came across a page on Rosetta Code showing how to directly
> > execute x86 instructions across several languages:
> > http://rosettacode.org/wiki/Machine_code
> >
> >
> I wrote an extension that allows to do that on Linux. You can check it out
> here:
> https://bitbucket.org/panicz/envy/src/
> 
> The file exec.c contains the code that exports a new procedure to Guile.
> You can write analogous function using ffi with mmap, without resorting to
> external modules, of course.

You've tickled my curiosity. I'll have a look!

This one might be related (although not a direct answer to the OP's question):

  


- -- t
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlXYIY0ACgkQBcgs9XrR2kZ7QgCfRd8Q3T++b6D85VxZETrMApug
CiEAn1iStsBHgKJA8CRVtavL0V97IXtP
=SNSt
-END PGP SIGNATURE-



Re: Executing Arbitrary Machine Code in Guile

2015-08-22 Thread Jan Wedekind
Executing machine code basically just requires a call to mmap to make the data 
executable. The calling code can be generated on the fly using ffi:
http://github.com/wedesoft/aiscm

On August 22, 2015 12:31:56 AM GMT+01:00, Elzair  
wrote:
>The other day I came across a page on Rosetta Code showing how to
>directly execute x86 instructions across several languages:
>http://rosettacode.org/wiki/Machine_code
>
>For example, here is the code for Racket.
>#lang racket/base
> 
>(require ffi/unsafe)
> 
>; set up access to racket internals
>(define scheme-malloc-code
>(get-ffi-obj 'scheme_malloc_code #f (_fun (len : _intptr) ->
>_pointer)))
>(define scheme-free-code
>(get-ffi-obj 'scheme_free_code #f (_fun _pointer -> _void)))
> 
>(define opcodes '(139 68 36 4 3 68 36 8 195))
> 
>(define code (scheme-malloc-code 64))
> 
>(for ([byte opcodes]
>[i (in-naturals)])
>(ptr-set! code _ubyte i byte))
> 
>(define function (cast code _pointer (_fun _ubyte _ubyte -> _ubyte)))
> 
>(function 7 12)
> 
>(scheme-free-code code)
>
>Is this possible in Guile (with, say, the FFI)?

-- 
Jan Wedekind
http://www.wedesoft.de/



HTTP GET Shakespeare

2015-08-22 Thread umano

Hello list, I need some help.

I'm following a Computer Science course material in Python and then try 
to implement the examples and exercises in Guile Scheme.


Currently I'm working on a small program to get a text document from the 
Web and do some string operations with the content, but my 
implementation in Guile takes about 5-7 minutes to finish, while the 
Python version takes 6-8 seconds. Also, the printed results are 
different, but I'm more concerned about the time issue right now.


Could you point me to possible mistakes I'm making here?

Guile Scheme program:
https://gist.github.com/umanomata/99f103ed686acd523d9e

Python program:
https://gist.github.com/umanomata/1dbb3beb2ce19f09fcee

Thanks,



Re: HTTP GET Shakespeare

2015-08-22 Thread Ralf Mattes
On Sat, Aug 22, 2015 at 03:07:24PM -0500, um...@openmailbox.org wrote:
> Hello list, I need some help.
> 
> I'm following a Computer Science course material in Python and then try to
> implement the examples and exercises in Guile Scheme.
> 
> Currently I'm working on a small program to get a text document from the Web
> and do some string operations with the content, but my implementation in
> Guile takes about 5-7 minutes to finish, while the Python version takes 6-8
> seconds. Also, the printed results are different, but I'm more concerned
> about the time issue right now.
> 
> Could you point me to possible mistakes I'm making here?
> 
> Guile Scheme program:
> https://gist.github.com/umanomata/99f103ed686acd523d9e

Hmm - only a quick look but delete-duplicates might be the guilty part here -
looking at the (C) code in libguile/srfi-1.c it seems the implementation 
builds up a list of results and needs to scan that list for every new item in
the source list. To be fair, the docstring mentiones:

 In the worst case, this is an @math{O(N^2)} algorithm ...

One might be tempted to use some kind of hashing data structure here ;-)

 HTH Ralf Mattes

> Python program:
> https://gist.github.com/umanomata/1dbb3beb2ce19f09fcee
> 
> Thanks,
> 



Re: HTTP GET Shakespeare

2015-08-22 Thread Andreas Rottmann
um...@openmailbox.org writes:

> Hello list, I need some help.
>
> I'm following a Computer Science course material in Python and then
> try to implement the examples and exercises in Guile Scheme.
>
> Currently I'm working on a small program to get a text document from
> the Web and do some string operations with the content, but my
> implementation in Guile takes about 5-7 minutes to finish, while the
> Python version takes 6-8 seconds. Also, the printed results are
> different, but I'm more concerned about the time issue right now.
>
> Could you point me to possible mistakes I'm making here?
>
> Guile Scheme program:
> https://gist.github.com/umanomata/99f103ed686acd523d9e
>
> Python program:
> https://gist.github.com/umanomata/1dbb3beb2ce19f09fcee
>
There's a difference in algorithmic complexity here: in Scheme, you use
SRFI-1's "delete-duplicates", which is noted to be O(n^2) complexity in
the SRFI-1 specification[0]. In Python, you use set(), which is probably
implemented using a hash table, yielding roughly O(1) complexity. Since
your input set is large, it would be better to feed the read words into
a hash table, instead of using a list and SRFI-1 "delete-duplicates".

[0] http://srfi.schemers.org/srfi-1/srfi-1.html#delete-duplicates

Regards, Rotty
-- 
Andreas Rottmann -- 



Re: HTTP GET Shakespeare

2015-08-22 Thread Ralf Mattes
On Sat, Aug 22, 2015 at 10:18:19PM +0200, Andreas Rottmann wrote:
> um...@openmailbox.org writes:
> 
> > Hello list, I need some help.
> >
> > I'm following a Computer Science course material in Python and then
> > try to implement the examples and exercises in Guile Scheme.
> >
> > Currently I'm working on a small program to get a text document from
> > the Web and do some string operations with the content, but my
> > implementation in Guile takes about 5-7 minutes to finish, while the
> > Python version takes 6-8 seconds. Also, the printed results are
> > different, but I'm more concerned about the time issue right now.
> >
> > Could you point me to possible mistakes I'm making here?
> >
> > Guile Scheme program:
> > https://gist.github.com/umanomata/99f103ed686acd523d9e
> >
> > Python program:
> > https://gist.github.com/umanomata/1dbb3beb2ce19f09fcee
> >
> There's a difference in algorithmic complexity here: in Scheme, you use
> SRFI-1's "delete-duplicates", which is noted to be O(n^2) complexity in
> the SRFI-1 specification[0]. In Python, you use set(), which is probably
> implemented using a hash table, yielding roughly O(1) complexity. Since
> your input set is large, it would be better to feed the read words into
> a hash table, instead of using a list and SRFI-1 "delete-duplicates".
> 
> [0] http://srfi.schemers.org/srfi-1/srfi-1.html#delete-duplicates

Of course, that leaves us with the question why code that was explicitly moved
from scheme to the c-level "... so that using srfi-1 wouldn't have performance
penalties" uses such a problematic algorithm. 

Cheers, RalfD

> Regards, Rotty
> -- 
> Andreas Rottmann -- 
> 



Re: HTTP GET Shakespeare

2015-08-22 Thread umano

On 2015-08-22 15:18, Andreas Rottmann wrote:

um...@openmailbox.org writes:


Hello list, I need some help.

I'm following a Computer Science course material in Python and then
try to implement the examples and exercises in Guile Scheme.

Currently I'm working on a small program to get a text document from
the Web and do some string operations with the content, but my
implementation in Guile takes about 5-7 minutes to finish, while the
Python version takes 6-8 seconds. Also, the printed results are
different, but I'm more concerned about the time issue right now.

Could you point me to possible mistakes I'm making here?

Guile Scheme program:
https://gist.github.com/umanomata/99f103ed686acd523d9e

Python program:
https://gist.github.com/umanomata/1dbb3beb2ce19f09fcee


There's a difference in algorithmic complexity here: in Scheme, you use
SRFI-1's "delete-duplicates", which is noted to be O(n^2) complexity in
the SRFI-1 specification[0]. In Python, you use set(), which is 
probably

implemented using a hash table, yielding roughly O(1) complexity. Since
your input set is large, it would be better to feed the read words into
a hash table, instead of using a list and SRFI-1 "delete-duplicates".

[0] http://srfi.schemers.org/srfi-1/srfi-1.html#delete-duplicates

Regards, Rotty


Well, I'm embarrassed now. I should have looked what O(n^2) meant. It's 
just that I'm a noob and didn't even know anything about the subject of 
order of complexity :)


I'll read about hash tables then.

Thank you very much Andreas and Ralf.