V. Glenn Tarcea | 21 Aug 22:37
Favicon

Executing words on data farther up the stack

Hi,

 

I’ve been working on an interface to BerkeleyDB for Factor. One thing I’ve noticed as I’ve worked through this is a common idiom like the following:

 

Say I have a tuple to hold my BerkeleyDB data like DB_ENV, DB, etc…. When I need to make a call I often end up with a stack that has the berkeleydb object on the bottom of the stack (denoted by <bdb>). When I make a call for example to open the database I’ll end up with a call like:

 

“/tmp/db” XXX  bdb-open ! where XXX are flags or’d together.

 

So my stack looks like:

<bdb>

“/tmp/db”

XXX

 

The code in the bdb-open word wants to grab the <bdb> object and pull a couple of items from it.

 

I was ending up with code that looked like:

>r >r [ dbenv>> ] [ db>> ] bi r> r>

 

So, I ended up writing some utility functions that I put in a vocabulary I’m tentatively calling “combinatorsn”. The vocabulary implements the spread and cleave combinators but allows you to execute them at arbitrary stack positions. For example rather than the >r >r code above I have:

[ dbenv>> ] [ db>> ]  bi-2

 

This word acts just like bi, except it skips over the first 2 stack entries and starts execution on the 3rd stack item, restoring the first 2 entries when done.

 

So:

 

“1 2 3 [ 100 + ] [ 200 + ] bi-2” results in a stack that looks like:

101

201

2

3

 

There is also a bi-n for arbitrary depth:

1 2 3 [ 100 + ] [ 200 + ] 2 bi-n

 

At the moment I have:

bi-n, bi-1, bi-2, bi-3

bi*-n, bi*-1 bi*-2 bi*-3

spread-n

cleave-n

2cleave-n

3cleave-n

 

I’m working my way through supporting all the other operators. Would this be of use to anyone else? Is there perhaps a better way? I’ve noticed I’m doing a fair bit of stack shuffling in my code as I slowly get used to working with the stack.

 

Thanks,

 

Glenn

 

V. Glenn Tarcea

gtarcea-63aXycvo3TyHXe+LvDLADg@public.gmane.org

Hey buddy, can you paradigm?

 

 

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Factor-talk mailing list
Factor-talk@...
https://lists.sourceforge.net/lists/listinfo/factor-talk
Slava Pestov | 21 Aug 22:54

Re: Executing words on data farther up the stack

On Thu, Aug 21, 2008 at 3:39 PM, V. Glenn Tarcea <gtarcea-63aXycvo3TyHXe+LvDLADg@public.gmane.org> wrote:

Hi,

 

I've been working on an interface to BerkeleyDB for Factor.


Cool!

Say I have a tuple to hold my BerkeleyDB data like DB_ENV, DB, etc…. When I need to make a call I often end up with a stack that has the berkeleydb object on the bottom of the stack (denoted by <bdb>). When I make a call for example to open the database I'll end up with a call like:

 

"/tmp/db" XXX  bdb-open ! where XXX are flags or'd together.

 

So my stack looks like:

<bdb>

"/tmp/db"

XXX

Why doesn't bdb-open create a new berkleydb object and return it on the stack?
 

 The code in the bdb-open word wants to grab the <bdb> object and pull a couple of items from it.

 

I was ending up with code that looked like:

>r >r [ dbenv>> ] [ db>> ] bi r> r>

You can use 2dip here.
 

 I'm working my way through supporting all the other operators. Would this be of use to anyone else? Is there perhaps a better way? I've noticed I'm doing a fair bit of stack shuffling in my code as I slowly get used to working with the stack.

You will find that you write less and less stack shuffling as you become more experienced with Factor. So I encourage you to experiment with new combinators, and so on; but always be on the lookout for tricks to simplify your code so that you don't need to shuffle at all.

I'm working on a new compiler right now; in 11,000 lines of code, there are less than a dozen usages of rot, -rot and pick combined. Even swap only appears in 165 lines; so less than 1 in 50 lines of code needs a swap. This is because I try to ensure that everything is already in the right order on the stack.

