Talk:Code Size

From Robowiki
Revision as of 13:15, 18 March 2009 by Skilgannon (talk | contribs) (→‎Reducing Code Size: when to inline)
Jump to navigation Jump to search

Codesize for some reason doesn't work for me. Can someone make a GUI version of it? It isn't too hard with Swing. (I've made a compression program with an algorithm of my own invention with a Swing GUI.) Or could someone post a javascript that detects it (the size) after you paste code in? Thanks! --Awesomeness 21:05, 13 March 2009 (UTC)

All you need to do to run the code size utility is "java -jar /path/to/it/codesize.jar /path/to/my/class.class". If you still can't get that working or just find it easier, you can use the "Package robot for upload" feature of Robocode which will tell you what the codesize is. Note, it's impossible for a javascript to easily detect the codesize of pastes java source code, because it's measured from the compiled bytecode not the source. --Rednaxela 21:16, 13 March 2009 (UTC)

Reducing Code Size

Well, I have a strong nano but it's roughly 280 in code size. I know I can shrink it... I'm just not very good at it. This method takes like twenty codesize points, (that's what I'll call them) and I SERIOUSLY need to shrink it. Any help please?

Code:

	public void alternate() {
		i();
		if (alternate) {
			alternate = false;
			i();
			return;
		}
			alternate = true;
	}

At least I TRIED to shrink it!


lolz,

--Awesomeness 01:22, 18 March 2009 (UTC)

The following is probably smaller codesize, but I haven't tested:

	public void alternate() {
		i();
		alternate = !alternate;
		if (!alternate) {
			i();
		}
	}

I'm not sure what i() does but I wonder if running it twice is REALLY a necessity, or if it could be worked around with very little codesize cost. Also, if codesize is critical, as it usually is for nanos, I'd suggest trying to reduce the number of number of methods in your code. If you have any methods in your code that are only called once, stop making it a seperate method. Some other tips are here in case you haven't see that. It would be easier to help if we could see more of the code, though I do have the suspicion that the biggest thing causing large codesize right now is that you may have too many methods. --Rednaxela 02:04, 18 March 2009 (UTC)

This may be even smaller

	public void alternate() {
		i();
		if (!(alternate = !alternate)) {
			i();
		}
	}

--zyx 06:36, 18 March 2009 (UTC)

  • Maybe, but I thought I tested that type of modification to not actually be helpful despite it's common usage in nanos. Doesn't hurt to test that though, I may be wrong. --Rednaxela 07:00, 18 March 2009 (UTC)
  • I have found that this type of modifications can help, but only sometimes. I think, that how many times(or some other factors) you use the same variable again affect how the compiler translates them or something like that. But my final conclusion on codesize is that everything has to be tested, because it is really hard to know before hand how it will affect codesize. --zyx 07:22, 18 March 2009 (UTC)
    • What affects it is if you have to switch between variables. Here, the boolean alternate is being accessed 3 times in a row, so inlining it wouldn't help. However, if you called i() in between changing it and accessing it for the if then inlining would help because alternate does not have to be re-loaded into the registers. So what really matters is execution order, not whether it is inlined. Sometimes, however, it only possible to get the execution order correct by inlining. By 'correct' I mean switching between different variables as little as possible. --Skilgannon 11:15, 18 March 2009 (UTC)
  • Hm, this tempts me to try coding a nanobot using a java assembler in order to see if I can make any smaller codesize bytecode than the normal compiler would... --Rednaxela 07:35, 18 March 2009 (UTC)
  • If you really want to go that deep I recommend this book, it is the official JavaTM Virtual Machine Specification. It fully explains how a class file is encoded, there are many weird stuff in that VM. I haven't used that for codesize at all, I used it for a homework once. --zyx 07:52, 18 March 2009 (UTC)

You cannot post new threads to this discussion page because it has been protected from new threads, or you do not currently have permission to edit.

Contents

Thread titleRepliesLast modified
Writing nanobots using Java Assembly directly405:54, 17 May 2019

Writing nanobots using Java Assembly directly

It seems that openjdk has a wonderful tool called asmtools that allows two directional transformation between jasm and .class bytecode

https://wiki.openjdk.java.net/display/CodeTools/asmtools

Has anyone tried writing robocode bots with java assembly directly? Since the relationship between Java and Bytecode is not as direct as jasm and bytecode, writing with jasm directly may be a better way than tricks listed on this wiki.

Xor (talk)15:03, 6 May 2019

Rednaxela did mention on his user page:

Bots[edit]

Figments of my Imagination[edit]

Probably reality within the next year[edit]

  • Fleck - A NanoBot coded in java assembly to have more direct control of how the codesize is used. Might just be my own take on the 'NanoSurfer' concept too...

User:Rednaxela

It seems that he thought of the same idea in 2009, though he never got around to implementing it.

MultiplyByZer0 (talk)10:06, 7 May 2019
 

I believe many of the top nanobots use java assembly or Jikes to shrink their code. I haven't used it in my bots yet, but I plan to.

Slugzilla (talk)13:18, 8 May 2019
 

I've had some success using Proguard to shrink a few bytes off of microbots when they really need it - eg. Yatagan requires this. It usually does it by re-assigning variables into a slot that is no longer used - note that the actual java asm is not type-aware, so you can stick one type of object into the variable you previously used for a different type of variable. This means you can better re-use the initial few variables, which have lower codesize cost.

IMO you'll get better effective shrinking by exploring the library - for example, the pattern matcher nanos are all using string and substring indexOf functions, they would never have space to put all of the actual matching code in.

Skilgannon (talk)21:55, 9 May 2019

I think making use of library may be much useful than micro-optimizing bytecode size. Bytecode tools can be used for a few additional bytes though.

Xor (talk)05:54, 17 May 2019