Slava
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Factor-talk mailing list
Factor-talk@...
https://lists.sourceforge.net/lists/listinfo/factor-talk
Glenn Tarcea | 22 Aug 03:51
Favicon

Re: Executing words on data farther up the stack


Why doesn't bdb-open create a new berkleydb object and return it on the stack?
 

I've been debating what the proper thing to do here is. That will help, but I'll still end up with calls where <bdb> floats to the bottom of the stack.

>r >r [ dbenv>> ] [ db>> ] bi r> r>

You can use 2dip here.
 

Ahhh.... That was one of those words I read about but it didn't click, thanks so much for pointing that out...

I'm working on a new compiler right now; in 11,000 lines of code, there are less than a dozen usages of rot, -rot and pick combined. Even swap only appears in 165 lines; so less than 1 in 50 lines of code needs a swap. This is because I try to ensure that everything is already in the right order on the stack.


Wow, that is really something. I have at least a dozen uses in one file :-) (and that's after factoring it down some). Will you be posting on approaches you've learned?

On a side note I have a question on the object system. Is it possible to override the accessor functions? That is could I do something like you can do in CLOS:

(defclass my-class ()
  ((a :documentation "a")))

(defgeneric (setf a) (a class))

(defmethod (setf a) (value (class my-class))
  (if (< a 10)
      (error  "bad value")
     (with-slots (a) my-class
       (setf a value))))

It looks like the >>a and a>> for a tuple would be automatically created and can't be hidden or overridden, is that true?

Thanks,

Glenn


V. Glenn Tarcea
Hey brother, can you paradigm?





-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Factor-talk mailing list
Factor-talk@...
https://lists.sourceforge.net/lists/listinfo/factor-talk
Eduardo Cavazos | 22 Aug 04:21

Re: Executing words on data farther up the stack

V. Glenn Tarcea wrote:

> I've been working on an interface to BerkeleyDB for Factor.

Hi Glenn,

An approach worth exploring is where you keep the bdb object in a variable.

Ed

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
Glenn Tarcea | 22 Aug 04:29
Favicon

Re: Executing words on data farther up the stack


Hi Ed,

Thanks for the suggestion! If I did this could I have multiple databases open? That is, if my constructor was <bdb> and I did that twice, could I associate a variable with each <bdb> instance?

Thanks,

Glenn

On Aug 21, 2008, at 10:21 PM, Eduardo Cavazos wrote:

V. Glenn Tarcea wrote:

I've been working on an interface to BerkeleyDB for Factor.

Hi Glenn,

An approach worth exploring is where you keep the bdb object in a variable.

Ed

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
_______________________________________________
Factor-talk mailing list



V. Glenn Tarcea
Hey brother, can you paradigm?





-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Factor-talk mailing list
Factor-talk@...
https://lists.sourceforge.net/lists/listinfo/factor-talk
Eduardo Cavazos | 22 Aug 04:48

Re: Executing words on data farther up the stack

Glenn Tarcea wrote:

> Thanks for the suggestion! If I did this could I have multiple
> databases open? That is, if my constructor was <bdb> and I did that
> twice, could I associate a variable with each <bdb> instance?

Sure, no problem.

The idea is that the words in your library would operate on the db handle 
stored in a certain variable. Let's say you call it 'db'.

	SYMBOL: db

Let's assume you use this word to open a database:

	: open-db ( file -- db-handle ) ... ;

A user might open a few databases:

SYMBOL: db-1
SYMBOL: db-2

	"/tmp/db-1" open-db db-1 set
	"/tmp/db-2" open-db db-2 set

Now is they want to operate on 'db-1' they do:

	db-1 get db set

	... call some db words here ...

You can have a 'with-db' word:

	: with-db ( db quot -- ) ... ;

Which calls quot with the db argument bound to 'db'. So code would look like:

	db-1 get
	  [ ... operations on db-1 ]
	with-db

	db-2 get
	  [ ... operations on db-2 ]
	with-db

Using a variable for cases like this is very common. That you were running 
into some tricky stack situations is a hint that using a variable might be in 
order.

Ed

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/

Gmane